Vector of Structures

前端 未结 6 463
悲哀的现实
悲哀的现实 2021-01-18 11:42

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

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


        
6条回答
  •  时光取名叫无心
    2021-01-18 12:18

    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 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));
    

提交回复
热议问题