lin*_*bin 4 c++ arrays stl vector
#include <vector>
using namespace std;
vector<int[60]> v;
int s[60];
v.push_back(s);
Run Code Online (Sandbox Code Playgroud)
Visual Studio 2015社区中的此代码报告编译错误:
错误(活动)没有重载函数的实例"std :: vector <_Ty,_Alloc> :: push_back [with _Ty = int [60],_ Alloc = std :: allocator]"匹配参数列表
错误C2664'void std :: vector> :: push_back(const int(&)[60])':无法将参数1从'int'转换为'int(&&)[60]'
Sam*_*hik 13
请std::array改用:
#include <vector>
#include <array>
using namespace std;
int main()
{
vector<array<int, 10>> v;
array<int, 10> s;
v.push_back(s);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但我还要质疑包含数组的向量的目的.无论是什么原因,都可能有更好的方法来实现相同的目标.
你可以这样做:
#include <iostream>
#include <vector>
int main()
{
int t[10] = {1,2,3,4,5,6,7,8,9,10};
std::vector<int*> v;
v.push_back(t);
std::cout << v[0][4] << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
更具体地说,在这个解决方案中,你实际上并没有将数组t的值存储到向量v中,只是存储指向数组的指针(并且更具体地说是数组的第一个元素)
| 归档时间: |
|
| 查看次数: |
2837 次 |
| 最近记录: |