vector in C ++ knowledge points

Last time I saw a title used in the vector solution is very subtle, and later made the process of thinking about the problem try using vector, but suffer from limited understanding. Here take notes to be tidy

vector concept of common scenarios &

Vector (the Vector) is a package size of the array of dynamic sequential container (Sequence Container). Like any other type of container, it is possible to store various types of objects. It may be simply that, a vector is capable of storing any type of dynamic arrays . (Excerpt from rookie tutorial invasion deleted)

The following scenarios I would like to use the vector:

1. The need to deal with a string of number, there is no fixed size, you need real-time add, delete. (But can not be deleted vector based on value, this may not apply)

2. a string of numbers to be processed, the order of data is not fixed, the need to do during the operation of the real-time adjustment in accordance with input conditions

“Compared to the other dynamic sequence containers (deques, lists and forward_lists), vectors are very efficient accessing its elements (just like arrays) and relatively efficient adding or removing elements from its end.For operations that involve inserting or removing elements at positions other than the end, they perform worse than the others, and have less consistent iterators and references than lists and forward_lists”

I.e., if this relates to a non-end position of the insertion or deletion operation elements, performance will be relatively poor, but by reference iterators and consistency difference than other containers. That is not really suitable for the needs of the vector container element intermediate position, do the scene of frequent operation.

Common Functions

#include <Vector> 

Vector < int > Vectors; // create a storage type int Vector 

// simply operate the tail 
void push_back (x); // add an element in the vector x tail 
void pop_back (); // remove the last element in the vector 

// insertion and deletion operations 
iterator iNSERT (iterator IT, const T & X); // front element vector iterator to add an element X 
iterator ERASE (iterator IT); // delete vector pointing iterator element, the element returns to a deleted value of the next position 
Iterator ERASE (Iterator First, Last Iterator); // delete the vector [first, last) element 
Iterator ERASE (a const_iterator __first, a const_iterator __last);
// traversal to find a specified value reference at (int pos); // return a reference position of the element pos Reference Front (); // return a reference to the first element Reference Back (); // return a reference to the end of the element Iterator the begin (); // Returns a vector of the head pointer to the first an element Iterator End (); // returns a vector of the tail pointer to the next position of the last element of the vector // other global operations BOOL empty (); // determines the vector is empty void Clear (); // empty vector all the elements int size (); // returns the number of elements in the vector

@ to sort the elements in the vector operation
Sort (vectors.begin (), obj.end ()); // small to large
reverse ( obj.end (), obj.begin ()); // output in reverse order, i.e. according to the above method in ascending order, and then in reverse order, i.e., in descending output

// return the last element of the vector
return vector.at(vector.size()-1);
return vector.back();
return vector.end()-1;

//返回vector的第一个元素
return vector[0];
return vector.front();
return *vector.begin();
return vector.at(0);

For example, you can see erase () function, there iterator and const_iterator, what difference is it?

All standard library container defines a corresponding iterator type, modern C ++ standard operating procedures are more likely to access container elements using an iterator rather than down.

member type definition notes
value_type The first template parameter (T)  
allocator_type The second template parameter (Alloc)

defaults to: allocator

<value_type>

reference value_type&  
const_reference const value_type&  
pointer allocator_traits<allocator_type>::pointer for the default allocator: value_type*
const_pointer allocator_traits<allocator_type>::const_pointer for the default allocator: const value_type*
iterator a random access iterator to value_type convertible to const_iterator
const_iterator a random access iterator to const value_type  
reverse_iterator reverse_iterator<iterator>  

const_reverse_

iterator

reverse_iterator<const_iterator>  
difference_type a signed integral type, identical to:
iterator_traits<iterator>::difference_type
usually the same as ptrdiff_t
size_type an unsigned integral type that can represent any non-negative value of difference_type

usually the same as size_t

Overall is, iterator element values ​​can change it points, but after adding const can only change their values, that is, can point to other elements, but can not change the element to which it points.

Practical application (using the iterator access)

#include < String .h> 
#include <Vector> 
#include <the iostream> 
#include <algorithm>
 the using  namespace STD; 
 
int main () 
{
  
    Vector < int > obj;
     for ( int I = 0 ; I < 10 ; I ++ ) 
    { 
        obj.push_back (I);    
    } 
    vector < int > :: iterator IT; // declare an iterator to access the vector container, effect: traversing pointing element in the vector or container 
    for (IT = obj.begin (); IT ! = obj.end (); IT ++ ) 
    { 
        cout<<*it<<" "; //输出0 1 2 3 4 5 6 7 8 9
    }
    return 0;
}

Or may also be output by accessing the vector index, i.e. obj [0] ~ obj [9].

 

Guess you like

Origin www.cnblogs.com/lyeeer/p/11518664.html
Recommended