vector 学习
#include <iostream> #include <iomanip> #include <vector> #include <algorithm> using namespace std; void printarray ( const vector <int> & ); int main() { vector <int> array1 = {3, 2, 7, 12, 3, 7, 10, 9, 89}; vector <int> array2 ( array1 ); cout << "---------------------------------------------------" << endl; cout << "the size of array1: " << array1.size() << endl; cout << "the capacity of array1: " << array1.capacity() << endl; printarray ( array1 ); cout << "---------------------------------------------------" << endl; array1.push_back ( 2 ); printarray ( array1 ); cout << "---------------------------------------------------" << endl; array1.pop_back(); printarray ( array1 ); cout << "---------------------------------------------------" << endl; array1.insert ( array1.begin() + 5, 99 ); printarray ( array1 ); cout << "---------------------------------------------------" << endl; array1.insert ( array1.end() - 5, 85 ); printarray ( array1 ); cout << "---------------------------------------------------" << endl; array1.erase ( array1.end() - 5 ); printarray ( array1 ); cout << "---------------------------------------------------" << endl; array1.erase ( array1.begin() + 2, array1.end() - 5 ); printarray ( array1 ); cout << "---------------------------------------------------" << endl; cout << "the first element of array1: " << array1.front() << endl; cout << "the last element of array1:" << array1.back() << endl; cout << "max number element of array1:" << array1.max_size() << endl; cout << "---------------------------------------------------" << endl; array1.at ( 5 ) = 20; printarray ( array1 ); cout << "---------------------------------------------------" << endl; reverse ( array1.begin(), array1.end() ); printarray ( array1 ); cout << "---------------------------------------------------" << endl; sort ( array1.begin(), array1.end() ); printarray ( array1 ); cout << "---------------------------------------------------" << endl; printarray ( array1 ); cout << "the size of array1: " << array1.size() << endl; cout << "the capacity of array1: " << array1.capacity() << endl; printarray ( array2 ); cout << "the size of array2: " << array2.size() << endl; cout << "the capacity of array2: " << array2.capacity() << endl; array2.swap ( array1 ); printarray ( array1 ); cout << "the size of array1: " << array1.size() << endl; cout << "the capacity of array1: " << array1.capacity() << endl; printarray ( array2 ); cout << "the size of array2: " << array2.size() << endl; cout << "the capacity of array2: " << array2.capacity() << endl; cout << "---------------------------------------------------" << endl; vector <int> (array2).swap(array2); printarray ( array2 ); cout << "the size of array2: " << array2.size() << endl; cout << "the capacity of array2: " << array2.capacity() << endl; cout << "---------------------------------------------------" << endl; array2.clear(); printarray ( array2 ); cout << "the size of array2: " << array2.size() << endl; cout << "the capacity of array2: " << array2.capacity() << endl; return 0; } void printarray ( const vector <int> & myarray ) { for ( size_t i = 0; i < myarray.size(); i++ ) { cout << setw ( 5 ) << myarray[i] ; if ( 0 == ( i + 1 ) % 10 ) { cout << endl; } } cout << endl; }