Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

push_back a vector into another vector

Tags:

c++

stl

vector

I want to push_back() a vector M into vector 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;
}

Compilation error

I get a syntax error.

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$ 
like image 531
Yucoat Avatar asked Dec 07 '11 07:12

Yucoat


1 Answers

This line

N.push_back(vector<int>M);

should be

N.push_back(M);

Also

vector<vector<int> >N,

should be

vector<vector<int> >N;
like image 132
StilesCrisis Avatar answered Nov 08 '22 08:11

StilesCrisis