开发者

Is this a valid datastructure Map<string, string, string> in C++?

I have to store 3 strings per variable, but don't know which is the best data structure to use for that in C++.

I can think of only Struct, but not sure if it is the best way to do it.

Something like string var[100][3], first dimension(100) should be dynamically add and remove.

I tried a开发者_如何学运维ll sorts of things with map, multimap.

Any help is appreciated. Thank you


If you have always exactly 3 strings together in a triplet and want to have multiple triplets, then define struct with three strings and put it to std::vector.

struct Triplet {
  std::string a,b,c;
};

std::vector<Triplet> data;


Map<string, string, string> is not valid. However, you could create a new data structure with 3 strings and store it in a vector.

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class data_structure
{
public:
    string first;
    string second;
    string third;
};


int main()
{
    vector<data_structure> my_vec;

    data_structure elem1;
    elem1.first = "one";
    elem1.second = "two";
    elem1.third = "three";

    my_vec.push_back(elem1);

    data_structure elem2;
    elem2.first = "four";
    elem2.second = "five";
    elem2.third = "six";

    my_vec.push_back(elem2);

    for (int i = 0; i < my_vec.size(); i++)
    {
        // print stuff
    }

}


If you always want 3 strings, then a tuple would require less overhead.

#include <iostream>
#include <string>
#include <tuple>

typedef std::tuple<std::string, std::string, std::string> MyTuple;

int
main(int argc, char **argv)
{
    MyTuple t =
        make_tuple(
                std::string("string 1"),
                std::string("string 2"),
                std::string("string 3")
                );

    std::cout
        << std::get<0>(t) << std::endl
        << std::get<1>(t) << std::endl
        << std::get<2>(t) << std::endl;

    return 0;
}


you can use a class or a tuple, and store the tuple in a vector

std::vector<boost::tuple<std::string, std::string, std::string> > v;
boost::tuple<std::string, std::string, std::string> t = boost::make_tuple(s1, s2, s3);
v.push_back(t)


Another to the above mix of suggestions: Boost Tuple (if you have Boost installed already).


Map <string , vector <string> > Will allow you to map a vector of strings to a string key value. If you have the same number of strings mapped to each key then use an array of strings to reduce the memory overhead of the vector.


You can use vector to store N elements

vector<string> three_strings(3);

 three_strings.push_back("one");
 three_strings.push_back("two");
 three_strings.push_back("three");

Please note that tuple is an alternative, BUT: (1) it is part of tr1 standard and therefore may not be available on your C++ compiler/installation yet; (2) It stores heterogeneous data (e.g. 3 random type items, not necessarily 3 strings) which may be an overkill


How about vector<vector<string> > ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜