7

I have some code given to me by another person in which we have a structure

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

Then he sets up a vector of these structs hard coded

std::vector<Pair> PairID;

  staticdata() {
      PairID={{"string","string2",true}, 
      {"string","string3",true}, 
      {"string","string4",false}, 
      {"string","string7",false}, 
      {"string3","string8",false}
    };
    }

Unfortunately my compiler is complaining on the line PairID={{"string","string2",true},

Why is this? He suggested to compile using -std=c++0x but my compiler (gcc 4.2) does not support this. Is there an easy way to convert the code so it works? Why is it failing??

I am using Mac OSX and would prefer not to update my compiler

Robert
  • 181
  • 1
  • 3
  • 9

6 Answers6

8

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

PigBen's solution is one way, the problem with it being the temporary data could be constructed & 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));
John Dibling
  • 99,718
  • 31
  • 186
  • 324
  • Then again, the temporary data in PigBen's answer could be made into a constant static variable, which would therefore take less room and be faster to copy than your solution. – Victor Nicollet Nov 11 '10 at 15:33
  • @Victor: To be sure, I would probably use a solution like PigBen's unless for whatever reason I didn't want to. – John Dibling Nov 11 '10 at 15:34
  • @Victor: But I seriously doubt that in an optimized build PigBen's solution will be any faster than this. – John Dibling Nov 11 '10 at 15:36
7

Why is it failing?

Because it’s not valid C++ before C++11. Something quite close will be valid afterwards. But in C++03, it’s just not valid. And since your compiler doesn’t yet support C++11, you will need to do it the hard way, i.e. populate the vector one element at a time, or copy from a 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 *data);
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
4
void staticdata() {
    Pair temp[] =
    {
        {"string","string2",true}, 
        {"string","string3",true}, 
        {"string","string4",false}, 
        {"string","string7",false}, 
        {"string3","string8",false}
    };

    PairID.assign(temp,temp+5);
}
Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
1

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

using std::vector<int>;
using namespace boost::assign;
vector<int> v = list_of(Pair("s11", "s12", true)(Pair("s21", "s22", false));
wilhelmtell
  • 57,473
  • 20
  • 96
  • 131
0
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

If you know the size of vector in advance then you can go for this

You can use any temporary instance of structure and assign the default values to that and then use a normal assignment like

typedef struct mystruct{
    int x;
    char name[10];
}mystruct;

mystruct default;
default.x = 1;
strcpy(default.name,"default_name");
vector <mystruct> v(10,default);
Onk_r
  • 836
  • 4
  • 21