4

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 all sorts of things with map, multimap.

Any help is appreciated. Thank you

rda3mon
  • 1,709
  • 4
  • 25
  • 37

8 Answers8

8

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;
Al Kepp
  • 5,831
  • 2
  • 28
  • 48
3

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
    }

}
karlphillip
  • 92,053
  • 36
  • 243
  • 426
2

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;
}
Judge Maygarden
  • 26,961
  • 9
  • 82
  • 99
1

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)
kinnou02
  • 644
  • 8
  • 15
1

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

yasouser
  • 5,113
  • 2
  • 27
  • 41
0

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

DVK
  • 126,886
  • 32
  • 213
  • 327
0

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.

Jordan
  • 4,928
  • 4
  • 26
  • 39
0

How about vector<vector<string> > ?

Stephen Chu
  • 12,724
  • 1
  • 35
  • 46