In depth analysis of C++ Vector usage
- 2020-04-02 02:34:13
- OfStack
There is an application method called Vector in C++ programming language, which is very important in actual programming. Here we will give you a detailed introduction of C++ Vector related application skills and basic content, I hope to give you some help.
(1) the vector
<
type
>
Identifier;
(2) the vector
<
type
>
Identifier (maximum capacity);
(3) the vector
<
type
>
Identifier (maximum capacity, all initial values);
(4) int I [4] = {12,3,4,5};
The vector
<
type
>
Vi (I, I + 2); // get I index value after 3;
(5) the vector
<
The vector
<
int
>
>
//vi defines a 2-dimensional container; Remember to have Spaces, or you will report an error
vector< int > line
//Be sure to initialize vi rows first when using;
for(int i = 0 ; i < 10 ; i ++)
{
vector.push_back(line);
}
//I think it is good to use vector to define two-dimensional array, because the length can not be determined in advance. Very good.
(6) c + + the Vector sequence
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 avenue is small
(7) sequential access
vector < int > vi ;
for( int i = 0 ; i < 10 ; i ++)
{
vector.push_back(i);
}
for(int i = 0 ; i < 10 ; i ++) /// the first method is called
{
cout <<vector[i] <<" " ;
}
for(vector<int>::iterator it = vi.begin() ;
it !=vi.end() ; it++) /// the second method is called
{
cout << *it << " " ;
}
(8)
vector < int > vi ;
for( int i = 0 ; i < 10 ; i ++)
{
vector.push_back(i);
}
vector < int >::interator it = find(vi.begin() , vi.end,3) ;
cout << *it << endl ; /// returns the location within the container where the value was found.
(9) use array to initialize C++ Vector
int i[10] ={1,2,3,4,5,6,7,78,8} ;
/// the first
vector<int> vi(i+1,i+3); /// from the second element to the third element
for(vector <int>::interator it = vi.begin() ;
it != vi.end() ; it++)
{
cout << *it <<" " ;
}
(10) structure type
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 ;
}
So much for the basic introduction of C++ Vector.