C ++ getting started -- vector of the standard template library

Source: Internet
Author: User

C ++ getting started -- vector of the standard template library

Vector is a very useful container in C ++. Vector is considered a container because it can store various types of objects like a container. Simply put, vector can be of any type (int, double, string, can also be a dynamic array of classes), can add and compress data.

 

Before using vector, you must include the corresponding header file. The vector belongs to the std naming field. Therefore, you must pass the naming restrictions:

# Include
Using std: vector; // using namespace std;

 

Definition and initialization of a vector object

Vector
 
  
A; // create an int-type empty vector object without any element vector
  
   
B (a); // B is the copy of a vector.
   
    
Str (10, mike); // 10 str, each containing mikevector
    
     
C (10); // create 10 int-type vector objects, equivalent to c [10]. The default value of each element is 0.
    
   
  
 

 

Common Operations on vector objects

 

Example:

 

# Include
 
  
# Include
  
   
Using std: vector; using std: cout; using std: endl; // using namespace std; int main () {vector
   
    
A (10); // create 10 int-type vector objects, equivalent to c [10], the value of each element is 0 // determines whether a is null if (true =. empty () {cout <it is empty;} else if (false =. empty () {// The result is not empty. cout <it is not empty;} // returns the number of elements in. Cout <size = <a. size () <endl; // The result is 10 // Add an element with a value of 5 at the end of. // There are 10 elements, and one is added, which becomes 11 a. push_back (5); // The subscript operation element has 11 elements, 0 ~ 10 // print the last element cout <a [. size ()-1] <endl; // The result is 5 // subscript, print the values of all elements // vector
    
     
: Size_type can be used as int to use for (vector
     
      
: Size_type I = 0; I! =. Size (); I ++) {cout <a [I] <, ;}cout <endl; cout <size before pop_back = <. size () <endl;. pop_back (); // Delete the cout element at the last position of the container <size after pop_back = <. size () <endl; // subscript method, assign values to all elements for (vector
      
        : Size_type I = 0; I! =. Size (); I ++) {a [I] = 5 ;}cout <it is a:; // print the values of all elements of a for (vector
       
         : Size_type I = 0; I! = A. size (); I ++) {cout <a. at (I) <, ;} cout <endl; vector
        
          B = a; // B is a replica of a. cout <it is B:; // print the values of all elements of B out for (vector
         
           : Size_type I = 0; I! =. Size (); I ++) {cout <B [I] <, ;}cout <endl;. clear (); // clear all elements cout <size after clear = <. size () <endl; return 0 ;}
         
        
       
      
     
    
   
  
 

 

The running result is as follows:

 

Iterator operation vector object

In addition to using subscript to access elements of a vector object, the standard library also provides another method to access elements: Using iterator ). An iterator is a data type that checks elements in a container and traverses elements. Actually,A vector object is equivalent to an array, and an iterator is equivalent to a pointer..

 

Each container type defines its own iterator type, such as vector:

Vector : Iterator iter;

 

 

Example:

 

# Include
 
  
# Include
  
   
Using std: vector; using std: cout; using std: endl; // using namespace std; int main () {vector
   
    
V (10, 5); // there are 10 elements, and the value of each element is 5 // print the values of all elements through the iterator. vector
    
     
: Iterator it; for (it = v. begin (); it! = V. end (); it ++) {cout <* it <, ;}cout <endl; // assign 1for (it = v. begin (); it! = V. end (); it ++) {* it = 1;} cout <endl; // print the values of all elements through the iterator for (it = v. begin (); it! = V. end (); it ++) {cout <* it <, ;}cout <endl; it = v. begin (); // returns the pointer (iterator) pointing to the element at the beginning of the container. // deletes the element pointing to the position of the it + 1 pointer, returns the pointer (iterator) v pointing to the next element. erase (it + 1); cout <endl <after erase:; // print the values of all elements through the iterator for (it = v. begin (); it! = V. end (); it ++) {cout <* it <, ;}cout <endl; it = v. begin (); // insert 3 5 v values after it. insert (it, 3, 5); cout <endl <after insert:; // print the values of all elements through the iterator for (it = v. begin (); it! = V. end (); it ++) {cout <* it <, ;}cout <endl; return 0 ;}
    
   
  
 

The running result is as follows:

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.