push_back一个向量到另一个向量中
push_back a vector into another vector
我想push_back()
向量M
到向量N
中。
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int i = -1;
vector<vector<int> >N,
vector<int>M;
int temp;
while (i++ != 5)
{
cin >> temp;
N.push_back(temp);
}
N.push_back(vector<int>M);
return 0;
}
编译错误
我收到语法错误。
test.cpp: In function ‘int main()’:
test.cpp:28: error: invalid declarator before ‘M’
test.cpp:34: error: no matching function for call to ‘std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >::push_back(int&)’
/usr/include/c++/4.4/bits/stl_vector.h:733: note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = std::vector<int, std::allocator<int> >, _Alloc = std::allocator<std::vector<int, std::allocator<int> > >]
test.cpp:37: error: ‘M’ was not declared in this scope
coat@thlgood:~/Algorithm$
N.insert(N.end(),M.begin(),M.end());
此行
N.push_back(vector<int>M);
应该是
N.push_back(M);
也
vector<vector<int> >N,
应该是
vector<vector<int> >N;
你需要
M.push_back(temp);
在 while 循环中,除了 @StilesCrisis 答案中指出的无效语法。
你有几个小错误。
您可以通过查看您得到的每个编译错误并思考它的含义来解决它们。如果错误不清楚,您可以查看错误的行号,并思考可能导致它的原因。
请参阅工作代码:
int main()
{
int i = -1;
vector<vector<int> >N;
vector<int>M;
int temp;
while (i++ != 5)
{
cin >> temp;
M.push_back(temp);
}
N.push_back(M);
return 0;
}
相关文章:
- 函数向量_指针有不同的原型,我可以构建一个吗
- C++从另一个类访问公共静态向量的正确方法是什么
- 为什么我不能将一个对象push_back到属于另一个类的对象向量中?
- 给定一个向量,如何找到该向量的所有子集和的原始索引
- 获取向量C++中第一个值和最后一个值的和
- 从多个源构造一个对象,包括一个对象向量
- 为什么一个向量上的多线程操作很慢
- 基于范围的 for 循环:迭代使用一个元素扩展的向量
- C++ - 如何在结构向量中找到结构体一个成员的最大值?
- 如何在 C++11 中查找和更新向量中的一个嵌套结构
- Lower_bound不适用于具有 3 个元素的向量的最后一个元素
- 将一个向量插入另一个向量的某个位置
- std::find,返回所有找到的值的替代方法,而不仅仅是存在重复的向量的第一个值
- 将指向给定子类的指针从一个向量复制到另一个向量
- 如何创建一个类,以便向量工作 std::vector<MyClass<int>> v{ 1,2,3 };
- C++ STD 函数运算符:有没有一种方法可以通过函数将一个向量映射到另一个向量上?
- 将向量之间的数字放在另一个向量之间<vector>>如果两个数字的差值为 1
- 如何在不复制的情况下将一个向量移动到另一个向量中
- 创建一个函数的 Python 绑定,返回指向带有 boost 的向量的指针
- 紧凑向量一个小的内存足迹向量