How to insert the second vector before the first? push_front not working!

 #include <iostream> #include <vector> #include <conio.h> using namespace std; int main(int argc, char* argv[]) { int k = 1; int s = 0; int a[50]; vector<int>v1(5, 1); v1.clear(); v1.push_back(1); v1.push_back(3); v1.push_back(5); v1.push_back(4); v1.push_back(6); vector<int>v2(5, 1); v2.clear(); v2.push_back(7); v2.push_back(6); v2.push_back(0); v2.push_back(0); v2.push_back(9); cout << "Ishodnii Vector" << endl; cout << "Razmer v1: " << v1.size() << endl; for (int i = 0; i<v1.size(); i++) cout << " v1[" << i << "]=" << v1[i]; for (int i = 0; i <v1.size(); i++) s += v1[i]; cout << endl; for (int i = 0; i <v1.size(); i++) if (v1[i] == 1) v2.push_back(i); for (int i = 0; i<v1.size(); i++) { v2.push_back(i); k++; } cout << "\nRezultat Zadania" << endl; cout << "Razmer v1: " << v1.size() << endl; for (int i = 0; i<v1.size(); i++) cout << " v1[" << i << "]=" << v1[i]; cout << endl; getchar(); } 

Closed due to the fact that off-topic by user194374, cheops , zRrr , αλεχολυτ , aleksandr barakin Jun 20 '16 at 7:29 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - community spirit, cheops, zRrr, αλεχολυτ, aleksandr barakin
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • What does "not working" mean? - VladD
  • And yes, try insert . - VladD
  • if you need to often insert in the middle, then maybe you should not have chosen a vector or is it better to add 1 to the end 2? - pavel
  • one
    @ Kirill21 thanks, it works !! - Holy Guacamole

1 answer 1

Solution from Kirill21 comment

https://ideone.com/rQEhPs

 #include <iostream> #include <vector> using namespace std; int main() { vector<int> v1{1,3,5,4,6}; vector<int> v2{7,6,0,0,9}; for(int i=0, k=v2.size()-1 ;i<v2.size();++i) { v1.insert(v1.begin(),v2[k--]); } for(int i = 0; i< v1.size();++i) { std::cout<<v1[i]<<endl; } return 0; }