String and Vector - C ++ Review (2)

A namespace usage

       Keyword namespace; Usage:

// declare the namespace 
namespace   myNS 
{ 
    int A;
     void F1 ();
     class per {
         public : void F2 ();     
    } 
} 

// function definition namespace 
void myNS :: F1 () {
     **** 
} 

// namespace class achieve 
void myNS per :: :: F2 () {
     **** 
}

Two, string string

       Need to include header <string>

       1. The read and write the string:

1  String S;
 2 cin >> S; // encounter spaces termination 
3 cout << endl << S;

       2. How to read an entire row (write cycle):

1  // cycle read several string specified exit face 
2  String S;
 . 3  the while (CIN >> S) {
 . 4      COUT S << << endl;
 . 5      IF (S == " BYE " ) {
 . 6        BREAK ;
 . 7      }
 . 8  }
 . 9  
10  // getline reader 
. 11  String S;
 12 is  the while (getline (CIN, S)) {
 13 is      COUT S << << endl;
 14      IF (S == " BYE " ) {
 15        BREAK ;
16     }
17 }

       3. The operation of the string

1  // determines whether an empty string is 
2  String S;
 . 3  BOOL Result = s.empty ();
 . 4  
. 5  // get the string length 
. 6  String S;
 . 7  int size = s.size ();
 . 8  
. 9  // obtained character on a position of the index 
10  string S = "Hello World";
 . 11  char C = S [ 2 ];
 12 is  
13 is  // string equality comparison will be exactly equal to the

Three, vector vector

       Is a container class, it can be regarded as a variable length array; need to include header <vector>;

       Creation Method: vector <int> v; specify the type of preservation elements;

                         vector <int> v <size>; // initializes the number of elements of the vector size

                         vector <int> v1 (v); // initialize to use another vector

       Additive element: v.push_back (1); // add an element to the end of the vector;

       Traversing vector elements inside, except for loop, it can also be used iterator iterator (pointer to an array):

. 1 Vector < int > V;
 2 Vector < int > :: = a const_iterator IT v.begin (); // IT is a pointer 
. 3  the while (IT =! V.endl ())
 . 4  {
 . 5      COUT << IT * ++ < <endl; // ++ * higher priority, the first calculation ++ dereference again, thereby obtaining the element pointed to 
6 }

       v.front (); // get the first element in reference to the container;

       v.back (); // reference to the last element of the container obtained;

       *******and many more

 

 

       

 

Guess you like

Origin www.cnblogs.com/jiang-021/p/11519407.html