Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ append vector into another vector [duplicate]

Tags:

c++

c++11

I am trying to append one vector to another vector, both vectors being same in "dimension".

int main()
{
std::vector<int> v1={1,2,3,4,5},v2={10,11,12};
//v1.push_back(v2)?
//v1 and v2 have same dimensions

}

Without creating loops and pushing back individual element, is there any way to achieve similar to this python statements?

v1=[1,2,3,4,5]
v2=[10,11,12]
v1.extend(v2)
print(v1)

gives [1, 2, 3, 4, 5, 10, 11, 12]

like image 208
er344tre Avatar asked Jul 20 '26 01:07

er344tre


1 Answers

v1.insert(v1.end(), v2.begin(), v2.end());

like image 73
Dhruv Sehgal Avatar answered Jul 21 '26 15:07

Dhruv Sehgal