push_back矢量到另一个向量

Yuc*_*oat 6 c++ stl 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;
}
Run Code Online (Sandbox Code Playgroud)

编译错误

我收到语法错误.

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$ 
Run Code Online (Sandbox Code Playgroud)

小智 8

N.insert(N.end(),M.begin(),M.end());
Run Code Online (Sandbox Code Playgroud)

  • 尽管此代码可能有助于解决问题,但提供有关“为什么”和/或“如何”回答问题的附加上下文将显着提高其长期价值。请[编辑]您的答案以添加一些解释。 (7认同)

Sti*_*sis 5

这条线

N.push_back(vector<int>M);
Run Code Online (Sandbox Code Playgroud)

应该

N.push_back(M);
Run Code Online (Sandbox Code Playgroud)

vector<vector<int> >N,
Run Code Online (Sandbox Code Playgroud)

应该

vector<vector<int> >N;
Run Code Online (Sandbox Code Playgroud)


jua*_*nza 3

你需要

M.push_back(temp);
Run Code Online (Sandbox Code Playgroud)

在 while 循环中,除了@StilesCrisis' 答案中指出的无效语法之外。