Learning C ++

Source: Internet
Author: User

 

VectorIt is a vector type. It is an object entity with values, so it can be considered as a variable. It can hold many other types of identical entities, such as several integers, so it is called a container.

 

The difference between the vector class and the general Array class is:

 

1. vector supports the concept of "assigning values to existing array elements" and the concept of "inserting additional elements"-that is, a vector array can be stored in.

 

2. It represents a wider range of applications. Its operations (such as equal to, less than operator, size (), and empty ). Some common operations (such as sort (), min (), max (), and find () are provided as independent generic algorithms.

 

 


VectorIt is an object entity that can accommodate many elements of the same type. Therefore, it is also called a container. Like string, vector belongs to a custom data type in STL (Standard Template Library, Standard Template Library) and can be regarded as an enhanced version of an array in a broad sense.

# Include <vector>

. In addition, vector also provides many methods to operate itself.

 



> a ;                                        vector<> a() ;                                    vector<> a(, ) ;                                 vector<> b(a) ;                                     vector<> b(a.begin(), a.begin()+) ;        


 n[] = {, , , , <> a(n, n+) ;                      vector<> a(&n[], &n[]) ;        

 





Cin>Enter, cout <a [n]Output as follows:

     #include<iostream>     #include<vector>                       vector<> a(, ) ;                         cin >>a[         cin >>a[         cin >>a[                            (i=; i<a.size(); i++             cout<<a[i]<<                 }


. In vector <int> B (a. begin (), a. begin () + 3 );In this declaration form, (a. begin (), a. begin () + 3)It indicates the element position between the starting element position of the vector and the starting element + 3. (A. begin (), a. end () indicates the position of the element other than the starting element and the last element.
At the same time, the traversal type in the vector is: vector <int >:: iterator. The traversal not only indicates the element position, but also can move the elements before and after the elements in the container.

    vector<>(t=a.begin(); t!=a.end(); t++<<*t<<  ;


* TFor indirect access, that is, access t points

 


>.A. size ()                 
>.A. empty ()
>.A. clear ()
4> CopyA= B;
5>ComparisonPersistenceA= B;
6>.Insert-Insert①. A. insert (a. begin (),1000 );A. insert (a. begin (),);<> A (, <> B (
7>. Delete-Erase① B. erase (B. begin ());② B. erase (B. begin (), B. begin () + 3 );
.Exchange-SwapB. swap ();

 

(B. begin (), B. begin () + 3); // 0 ~ of the B vector ~ 2 element composition vector assigned to a (); // make a vector contain only 0 ~ 3 element with a value of 2 (); // assign the value of the last vector element of a to the integer variable x (); // clear (no element exists) () cout <"empty"; //. empty () is often used as a condition to determine whether the vector is empty (); // assign the first element value of a to the integer variable y (); // Delete the last element (5) of vector a; // insert an element at the end of vector a with a value of 5 (10 ); // adjust the number of vector elements to 10. If multiple elements exist, delete them. If few elements exist, add them. The values are random (). // adjust the number of vector elements to 10. If there are multiple, delete them. If there are few, add them. The value is 2.

 

 

Here is a piece of code that operates the four methods for assigning values to a vector.

#include <iostream><vector>   main(<> a();    vector<> b(,);    vector<> c(b);     vector<> d(b.begin(),b.begin() + );<<  ( i=;i<a.size();i++<< a[i] <<   ; << <<  (vector<>::iterator it = a.begin();it!=a.end();++<< *it <<   ;    cout << << +);      (vector<>::iterator it = a.begin();it!=a.end();++<< *it <<   << << ,);      (vector<>::iterator it = a.begin();it!=a.end();++<< *it <<   << <<  x = a.back();    cout <<  << x <<<<  y = a.front();     cout <<  << y <<<<  (a.empty()) cout <<  <<<< );      (vector<>::iterator it = a.begin();it!=a.end();++<< *it <<   << << ,);      (vector<>::iterator it = a.begin();it!=a.end();++<< *it <<   << << );     (vector<>::iterator it = a.begin();it!=a.end();++<< *it <<   << <<      (vector<>::iterator it = a.begin();it!=a.end();++<< *it <<   << <<  (a==b)         cout << <<  

 



> > b(, vector<>());        

 


Example:

     #include<iostream>     #include<vector>                       vector< vector<> > b(, vector<>(,                    cin>>b[][         cin>>b[][         cin>>b[][                            (m=; m<b.size(); m++)                         (n=; n<b[m].size(); n++)                     cout<<b[m][n]<<              cout<<                 }

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.