Home > Back-end >  C novice, want to pour the elements in a vector with adding another vector, why not
C novice, want to pour the elements in a vector with adding another vector, why not

Time:10-14

#include
#include
using namespace std;

Int main ()
{
Vector Vec.
Vec. Push_back (1);
Vec. Push_back (2);
Vec. Push_back (3);
Vector Vec1;
For (unsigned I=vec. The size () - 1; I>=0; I -)
Vec1. Push_back (vec [I]);
return 0;
}

CodePudding user response:

 # include 
#include
using namespace std;

Int main ()
{
Vector Vec.
Vec. Push_back (1);
Vec. Push_back (2);
Vec. Push_back (3);
Vector Vec1;
For (int I=vec. The size () - 1; I>=0; I -)
Vec1. Push_back (vec [I]);
for(int i=0; I & lt; Vec1. The size (); I++)
Cout & lt; return 0;
}


The output 3, 2, 1

Problem, unsigned integer, into a lot of positive 1-0, dead cycle, the program crash

CodePudding user response:

Upstairs said to, either with an int or use iterators

CodePudding user response:

 int main () 
{
Vector Vec.
Vec. Push_back (1);
Vec. Push_back (2);
Vec. Push_back (3);
Reverse (vec) the begin (), vec. The end ());
return 0;
}

Reverse function in the STL algorithms library is used
  • Related