how to append one vector to another c++
vector<int> a;
vector<int> b;
// Appending the integers of b to the end of a
a.insert(a.end(), b.begin(), b.end());
how to append one vector to another c++
vector<int> a;
vector<int> b;
// Appending the integers of b to the end of a
a.insert(a.end(), b.begin(), b.end());
vector insert
// inserting into a vector
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> myvector (3,100);
std::vector<int>::iterator it;
it = myvector.begin();
it = myvector.insert ( it , 200 );
myvector.insert (it,2,300);
// "it" no longer valid, get a new one:
it = myvector.begin();
std::vector<int> anothervector (2,400);
myvector.insert (it+2,anothervector.begin(),anothervector.end());
int myarray [] = { 501,502,503 };
myvector.insert (myvector.begin(), myarray, myarray+3);
std::cout << "myvector contains:";
for (it=myvector.begin(); it<myvector.end(); it++)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
insert vector
#include <iostream>
#include <vector>
void print_vec(const std::vector<int>& vec)
{
for (auto x: vec) {
std::cout << ' ' << x;
}
std::cout << '\n';
}
int main ()
{
std::vector<int> vec(3,100);
print_vec(vec);
auto it = vec.begin();
it = vec.insert(it, 200);
print_vec(vec);
vec.insert(it,2,300);
print_vec(vec);
// "it" no longer valid, get a new one:
it = vec.begin();
std::vector<int> vec2(2,400);
vec.insert(it+2, vec2.begin(), vec2.end());
print_vec(vec);
int arr[] = { 501,502,503 };
vec.insert(vec.begin(), arr, arr+3);
print_vec(vec);
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us