0.vector source code
// the alloc space is SGI STL configurator Template < class T, class of Alloc the alloc => class vector { public : // nested type definition of the vector typedef T value_type; typedef value_type* pointer; typedef value_type* iterator; typedef value_type* reference; typedef size_t size_type; typedef ptrdiff_t difference_type; protected : // simple_alloc spatial configuration is an SGI STL typedef simple_alloc <the value_type, of Alloc> data_allocator; Start Iterator; // represents the head space currently used Iterator Finish; // represents the space currently used tail Iterator end_of_storage; // represents the end of the current free space void insert_aux(iterator position,const T& x); void deallocate(){ if(start) data_allocator::deallocate(start,end_of_storage-start); } void fill_initialize(size_type n,const T& value) { start=allocate_and_fill(n,value); finish=start+n; end_of_storage=finsih; } public: iterator begin(){return start;} iterator end(){return finish;} size_type size() const {return size_type(end()-begin());} size_type capacity() const {return size_type(end_of_storage-begin());} bool empty() const {return begin()==end();} reference operator[](size_type n) {return *(begin()+n);} vector():start(0),finish(0),end_of_storage(0){} vector(size_type n,const T& value){fill_initialize(n,value);} vector(int n,const T& value){fill_initialize(n,value);} vector(long n,const T& value){fill_initialize(n,value);} explicit vector(size_type n){fill_initialize(n,T());} ~vector(){ destroy(start,finish); deallocate(); } Front Reference () { return * the begin ();} // first element Reference Back () { return * (End () - . 1 );} // last element void push_back ( const T & X) { // the element is inserted into the trailing end of the most IF (Finish! = end_of_storage) { construct(finish,x); ++finish; } else insert_aux(end(),x); } void pop_back () { // the most trailing end elements removed - Finish; the destroy (Finish); // global function } ERASE Iterator (Iterator position) { // remove a certain position on the element IF (position + . 1 ! = End) { Copy (position + . 1 , Finish, position); // subsequent elements to move forward } - Finish; destroy(finish); return position; } void resize(size_type new_size,const T& x) { if(new_size<size()) erase(begin()+new_size,end()); else insert(end(),new_size-size(),x); } void resize(size_type new_size){resize(new_size,T());} void clear() {erase(begin(),end());} protected : // configuration space and fills the contents Iterator allocate_and_fill (n-size_type, const T & X) { iterator result=data_allocator::allocate(n); uninitialized_fill_n(result,n,x); return result; } };
1. Definitions and initialize a vector
vector <T> v1; // v1 is a null vector, that the default initialization
vector <T> v2 (v1); // v2 contains a copy of all of the elements v1
vector <T> v2 = v1; // equivalent with the above
vector <T> v3 (n, val); // v3 contains elements of n val
vector <T> v4 (n); // v4 n objects included in an initialized value is repeatedly executed, initializes the value, int type is 0, string type is a null character
vector <T> v5 {a, b, c ...}; // v5 contains the number of elements of the initial value, the initialization list. vector <T> v5 (a, b, c ..); Error
vector <T> v5 = {a, b, c ...}; // supra
Vector < int> tmp (vec.begin (), vec.begin () + . 3); // vectors vec 0th to the second initialization value tmp
Example:
vector <int> v1 (10); // v1 has 10 elements are all 0
vector <int> v2 {10}; // v2 has an element 10
vector <int> v3 (10,1); // v3 elements 10, are a
vector <int> v4 {10, 1}; // v4 has two elements, is 10,1
2.vector operating elements
(1) Capacity
- Vector Size: vec.size ();
- Vector maximum capacity: vec.max_size ();
- Change the vector magnitude: vec.resize ();
- Vector real size: vec.capacity ();
- Vector sentenced empty: vec.empty ();
- Reducing the magnitude of the vector size to meet the storage space occupied by the elements: vec.shrink_to_fit ();
(2) Modify
- A plurality of elements assignment: vec.assign (); // assignment with a similar array initialization time, which will be replaced old element
- Add the last element: vec.push_back ();
- End remove elements: vec.pop_back ();
- val insert element position at the specified location: vec.insert (const_iterator position, value_type & val);
- val n elements inserted position at the specified location: vec.insert (const_iterator position, size_type n, value_type & val);
- Insertion elements within the target range of the same type of container at the specified location: vec.insert (const_iterator position, InputIterator first, InputIterator last);
- Delete the specified location elements: vec.erase (const_iterator position); vec.erase (const_iterator first, const_iterator last); delete the specified range of elements
- Two switching elements of the vector: vec.swap ();
- Empty vector elements: vec.clear ();
(3) iterator
- The start pointers: vec.begin ();
- Pointer to the end: vec.end (); // points to the next position of the last element
- The start pointers pointing constants: vec.cbegin (); // this means that the pointer can not be referred to modify the contents, but can be modified or otherwise, but also can move the pointer.
- Pointer to the end of the constant: vec.cend ();
Access (4) elements
- Subscript visit: vec [1]; // does not check whether the cross-border
- at ways to access: vec.at (1); // difference between the two is at least checks for cross-border, is an exception will be thrown out of range
- Access the first element: vec.front ();
- The last element visit: vec.back ();
- Returns a pointer: int * p = vec.data (); // feasible because the vector array in memory is stored in a continuous, it can return a pointer to the array. This is is a characteristic of C ++ 11.