One-dimensional vector initialization

vector detailed summary

Need to introduce headers #include <the Vector> before you can use

Initialize a one-dimensional vector

. 1 Vector < int > V; // defaults to an empty 
2 Vector < int > V1 (V); // Use the initialization vector v V1 
. 3 Vector < int > V2 (v.begin (), v.end ()); / / use v interval [begin, end] initialization v1, customizable interval 
. 4 Vector < int > V3 (v.begin () + . 1 , v.begin () + . 3 ); // with the interval [begin + 1, begin + 3) to the second third initialization V5 
. 5 Vector < int > V4 ( 15 ); // Vector initialized with the value n = 15 elements 0 
. 6 Vector < int > V5 ( 15 , 10 );// Initialization n = 15 elements and the value of m = vector 10

 

closing the left and right vector v.end open interval () no value; V3 (v.begin () +. 1, v.begin () +. 3) is initialized when not reach b.begin () + 3, belonging to the right to open interval [1,3) comprises two data 1,2

Example:

Code:

. 1 #include <the iostream>
 2 #include <Vector>
 . 3  the using  namespace STD;
 . 4  void Show (Vector < int > & V) {
 . 5      for ( int I = 0 ; I <( int ) v.size (); I ++ ) {
 . 6          COUT << V [I] << "  " ;
 . 7      }
 . 8      COUT << endl;
 . 9  }
 10  int main ()
 . 11  {
 12 is      // Vector initialization requires include headers #include <vector> can use 
13     Vector < int > V; // defaults to an empty 
14      int Data, n-;
 15      CIN n->>; // Representative number 
16      for ( int I = 0 ; I <n-; I ++ ) {
 . 17          // CIN >> V [I]; // wrong, because no initialization vector v of size, can not use the V [I] 
18 is          CIN >> Data;
 . 19          v.push_back (Data);
 20 is      }
 21 is      Vector < int > V1 (V); // use initialization vector v V1 
22 is      Vector < int > V2 (v.begin (), v.end ()); //Interval using v [begin, end] initialization v1, customizable section 
23 is      Vector < int > V3 (v.begin () + . 1 , v.begin () + . 3 ); // with the interval [begin + 1, begin + 3) the second to the third initialization V5 
24      Vector < int > V4 ( 15 ); // Vector initializing n = 15 elements with the value 0 
25      Vector < int > V5 ( 15 , 10 ); // initializing n = 15 elements and the value of m = vector 10 of 
26 is      Show (V);
 27      Show (V2);
 28      Show (V3);
 29      Show (V4);
 30      Show (V5);
 31 is      return  0;
32 }

 

Input:

10 1 2 3 4 5 6 7 8 9 10

Export

 

Guess you like

Origin www.cnblogs.com/NirobertEinteson/p/11960258.html