1、push_back——将某个元素从容器后端入栈
#include <iostream> #include <vector> #include <string> #include <iterator> using namespace std; template <class T> class Name { public: Name(T t) : name(t) {} void print() { cout << name << " "; } private: T name; }; //============================= int main () { typedef Name<string> N; typedef vector<N> V; V v; N n1("Robert"); N n2("Alex"); v.push_back(n1); v.push_back(n2); v.push_back(N("Linda")); V::iterator It = v.begin(); while ( It != v.end() ) (It++)->print(); cout << endl; return 0; } 运行结果: // Robert Alex Linda
2、rbegin、rend——容器逆序输出的头和尾
#include <iostream> #include <iomanip> #include <vector> #include <string> #include <algorithm> #include <iterator> using namespace std; class ID { friend bool operator < ( const ID&, const ID& ); public: ID(string name,int score) : name(name), score(score) {} void display () { cout.setf(ios::left); cout << setw(3) << score << name << endl; } private: string name; int score; }; //----------------------------------------------------- // 比较排序函数 bool operator < ( const ID& a, const ID& b ) { return a.score < b.score; } //----------------------------------------------------- typedef vector<ID> Vector; // 为已存在的类型重新定义一个名字 int main () { Vector v; Vector::iterator Iter; v.push_back(ID("Smith A",96)); v.push_back(ID("Amstrong B.",91)); v.push_back(ID("Watson D.",82)); for ( Iter = v.begin(); Iter != v.end(); Iter++ ) Iter->display(); sort(v.begin(),v.end()); // 排序算法 cout << endl << "Sorted by Score" << endl; cout << "===============" << endl; for ( Iter = v.begin(); Iter != v.end(); Iter++ ) Iter->display(); cout << endl << "Reverse output" << endl; cout << "===============" << endl; Vector::reverse_iterator r = v.rbegin(); while ( r != v.rend() ) cout << r->display(); cout << endl; return 0; } 运行结果: // 96 Smith A. // 91 Amstrong B. // 82 Watson D. // // Sorted by Score // =============== // 82 Watson D. // 91 Amstrong B. // 96 Smith A. // // Reverse output // =============== // 96 Smith A. // 91 Amstrong B. // 82 Watson D.
3、reserve——扩充容器的容量
#include <iostream> #include <vector> using namespace std; int main () { vector<int> v(5,0); // 5个元素并初始化为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; // 改变第一个元素的值 v[1] = 8; v.push_back(3); // 为容器新建第六个元素 v.push_back(7); // 为容器新建第七个元素 cout << endl; // 容器满了以后,capacity自动增加为原来的两倍 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); // 将容器容量capacity增加到100 cout << "Size of v1_int = " << v.size() << endl; cout << "Capacity v1_int = " << v.capacity() << endl; int size = sizeof(v); //vector容器大小 cout << "sizeof v = " << size << endl; return 0; } 运行结果: // Size of v = 5 // Capacity v = 5 // 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
4、resize——减小或者扩大容器的容量
#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; } 运行结果: // 0 2 4 6 8 // 0 2 4 6 8 100 100 // 0 2 4 6
5、size——返回容器内元素的个数
#include <iostream> #include <vector> #include <algorithm> #include <iterator> using namespace std; template <class T> class Print { public: void operator () (T& t) { cout << t << " "; } }; //============================= int main () { vector<char> v(5); Print<char> print; cout << "Size of v = " << v.size() << endl; fill(v.begin(),v.end(),'*'); for_each(v.begin(),v.end(),print); cout << endl; for ( int i=0; i < v.size(); i++ ) cout << v[i] << " "; cout << endl; for ( int i=0; i<5; i++ ) { cout << "Size of v = "; for_each(v.begin(),v.end(),print); cout << endl; v.pop_back(); } return 0; } 运行结果: // Size of v = 5 // * * * * * // * * * * * // Size of v = * * * * * // Size of v = * * * * // Size of v = * * * // Size of v = * * // Size of v = *
6、swap——将自身容器的内容与指定容器的内容交换
#include <iostream> #include <vector> #include <algorithm> using namespace std; template <class T> class Print { public: void operator () (T& t) { cout << t << " "; } }; //============================= int main () { int ary[] = {1,2,3,4,5,6,7,8,9,10}; Print print; vector<int> v1(ary,ary+7); vector<int> v2(ary+7,ary+10); cout << "Vector v1 : "; for_each(v1.begin(),v1.end(),print); cout << endl; cout << "Size of v1 = " << v1.size() << endl << endl; cout << "Vector v2 : "; for_each(v2.begin(),v2.end(),print); cout << endl; cout << "Size of v2 = " << v2.size() << endl << endl; v1.swap(v2); cout << "After swapping:" << endl; cout << "Vector v1 : "; for_each(v1.begin(),v1.end(),print); cout << endl; cout << "Size of v1 = " << v1.size() << endl << endl; cout << "Vector v2 : "; for_each(v2.begin(),v2.end(),print); cout << endl; cout << "Size of v2 = " << v2.size() << endl << endl; return 0; } 运行结果: // Vector v1 : 1 2 3 4 5 6 7 // Size of v1 = 7 // // Vector v2 : 8 9 10 // Size of v2 = 3 // // After swapping: // Vector v1 : 8 9 10 // Size of v1 = 3 // // Vector v2 : 1 2 3 4 5 6 7 // Size of v2 = 7
除非注明,文章均为CppLive 编程在线原创,转载请注明出处,谢谢。