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

Sai*_*lah 4 c# c++ dictionary list data-structures

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

Dictionary<string, List<string>>
Run Code Online (Sandbox Code Playgroud)

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

谢谢

小智 6

所以我想知道在C++中是否有这样的东西是可能的

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

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

这取决于您的要求.例如std::vectorvs std::list(有关详细信息,请参见此处)

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

#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;
}
Run Code Online (Sandbox Code Playgroud)


Bad*_*Bad 5

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

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

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

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