In-depth analysis of C ++ Vector usage

Source: Internet
Author: User

In C ++ programming language, there is an application method called Vector. Its role is very important in actual programming. Here we will give you a detailed introduction to the application skills and basic content of C ++ Vector, hoping to help you.

  • Overview of C ++ header file content
  • C ++ pointer drift Solution
  • How to use C ++ Builder to Operate Excel tables
  • Tips for deleting C ++ container values
  • Overview of basic concepts of C ++ set Initialization

(1) vector <type> identifier;

(2) vector <type> identifier (maximum capacity );

(3) vector <type> maximum identifier capacity, all initial values );

(4) int I [4] = {12, 3, 4, 5 };

 
 
  1. Vector <type> vi (I, I + 2); // obtain the value after I index value is 3;

(5) vector <int> // vi defines a 2-dimensional container. Remember to have spaces. Otherwise, an error will be reported.

 
 
  1. Vector <int> line
  2. // When using the tool, you must first initialize the vi rows;
  3. For (int I = 0; I <10; I ++)
  4. {
  5. Vector. push_back (line );
  6. }
  7. /// I personally think it is good to define a two-dimensional array using vector,
    Because the length is not predetermined. Good.

(6) C ++ Vector sorting

 
 
  1. Vector <int> vi;
  2. Vi. push_back (1 );
  3. Vi. push_back (3 );
  4. Vi. push_back (0 );
  5. Sort (vi. begin (), vi. end (); // small to large
  6. Reverse (vi. begin (), vi. end () // small from Avenue

(7) sequential access

 
 
  1. Vector <int> vi;
  2. For (int I = 0; I <10; I ++)
  3. {
  4. Vector. push_back (I );
  5. }
  6. For (int I = 0; I <10; I ++) // The first call method.
  7. {
  8. Cout <vector [I] <"";
  9. }
  10. For (vector <int >:: iterator it = vi. begin ();
  11. It! = Vi. end (); it ++) // The second call Method
  12. {
  13. Cout <* it <"";
  14. }

(8) Search

 
 
  1. Vector <int> vi;
  2. For (int I = 0; I <10; I ++)
  3. {
  4. Vector. push_back (I );
  5. }
  6. Vector <int>: interator it = find (vi. begin (), vi. end, 3 );
  7. Cout <* it <endl; // return the location of the value in the container.

(9) Use arrays to initialize the C ++ Vector.

 
 
  1. Int I [10] = {1, 2, 4, 5, 6, 7, 78, 8 };
  2. /// First
  3. Vector <int> vi (I + 1, I + 3); // from the first element to the third element
  4. For (vector <int >:: interator it = vi. begin ();
  5. It! = Vi. end (); it ++)
  6. {
  7. Cout <* it <"";
  8. }

(10) struct type

 
 
  1. struct temp  
  2. {  
  3. public :  
  4. string str ;   
  5. public :  
  6. int id ;  
  7. }tmp  
  8. int main()  
  9. {  
  10. vector <temp> t ;   
  11. temp w1 ;   
  12. w1.str = "Hellowor" ;  
  13. w1.id = 1 ;   
  14. t.push_back(t1);  
  15. cout << w1.str << "," <<w1.id <<endl ;   
  16. return 0 ;   

The basic introduction of C ++ Vector is here.

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.