SYSU programming c++ (15th week)

vectorcontainer

1. To open the vector library

        

2.vector<T> is a dynamic continuous array that can be initialized with a list

        

          vector<int> ivec(10, 2); //Create 10 elements with a value of 2

3. The elements can be accessed by [ ], at(int), front, back, and iterators, among which at will automatically check that the subscript is out of bounds and throw an exception

  

4. Iterators

vector<int>::iterator iter; // Declare a forward iterator object (+1 to the right)

vector<int>::reverse_iterator riter; // Declare a reverse iterator object (+1 to the left)

begin() points to the first position, end points to the position after the last position, rbegin() points to the last position (the first position for the reverse), rend points to the position before the first position (the last position for the reverse)

 

auto and decltype

Flexible use of auto and decltype can reduce the program's dependence on template arguments and improve program versatility and readability

For example auto iter = ivec.begin() or decltype(ivec.begin()) iter ; (declare the type deduced from the expression)

traverse elements with iterator

 5. Element insertion

        c.insert(iter, t) inserts element t before iter points to

        c.insert(iter, n, t) inserts n elements t before what iter points to

        c.push_back(t) inserts element t at the end

        c.push_front(t) inserts element t at the head //vector does not provide front

6. Element deletion

        c.clear() deletes all

        c.erase(iter) deletes a bit and returns the next constant iterator

        c.erase(b, e) deletes the previous digit of b~e, and returns the next digit (e) constant iterator

        c.pop_back() deletes the last bit

        c.pop_front() deletes the first place //vector does not provide front

7. Volume operation

        c.empty() returns true if it is empty, otherwise false

        c.size() returns the number of elements

        c.resize(n) adjust the size of the container so that it can hold n pieces, return more and make up less

        If c.resize(n, t) complements, the value of the new element is t, and the other effects are the same

8. Assignment and exchange

        c1 = c2

        c.assign(n, t) first delete all in c, and then store n elements whose value is t in c

        c1.swap(c2)

おすすめ

転載: blog.csdn.net/jz_terry/article/details/131107109