gpt4 book ai didi

c++ - 是否可以保留在容器静态数组中?

转载 作者:行者123 更新时间:2023-11-28 03:42:10 25 4
gpt4 key购买 nike

我的意思是不在数组上保留指针

std::vector<int*> vector;

但是

std::vector<int[]> vector;

问题是将这样的数组保存在 hash_map 中,以便在插入时比较指针而不是指针,但当我这样尝试时

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

我有一个错误。

最佳答案

你做不到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,所以这可能更可取)

关于c++ - 是否可以保留在容器静态数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8796269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com