1、empty——判断容器内元素是否为空
#include <iostream> #include <vector> using namespace std; int main () { vector<int> v; cout << "Vector is "; v.empty() ? cout << "" : cout << "not "; cout << "empty" << endl; v.push_back(100); cout << "Vector is "; v.empty() ? cout << "" : cout << "not "; cout << "empty" << endl; return 0; } 运行结果: // Vector is empty // Vector is not empty
2、end——容器迭代器最后一个元素的下一个迭代
#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; // vector容器的最后一个元素 It = v.end()-1; cout << *It << endl; return 0; } 运行结果: // 1 2 3 4 5 // 5
3、erase——从容器中删除某个元素或者某个区间
#include <iostream> #include <vector> #include <iterator> #include <algorithm> using namespace std; int main () { vector<int> v(10); vector<int>::iterator It; for ( int i=0; i<10; i++ ) v[i] = i+1; copy(v.begin(),v.end(), ostream_iterator<int>(cout," ")); cout << endl; It = v.begin()+2; // 删除第三个元素 v.erase(It); copy(v.begin(),v.end(), ostream_iterator<int>(cout," ")); cout << endl; It = v.begin(); // 从v容器中删除前两个元素 v.erase(It,It+2); copy(v.begin(),v.end(), ostream_iterator<int>(cout," ")); cout << endl; return 0; } 运行结果: // 1 2 3 4 5 6 7 8 9 10 // 1 2 4 5 6 7 8 9 10 // 4 5 6 7 8 9 10
4、front——返回容器的第一个元素
#include <iostream> #include <vector> #include <string> #include <iterator> using namespace std; template<class T, class D> class Member { public: Member(T t, D d) : name(t), sal(d) {} void print(); private: T name; D sal; }; template<class T, class D> void Member::print() { cout << name << " " << sal << endl; } //====================================== int main () { typedef Member<string,double> M; vector<M> v; v.push_back(M("Linda",75000)); v.push_back(M("Robert",60000)); vector<M>::iterator It = v.begin(); cout << "Entire vector:" << endl; while ( It != v.end() ) (It++)->print(); cout << endl; cout << "Return from front()" << endl; v.front().print(); return 0; } 运行结果: // Entire vector: // Linda 75000 // Robert 60000 // // Return from front() // Linda 75000
5、insert——想容器中插入一个、多个或者一组元素
#include <iostream> #include <vector> #include <iterator> #include <algorithm> using namespace std; template <class T> class Print { public: void operator () (T& t) { cout << t << " "; } }; //============================= int main () { int ary[5]; fill(ary,ary+5,1); vector<int> v; vector<int>::iterator It; Print<int> print; copy(ary,ary+5, back_inserter(v)); cout << "vector v : "; for_each(v.begin(),v.end(),print); cout << endl; It = v.begin(); // 将5插入容器的It位置 cout << "v.insert(It,5) : "; v.insert(It,5); for_each(v.begin(),v.end(),print); cout << endl; // 将区间【ary+2 - ary+5】插入位置"It" It = v.begin()+5; cout << "v.insert(It,ary+2,ary+5 : "; v.insert(It,ary+2,ary+5); for_each(v.begin(),v.end(),print); cout << endl; // 将2个20插入"It"位置 It = v.end()-2; cout << "v.insert(It,2,20) : "; v.insert(It,2,20); for_each(v.begin(),v.end(),print); cout << endl; return 0; } 运行结果: // vector v : 1 1 1 1 1 // v.insert(It,5) : 5 1 1 1 1 1 // v.insert(It,ary+2,ary+5 : 5 1 1 1 1 1 1 1 1 // v.insert(It,2,20) : 5 1 1 1 1 1 1 20 20 1
6、max_size——容器能申请到的最大容量
#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; } 运行结果: // Size of v = 10 // Max_size of v = 1073741823
7、pop_back——让容器的最后一个元素出栈
#include <iostream> #include <vector> #include <algorithm> using namespace std; 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; } 运行结果: // 1 2 3 4 5 // 1 2 3 4 // 1 2 3 // 1 2 // 1
除非注明,文章均为CppLive 编程在线原创,转载请注明出处,谢谢。