C ++相当于C#的词典和列表

时间:2016-04-01 20:02:44

标签: c# c++ list dictionary data-structures

所以在C#中,我有类似于以下内容:

Dictionary<string, List<string>>

在C ++中最有效的方法是什么?我知道c ++有'map'和'list'但是我仍处于编写这个函数的伪代码阶段,所以我想知道在C ++中是否有这样的东西是可能的。如果是这样,那么制作等效数据结构的最佳方法是什么?

由于

2 个答案:

答案 0 :(得分:5)

  

所以我想知道这样的事情是否可以在C ++中实现

是。 STL功能有各种不同的容器:http://www.cplusplus.com/reference/stl/

  

如果是这样,那么制作等效数据结构的最佳方法是什么?

这取决于您的要求。例如std::vector vs std::list(有关详细信息,请参阅here

对于一个简单的案例,我建议你使用这样的东西:

#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;
}

答案 1 :(得分:1)

您可以使用:map<string, vector<string>>Map最接近C#DictionaryVector最接近C#List

如果我们从任何语言中抽象出来,那就有:

可调整大小的数组 - C#中的List,C ++中的Vector

键值对的集合/容器 - C#中的Dictionary和C ++中的Map