的std ::数组的矢量 [英] std::vector of an array

查看:151
本文介绍了的std ::数组的矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的声明通过编译检查:

Following declaration passes compilation check:

int arr[3];
vector<int[3]> vec;  // ok !!

当尝试使用 VEC 几乎为,

vec.push_back(arr);

这会导致很多错误,如:

it results in many error like:

/usr/include/c++/4.6/ext/new_allocator.h:108:9: error: ISO C++ forbids initialization in array new [-fpermissive]
/usr/include/c++/4.6/bits/vector.tcc:314:4: error: invalid array assignment 
/usr/include/c++/4.6/ext/new_allocator.h:118:30: error: request for member ‘~int [3]’ in ‘* __p’, which is of non-class type ‘int [3]’

此外, VEC 的push_back()为int * 也。

到底出了什么吗?在C ++ 11正在解决这样的问题?

What exactly goes wrong here ? Is such issue being addressed in C++11 ?

推荐答案

您不能存储阵列的容器,因为它们既不分配,也不拷贝的,这是为与标准库容器中使用的所有对象的要求。

You can't store arrays in containers because they are neither assignable nor copyable, which are the requirements for all objects that are used with Standard Library containers.

赋值运算符是唯一的尝试,当你做的push_back()这就是为什么你的code 编译没有它

The assignment operator is only attempted when you do push_back() which is why your code compiles without it.

这篇关于的std ::数组的矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆