Structures vector

I have a code provided to me by another person who has a structure

struct Pair { string s1; string s2; bool equivalent; }; 

He then sets the vector of these hardcoded structures.

 std::vector<Pair> PairID; staticdata() { PairID={{"string","string2",true}, {"string","string3",true}, {"string","string4",false}, {"string","string7",false}, {"string3","string8",false} }; } 

Unfortunately, my compiler complains about the string PairID = {{"string", "string2", true},

Why is this? He suggested compiling with -std = C ++ 0x, but my compiler (gcc 4.2) does not support this. Is there an easy way to convert code to make it work? Why does this fail?

I use Mac OSX and prefer not to update my compiler

+4
c ++ struct vector
Nov 11 '10 at 15:10
source share
5 answers

Your code is not legal C ++. This is legal C ++ 0x, but there have been many changes to the language. Therefore, if you want to compile this code as C ++ code, you need to change it.

The PigBen solution is one way, the problem is that temporary data can be built and destroyed many times or live for a long time.

Here's another way:

  struct Pair { string s1; string s2; bool equivalent; }; Pair make_Pair(const string& s1, const string& s2, bool equivalent) { Pair ret; ret.s1 = s1; ret.s2 = s2; ret.equivalent = equivalent; return ret; } // somewhere in the init code... std::vector<Pair> PairID; PairID.push_back(make_Pair("string","string2",true)); PairID.push_back(make_Pair("string","string3",true)); PairID.push_back(make_Pair("string","string4",false)); PairID.push_back(make_Pair("string","string7",false)); PairID.push_back(make_Pair("string3","string8",false)); 
+5
Nov 11
source share

Why doesn't it work?

Because its invalid C ++. It will be, in C ++ 0x. But for now, this is simply not valid. And since your compiler does not yet support C ++ 0x, you will need to do this in a complicated way, that is, fill the vector with one element at a time or copy from the C ... array:

 Pair data[] ={ {"string","string2",true}, {"string","string3",true}, {"string","string4",false}, {"string","string7",false}, {"string3","string8",false} }; PairID.assign(data, data + sizeof(data) / sizeof(Pair)); 

(This will require standard algorithm and iterator headers.)

+6
Nov 11 2018-10-11
source share
 void staticdata() { Pair temp[] = { {"string","string2",true}, {"string","string3",true}, {"string","string4",false}, {"string","string7",false}, {"string3","string8",false} }; PairID.assign(temp,temp+5); } 
+4
Nov 11
source share

You can use Boost.Assign, which is syntactic sugar for creating a vector, and then populates it:

 using std::vector<int>; using namespace boost::assign; vector<int> v = list_of(Pair("s11", "s12", true)(Pair("s21", "s22", false)); 
+1
Nov 11 2018-10-11
source share
 struct Pair { string s1; string s2; bool equivalent; }; std::vector<Pair> PairID; Pair pair; pair.s1 = "abc"; pair.s2 = "abc"; pair.equivalent = TRUE; PairID.push_back(pair); 
0
Jan 08 '18 at 5:31
source share