是否可以保留容器静态数组?

时间:2012-01-09 22:47:13

标签: c++

我的意思是不要将指针放在数组上

std::vector<int*> vector;

std::vector<int[]> vector;

问题是在hash_map中保留这样的数组,以便在插入那里时比较非指针但是当我尝试这样时

std::hash_map<std::vector<BYTE>,std::string> xxx

我遇到了错误。

1 个答案:

答案 0 :(得分:2)

你不能std::vector<int[]> vector。您必须指定数组的大小才能进行编译,如std::vector<int[5]> vector

然而,这是一个坏主意,因为你不能将数组分配给其他数组等,当你尝试使用vector时,你会得到各种错误。

相反,使用vector<vector<int>> vector或在C ++ 11中使用vector<std::array<int, 5>> vector

另外,我不知道您使用的是hash_map的实现方式,因此我不知道上述解决方案是否适用于您的情况。 (另外,C ++ 11有unordered_map,因此可能更合适)