vector<vector<string>> inside map

Hi,

I don't know if what I am trying to do is crazy. but the following is the problem

I have a map:

1
2
//Global.h
map<string,vector<vector<string>>> mill_positions;


and I am trying to insert into the map as in the following snippet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Misc_Functions.cpp includes Global.h

        vector<string> mill_pos_v;
	vector<vector<string>> mill_pos_set_v;
	//a0
	mill_pos_v.push_back("b1");
	mill_pos_v.push_back("c2");
	mill_pos_set_v.push_back(mill_pos_v);
	mill_pos_v.clear();
	mill_pos_v.push_back("a3");
	mill_pos_v.push_back("a6");
	mill_pos_set_v.push_back(mill_pos_v);
	mill_pos_v.clear();
	mill_pos_v.push_back("d0");
	mill_pos_v.push_back("g0");
	mill_pos_set_v.push_back(mill_pos_v);
	mill_pos_v.clear();

        ///the value does not get inserted into the map here
	mill_positions["a0"]=mill_pos_set_v; 
	mill_pos_set_v.clear();


but nothing gets inserted into the map. Is there some other way I should do this? Or is there any other approach to do the same task?

I hope I've been able to make myself clear. Could someone please help me with this?

Thanks in advance!
Last edited on
That looks fine to me. What is mill_positions and where is it declared/used?
Thanks for the response. This is the code for a variation of the nine-men's morris game.

the mill positions are 3 adjacent positions dictated by game rules.

this map has been declared in a header "Global.h" and used in "Misc_Functions.cpp"

I get no compiler errors.
He means what type is mill_positions.

This works as expected:

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <vector>
#include <map>
#include <string>
#include <iostream>

using namespace std;

ostream& operator<<(ostream& os, const vector<vector<string>>& v) ;

int main()
{
	map<string,vector<vector<string> > > mill_positions ;


	vector<string> mill_pos_v;
	vector<vector<string>> mill_pos_set_v;
	//a0
	mill_pos_v.push_back("b1");
	mill_pos_v.push_back("c2");
	mill_pos_set_v.push_back(mill_pos_v);
	mill_pos_v.clear();
	mill_pos_v.push_back("a3");
	mill_pos_v.push_back("a6");
	mill_pos_set_v.push_back(mill_pos_v);
	mill_pos_v.clear();
	mill_pos_v.push_back("d0");
	mill_pos_v.push_back("g0");
	mill_pos_set_v.push_back(mill_pos_v);
	mill_pos_v.clear();

        ///the value does not get inserted into the map here
	mill_positions["a0"]=mill_pos_set_v; 
	mill_pos_set_v.clear();

	vector<vector<string>>& v = mill_positions["a0"] ;

	cout << v ;
}

ostream& operator<<(ostream& os, const vector<vector<string>>& v)
{
	os << "{\n" ;

	for ( auto i = v.begin() ; i != v.end() ; ++i )
	{
		os << "\t{" ;
		for ( auto j = i->begin() ; j != i->end() ; ++j )
			os << ' ' << *j ;
		os << "}\n" ;
	}

	return os << "}\n" ;
}

{
        { b1 c2}
        { a3 a6}
        { d0 g0}
}
Press any key to continue . . .
Last edited on
Globals are generally not a good idea to have around, but...are they properly declared extern everywhere except one cpp file? I'd except you'd get compiler errors if they weren't, but maybe not.
I do not think I can do away with globals in this case, since these data structures declared in Global.h has to be used by all functions and persisted across the game.

Or, do you think its a better I idea to pass the data structure to functions each time rather than making them global?

Yes, they have been declared as externs everywhere except in Misc_Functions.cpp
Last edited on
@cire

Thanks for the response.

This is interesting.

I am using Visual Studio 2008, and what I see is that although like in your code I am able to retrieve the value by key.

When I check the value using "quick watch" of VS (using a breakpoint), it shows the value of map as (key "error" value 0).

This seems strange to me, does anyone know the reason?

I dunno, your code looks perfectly fine to me. I can't think of any reason why it wouldn't be being set correctly...
VS2008 just doesn't handle iterating the tree internal to the map in the debug view. The debugger is a bit more sophisticated in vs10.
@cire,

I do not think it's just a debug view issue, because when I tried to access the value in the map using a map iterator, it returned a null value.

I however could retrieve values using their keys as index.

Now I all the more want to know what could possibly be the problem!
If I modify the code I posted to iterate through the map and compile on VS2008, the output is still as expected:

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <vector>
#include <map>
#include <string>
#include <iostream>

using namespace std;

typedef vector<string> vs  ;
typedef vector<vs>     vvs ;

ostream& operator<<(ostream& os, const vvs& v) ;
ostream& operator<<(ostream& os, const map<string, vvs>&m) ;

int main()
{
	map<string,vvs> mill_positions ;


	vs mill_pos_v;
	vvs mill_pos_set_v;
	//a0
	mill_pos_v.push_back("b1");
	mill_pos_v.push_back("c2");
	mill_pos_set_v.push_back(mill_pos_v);
	mill_pos_v.clear();
	mill_pos_v.push_back("a3");
	mill_pos_v.push_back("a6");
	mill_pos_set_v.push_back(mill_pos_v);
	mill_pos_v.clear();
	mill_pos_v.push_back("d0");
	mill_pos_v.push_back("g0");
	mill_pos_set_v.push_back(mill_pos_v);
	mill_pos_v.clear();

        ///the value does not get inserted into the map here
	mill_positions["a0"]=mill_pos_set_v; 
	mill_pos_set_v.clear();

	cout << mill_positions << '\n' ;

}

ostream& operator<<(ostream& os, const map<string,vvs>& m)
{
	for ( map<string,vvs>::const_iterator i = m.begin(); i!= m.end(); ++i )
		os << i->first << ": " << i->second << '\n' ;
	return os ;
}

ostream& operator<<(ostream& os, const vvs& v)
{
	os << "{ " ;

	for ( vvs::const_iterator i = v.begin() ; i != v.end() ; ++i )
	{
		os << "{" ;
		for ( vs::const_iterator j = i->begin() ; j != i->end() ; ++j )
			os << (j == i->begin()? "" : " ") << *j ;
		os << "} " ;
	}

	return os << '}' ;
}

a0: { {b1 c2} {a3 a6} {d0 g0} }

Press any key to continue . . .


And, no, quickwatch still won't handle the iterator correctly. Too much indirection. I never used quickwatch, because it was just too useless. However a regular watch on the iterator does the job if you expand it in the watch window. In VS10, you can just hover over the expression in the source to get the result of a regular watch expression.
Last edited on
Yes, I see that it works! Thanks for the code, I need to figure out where I am messing up.
Topic archived. No new replies allowed.