Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++'s equivalent to C#'s Dictionary and list

So in C#, I have something similar to the following:

Dictionary<string, List<string>>

What's the most efficient way to do that in C++? I know c++ has 'map' and 'list' but I'm still in the pseudocode phase of writing this function, so I was wondering if something like this is even possible in C++. If so, how's the best way to make that equivalent data structure?

Thanks

like image 610
Saif Ahsanullah Avatar asked Apr 01 '16 20:04

Saif Ahsanullah


1 Answers

so I was wondering if something like this is even possible in C++

Yes. The STL features are variety of different containers: http://www.cplusplus.com/reference/stl/.

If so, how's the best way to make that equivalent data structure?

That depends on your requirements. For example std::vector vs std::list (see here for further information)

For a simple case I would just suggest you to use something like this:

#include <vector>
#include <map>
#include <string>

int main()
{
  std::map<std::string, std::vector<std::string>> map_of_strings;

  map_of_strings["a"] = { "1", "2", "3" };
  map_of_strings["b"] = { "4", "5", "6" };
  map_of_strings["c"] = { "7", "8", "9" };

  return 0;
}
like image 78
Lukas Avatar answered Oct 09 '22 02:10

Lukas