Vector Newbie...

Mar 22, 2014 at 2:27am
So, I'm learning about vectors, and having a whole lot of fun... No, it's a pain. I had a 2D char array and would like to have a vector instead. I'll post the array below. It's a map. (A very pretty one, if you ask me). Like I said I am pretty much totally new to vectors. I'm vaguely familiar with the concept; I need to learn to use them, but can't get it right.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  char world[33][72] = {
{"PPPPPPPPPPPPPPPPPPP-------------------MMMMMM-MMMMMMMMMMMMMMMMMMMMMMMMMM"},
{"PP-PPPPPPP------PPPPPPPP--------------MMMMMM--MMMMMMMMMMMMMMMMMMMM/ \\MM"},
{"PPPPPPPPP---/ \\---PPPPPPP---------------MMMMM---MMMMMMMMMMMMMMMMMM| |MM"},
{"PPPPPPPPPP--| |----PPPPPPP-------------MMMMMMMM--MMMMMMMMMMMMMMMM--MMMM"},
{"PPPPPPPPPPPP---PPPPPPPPP-----------------MMMMM------MMMMMMMMMMMMMMMMMMM"},
{"PPPPPPPPPPPPP-PPPP------------------------MM--------------MMMMMMMMMMMMM"},
{"PPPPPPPP--------------------------------------------------------MMMMMMM"},
{"PPPPPPPPPP-------------------------------------------------------------"},
{"PPPPPP---------------------------------------------------------^^------"},
{"PPP--------------MMM------------------------------------------^  ^-----"},
{"-------------~~~~~~~MM-------------/ \\-----------------------^^  ^^----"},
{"------------~~~~~~~~~~MM-----------| |---------------------------------"},
{"-----------~~~~~~~~~~~~M-----------------------------------------------"},
{"------------~~~~~~~~~MM----------------------------------~~~~~~~~~~----"},
{"--------------~~~~~~M-----------------------------------~~~~~~~~~~~~PP-"},
{"----------------MMMM-----------------------------------~~~~~~~~~~~~~~P-"},
{"-------------------------------------------------------~~~~~~~~~~~~~~PP"},
{"--------------------------------------------------------~~~~~~~~~~~~PP-"},
{"------------^^--------------------------------------PPPP-~~~~~~~~~~-PPP"},
{"-----------^  ^-----------------------------------PPPPPPP-~~~~~~~~-PPPP"},
{"----------^^  ^^---------------------------PPPPPPPPPPPPPPPPPPPPPPPPPPPP"},
{"---------------------------------------------PPPPPPPPPPPPPPPPPPPPPPPPP-"},
{".....-------------------------------/ \\-------PPPPPPPPPPPPPPPPPPPPPPP--"},
{"........----------------------------| |------PPPPPPPPPPPPPPPPPPPPPPPPP-"},
{"...........--------------------------------------PPPPPPPPPPPPPPPPPPPPPP"},
{".............-----------------------------------PPPPPPPPPPPPPPPPPPPPPPP"},
{"..............---------------------------------PPPPPPPPPPPPPPPPPPPPPPPP"},
{"...............-----------------------------------PPPPPPPPPPPPPPPPPPPPP"},
{"..~...........-------------------------------------PPPPPPPPPPPPPPPPPPPP"},
{".~~~...........-----------^^---------------------------PPPPPPPPPPPPPPPP"},
{"..~.............---------^  ^--------------------------------PPPPPPPPPP"},
{"...............---------^^  ^^------------------------------PPPPPPPPPPP"},
};


Really any help with vectors would be nice, since all the reference material is a bit wordy, and nobody else seems to have had problems with this, so I can't find it on the forums. Thank you in advance.
Mar 22, 2014 at 2:56am
You haven't really said what you want to use a vector for. I'm assuming you want to store your map in a vector instead of a two dimensional character array. A vector of strings works well for this.

1
2
3
4
5
6
7
8
9
#include <vector> 
#include <string> 
using namespace std; 

vector<string> map;

map.push_back ("PPPPPPPPPPPPPPPPPPP-------------------MMMMMM-MMMMMMMMMMMMMMMMMMMMMMMMMM");
...
map.push_back ("...............---------^^  ^^------------------------------PPPPPPPPPPP");



Mar 22, 2014 at 4:00am
Ok, so when I do that, it says that
'std' does not name a type
and
"world" does not name a type
I used world instead of map.
Mar 22, 2014 at 4:58am
What is your exact code? If you wanted to make a map of your world, you can do it like this (using exactly what you have there):
1
2
3
4
5
6
7
8
9
#include <vector>
#include <string>

const std::vector<std::string> world {
    "PPPPPPPPPPPPPPPPPPP-------------------MMMMMM-MMMMMMMMMMMMMMMMMMMMMMMMMM",
    "PP-PPPPPPP------PPPPPPPP--------------MMMMMM--MMMMMMMMMMMMMMMMMMMM/ \\MM",
    "PPPPPPPPP---/ \\---PPPPPPP---------------MMMMM---MMMMMMMMMMMMMMMMMM| |MM",
    // etc...
};


And then you can access individual elements like normal:
1
2
char posType = world[row][col];
if (posType == '-') world[row][col] = 'X';


Or you can loop through and print the vector:
1
2
for (const std::string& line : world)
    std::cout << line << '\n';


Hope this helps! If it didn't, please rephrase your questions and give us more information.
Mar 22, 2014 at 8:05pm
Thanks NT3, that's what I want to do. it says
error: in C++98 'world' must be initialized by constructor, not by '{...}'|
and
error: no matching function for call to 'std::vector<std::basic_string<char> >::vector(<brace-enclosed initializer list>)'|

It also said I needed to add a semicolon or comma before using, in 'using namespace std'; Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <vector>
#include <string>

const std::vector<std::string> world{
"PPPPPPPPPPPPPPPPPPP-------------------MMMMMM-MMMMMMMMMMMMMMMMMMMMMMMMMM",
"PP-PPPPPPP------PPPPPPPP--------------MMMMMM--MMMMMMMMMMMMMMMMMMMM/ \\MM",
"PPPPPPPPP---/ \\---PPPPPPP---------------MMMMM---MMMMMMMMMMMMMMMMMM| |MM",
"PPPPPPPPPP--| |----PPPPPPP-------------MMMMMMMM--MMMMMMMMMMMMMMMM--MMMM",
"PPPPPPPPPPPP---PPPPPPPPP-----------------MMMMM------MMMMMMMMMMMMMMMMMMM",
"PPPPPPPPPPPPP-PPPP------------------------MM--------------MMMMMMMMMMMMM",
"PPPPPPPP--------------------------------------------------------MMMMMMM",
"PPPPPPPPPP-------------------------------------------------------------",
"PPPPPP---------------------------------------------------------^^------",
"PPP--------------MMM------------------------------------------^  ^-----",
"-------------~~~~~~~MM-------------/ \\-----------------------^^  ^^----",
"------------~~~~~~~~~~MM-----------| |---------------------------------",
"-----------~~~~~~~~~~~~M-----------------------------------------------",
"------------~~~~~~~~~MM----------------------------------~~~~~~~~~~----",
"--------------~~~~~~M-----------------------------------~~~~~~~~~~~~PP-",
"----------------MMMM-----------------------------------~~~~~~~~~~~~~~P-",
"-------------------------------------------------------~~~~~~~~~~~~~~PP",
"--------------------------------------------------------~~~~~~~~~~~~PP-",
"------------^^--------------------------------------PPPP-~~~~~~~~~~-PPP",
"-----------^  ^-----------------------------------PPPPPPP-~~~~~~~~-PPPP",
"----------^^  ^^---------------------------PPPPPPPPPPPPPPPPPPPPPPPPPPPP",
"---------------------------------------------PPPPPPPPPPPPPPPPPPPPPPPPP-",
".....-------------------------------/ \\-------PPPPPPPPPPPPPPPPPPPPPPP--",
"........----------------------------| |------PPPPPPPPPPPPPPPPPPPPPPPPP-",
"...........--------------------------------------PPPPPPPPPPPPPPPPPPPPPP",
".............-----------------------------------PPPPPPPPPPPPPPPPPPPPPPP",
"..............---------------------------------PPPPPPPPPPPPPPPPPPPPPPPP",
"...............-----------------------------------PPPPPPPPPPPPPPPPPPPPP",
"..~...........-------------------------------------PPPPPPPPPPPPPPPPPPPP",
".~~~...........-----------^^---------------------------PPPPPPPPPPPPPPPP",
"..~.............---------^  ^--------------------------------PPPPPPPPPP",
"...............---------^^  ^^------------------------------PPPPPPPPPPP",
}
Last edited on Mar 22, 2014 at 8:06pm
Mar 22, 2014 at 9:36pm
What compiler are you using? Assuming Clang or GCC, add an option -std=c++11 to the command line options, that should fix it. Also, sorry, I forgot the semicolon on the end of the initializer list (it is a variable, not a function!). Just throw one on the end.
Mar 22, 2014 at 10:04pm
closed account (iAk3T05o)
Where is int main()?
Mar 22, 2014 at 10:15pm
NT3, thank you so much. I kept seeing things like that, and when I would try to define it with the brackets, it said that. (I actually did try it like that a couple times before I asked, since I read to do it that way.)
Topic archived. No new replies allowed.