Vector

1.string str[]={"Alex","John","Robert"};
// creates vector with 10 elements,
 // and assign value 0 for each 
vector<int> v3(10,0);
vector<string> v4(str+0,str+3); 
vector<string>::iterator sIt = v4.begin(); 
while ( sIt != v4.end() ) 
cout << *sIt++ << " "; 
cout << endl; 
// copy constructor 
vector<string> v5(v4); 
for ( int i=0; i<3; i++)
cout << v5[i] << " "; 
cout << endl; 
return 0; 
} 
OUTPUT: 
// Alex John Robert 
// Alex John Robert




1、string s1( "Mississippi" ); 
string s3;   
// 拷贝s1 的前4 个字符   
s3.assign( s1, 0, 4 );   
s3 现在的值为“Miss”。   
2、 string str1, str2 = "War and Peace";   
str1.assign( str2, 4, 3 );   
cout << str1 << endl;   
显示   and


#include <iostream> 
#include <vector> 
using namespace std; 
int main () 
{ 
vector<int> v(3,0); 
v[0] = 100; 
v.at(1) = 200; 
for ( int i=0; i<3; i++ ) 
cout << v.at(i) << " "; 
cout << endl; 
return 0;
 } 
OUTPUT: // 100 200 0


#include <iostream> 
#include <vector> 
#include <iterator>
 #include <numeric> //iota
using namespace std; 
int main () 
{ 
vector<int> v(5); 
iota(v.begin(),v.end(),1);//stl自增函数 
vector<int>::iterator It = v.begin(); 
while ( It != v.end() )
cout << *It++ << " "; 
cout << endl; 
// third element of the vector 
It = v.begin()+2; 
cout << *It << endl; 
return 0;
} 
OUTPUT: // 1 2 3 4 5 
// 3

  

1.
#include <iostream>
 #include <vector> 
using namespace std; 
int main () 
{ 
vector<int> v(10); 
cout << "Size of v = " << v.size() << endl; 
cout << "Capacity of v = " << v.capacity() << endl; 
v.resize(100); 
cout << "After resizing:" << endl; 
cout << "Size of v = " << v.size() << endl; 
cout << "Capacity of v = " << v.capacity() << endl; 
return 0;
}
 OUTPUT: // Size of v = 10
 // Capacity of v = 10 
// After resizing: 
// Size of v = 100 
// Capacity of v = 100



2.fill(v.begin(),v.end(),5);

v.clear();//清除vector内容


3.#include <iostream> 
#include <vector> 
using namespace std; 
int main () 
{ 
vector<int> v; 
cout << "Vector is "; 
v.empty() ? cout << "" : cout << "not "; 
cout << "empty" << endl; 
//如c++中的vector头文件里面就有这个push_back函数,在vector类中作用为在vector尾

部加入一个数据。
v.push_back(100); 
cout << "Vector is "; 
v.empty() ? cout << "" : cout << "not "; 
cout << "empty" << endl; return 0; 
}
 // Vector is empty 
// Vector is not empty


4.
#include <iostream> 
#include <vector> 
#include <iterator> 
#include <numeric> 
using namespace std; 
int main () 
{ 
vector<int> v(5);
iota(v.begin(),v.end(),1); 
vector<int>::iterator It = v.begin(); 
while ( It != v.end() ) 
cout << *It++ << " "; 
cout << endl; 
// last element of the vector 
It = v.end()-1; 
cout << *It << endl; 
return 0; 
} 
OUTPUT: // 1 2 3 4 5 
// 5

5.
#include <vector>
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
    vector<int> vect;    
    vect.push_back(1);
    vect.push_back(2);
    vect.push_back(3);
    vect.push_back(4);
    vect.resize(100);    //新的空间不覆盖原有四个元素占有的空间,现在size和

capacity都是100
    cout<<vect.size()<<endl;
    int i = 0;
    for (i = 0; i < 104; i++)
    {
        cout<<vect[i]<<endl;  
    }
    return 0;
}

#include <vector>
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
    vector<int> vect;        
    vect.resize(100);    //分配100个空间
        vect.push_back(1);
    vect.push_back(2);
    vect.push_back(3);
    vect.push_back(4);
    cout<<vect.size()<<endl; //现在size和capacity都是104
    int i = 0;
    for (i = 0; i < 104; i++)
    {
        cout<<vect[i]<<endl;  
    }
    return 0;
}


6.
#include <iostream> 
#include <vector> 
using namespace std; 
int main () 
{
 vector<int> v(10); 
cout << "Size of v = " << v.size() << endl; 
cout << "Max_size of v = " << v.max_size() << endl; 
return 0;
 } 
OUTPUT: // Size of v = 10 
// Max_size of v = 1073741823


7.pop_back() 删除最后一个元素

template <class T> 
class Print 
{ public: 
void operator () (T& t) 
{ cout << t << " "; } }; 
//============================= 
int main () 
{ 
vector<int> v; 
Print<int> print; 
for ( int i=0; i<5; i++ )
 v.push_back(i+1); 
while ( !v.empty() ) 
{ 
for_each(v.begin(),v.end(),print);
 cout << endl; 
v.pop_back(); 
} return 0; 
} OUTPUT: // 1 2 3 4 5 
// 1 2 3 4 
// 1 2 3
 // 1 2 
// 1



8.
#include <iostream> 
#include <vector> 
using namespace std; 
int main () 
{ 
vector<int> v(5,0); 
// 5 elements, each - value 0 

/*------------------------------------------------*/ 
cout << "Size of v = " << v.size() << endl; 
cout << "Capacity v = " << v.capacity() << endl;
 cout << "Value of each element is - "; 
for ( int i = 0; i < v.size(); i++ ) 
cout << v[i] << " "; 
cout << endl; 
v[0] = 5; 
// new value for first element 
v[1] = 8; 
v.push_back(3);
 // creates new (6th) element of vector, 
v.push_back(7); // automatically increases size
 cout << endl; // capacity of vector v 
cout << "Size of v = " << v.size() << endl; 
cout << "Capacity v = " << v.capacity() << endl; 
cout << "Value of each element is - "; 
for ( int i = 0; i < v.size(); i++ )
cout << v[i] << " ";
cout << endl << endl; 
v.reserve(100); // increase capacity to 100 
cout << "Size of v1_int = " << v.size() << endl;
 cout << "Capacity v1_int = " << v.capacity() << endl; 
int size = sizeof(v); // how big is vector itself 
cout << "sizeof v = " << size << endl; 
return 0; 
} 

// Size of v = 5 
// Capacity v = 5//返回当前vector在重新进行内存分配以前所能容纳的元素数量。
 // Value of each element is - 0 0 0 0 0 
// 
// Size of v = 7 
// Capacity v = 10 
// Value of each element is - 5 8 0 0 0 3 7 
// 
// Size of v = 7
 // Capacity v = 100
 // sizeof v = 12

9.

#include <iostream> 
#include <vector> 
#include <algorithm> 
#include <iterator> 
using namespace std; 
int main () 
{ 
vector<int> v(5); 
for ( int i=0; i<5; i++ ) 
v[i] = i*2; 
copy(v.begin(),v.end(), ostream_iterator<int>(cout," "));
 cout << endl; 
v.resize(7,100); 
copy(v.begin(),v.end(), ostream_iterator<int>(cout," "));
cout << endl; v.resize(4); 
copy(v.begin(),v.end(), ostream_iterator<int>(cout," "));
 cout << endl; 
return 0; 
}
 OUTPUT: // 0 2 4 6 8 
// 0 2 4 6 8 100 100
 // 0 2 4 6


10.

  

原文地址:https://www.cnblogs.com/hxsyl/p/2623916.html