c++ - 如何 push_back() C 数组到 std::vector
问题描述
这不编译:
vector<int[2]> v;
int p[2] = {1, 2};
v.push_back(p); //< compile error here
有什么选择?我不想使用std::array
.
自己的std::vector
声明编译。这是push_back
不编译的。
解决方案
如果您真的不想使用,可以使用包含数组的结构或包含两个整数的结构std::array
:
struct coords {
int x, y;
};
vector<coords> v;
coords c = {1, 2};
v.push_back(c);
或者,如前所述,您可以使用包含数组的结构:
struct coords {
int x[2];
};
vector<coords> v;
coords c = {1, 2};
v.push_back(c);
推荐阅读
- c# - UploadFromStreamAsync 到 Azure 存储的静默失败
- leaflet - LeafletJS 如何适应 ViewPort 中的每一层?
- spring - Spring-boot Rest :使用 ControllerAdvice 但保留默认处理程序
- flutter - 打印到控制台列表类型不起作用
- python - Holoviews 中的热图?
- wordpress - 在 Wordpress (.htaccess) 中剪切部分网址
- xcode - Flutter Firebase iOS Google 登录错误
- c - 为什么这段代码不接受字符输入?
- typescript - 在打字稿中用星号替换字母
- architecture - NetWeight 和 GrossWeight 作为单独的值对象