Is this a valid datastructure Map <string, string, string> in C ++?
I have to store 3 lines for each variable, but I donβt know which one is the best to use for C ++. I can only think of Struct, but I'm not sure if this is the best way to do this.
Something like the string var [100] [3], the first dimension (100) should dynamically add and remove.
I tried all kinds of things with a map, multimap.
Any help is appreciated. thank you
Map<string, string, string> invalid. However, you can create a new data structure with three rows and save 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 lines, then tuple will 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 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 the tr1 standard and, therefore, may not be available on your compiler / installation in C ++; (2) It stores heterogeneous data (e.g. 3 random types, not necessarily 3 rows) that may be full