C + + vector usage deep analysis of _c language

Source: Internet
Author: User

The C + + programming language has an application called vector, and its role is very important in practical programming. Here we will give you a detailed introduction of C + + vector related to the application skills and basic content, I hope to bring some help.

(1) vector< type > identifier;
(2) vector< type > identifier (maximum capacity);
(3) vector< type > identifier (maximum capacity, initial all values);
(4) int i[4] = {12,3,4,5};
vector< type > VI (i, i+2); The value of I index is 3;
(5) vector< vector<int> >//vi defines a 2-dimensional container; Remember to have a space, or it will be an error

vector< int >  
Line//When using, you must first initialize the vi row;  
for (int i = 0; i < i + +) 
{ 
vector.push_back (line); 
} 
The individual believes that it is good to define a two-dimensional array using vectors because the length can not be predetermined. Very good. 

(6) C + + vector sort

vector< int > VI;  
Vi.push_back (1); 
Vi.push_back (3); 
Vi.push_back (0); 
Sort (Vi.begin (), Vi.end ()); /Small to large 
reverse (vi.begin (), Vi.end ())///from the Boulevard small 

(7) Sequential access

Vector < int > vi;  
for (int i = 0; i < i + +) 
{ 
vector.push_back (i); 
}  
for (int i = 0; i < i + +)///The first invocation method 
{ 
cout <<vector[i] << "";  
} 
for (Vector<int>::iterator it = Vi.begin (); 
it!=vi.end (); it++)///The second invocation method 
{ 
cout << *it << ""; 
} 

(8) Find

Vector < int > vi;  
for (int i = 0; i < i + +) 
{ 
vector.push_back (i); 
}  
Vector < int >::interator it = find (Vi.begin (), vi.end,3); 
cout << *it << Endl; Returns the location of the value found within the container. 

(9) using arrays to initialize C + + vector

int i[10] ={1,2,3,4,5,6,7,78,8}; 
The first type of  
vector<int> VI (I+1,I+3);///from the 2nd element to the third element for 
(vector <int>::interator it = vi.begin (); 
It!= vi.end (); it++) 
{ 
cout << *it << "";  
} 

(10) Type of structural body

struct temp 
{public 
: 
string str;  
Public: 
int id; 
} TMP 
int main () 
{ 
vector <temp> t;  
Temp W1;  
W1.str = "Hellowor"; 
W1.id = 1;  
T.push_back (t1); 
cout << w1.str << "," <<w1.id <<endl;  
return 0;  
} 

The basic introduction of vector C + + is introduced 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.