STL learning vector

STL learning chapter: vector

- Introduction to vector

vector is a serialization container with variable length
The difference between vector and array: array is a static space, and vector can be expanded dynamically

Dynamic expansion: instead of continuing the new space after the original space, find a larger memory space, and then copy the original data to the new space to free the original space

- header file of vector

#include<vector>

- definition of vector

Format:

vector<object_type> variable_name;
//object_type is the type of the element in the vector
//variable_name is the name of the vector

initialization:

vector<vector<int> > one;//Storage vector < int > type
vector<string> two;//Store string type
vector<string> three(10,NULL);//10 elements, initialization is NULL
vector<string> four(10,"hi");//10 elements, all initialized as hi
vector<int> v1;//Store int type, name v1
vector<int> v2(10,42);//10 data, initialization is 42

- iterator of vector

Equivalent to a class pointer

vector.begin()//Points to the first element of the vector
vector.end()//Point to the next position of the vector tail element, and end-1 is the tail element
vector.rbegin()//Point to the tail element of the vector, that is, rbegin points to the last element, and rbegin-1 points to the penultimate element
vector.rend()//Point to the previous position of the vector header element, that is, rend points to the previous element of the header element, and rbegin-1 points to the first element

vector::iterator is the type of iterator and can be replaced by auto

vector<typename>::iterator iter Write directly auto iter

Iterators do not support ITER < vector End() and only ITER= vector. end()

For traversal:

for (vector<int>::iterator iter = vector.begin(); iter != vector.end(); iter++){
        cout << *iter << " ";
    } 

- vector common functions

Capacity and size

  • Capacity() / / capacity of vector
  • Size() / / the size of the vector
  • max_size() / / the maximum number of elements that can be accommodated
  • resize(n) / / change the size of the vector to n
  • reserve(n) / / the capacity of the vector is required to be at least n
  • shrink_to_fit() / / it is required to reduce the capacity of the vector to the size
example:

capacity() and size()

//capacity () and size ()
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    vector<int> test{ 1, 2, 3, 4, 5};
    cout << test.capacity() << endl;
    cout << test.size() << endl;
    test.push_back(6);//Insert a new element
    cout << test.capacity() << endl;
    cout << test.size() << endl; 
    return 0;
//output
5
5
10
6
}

When the vector capacity is insufficient to accommodate the current number of elements, it will double the previous number

max_size()

    vector<int> test1;
    cout << test1.max_size() << endl;
//output
4611686018427387903

resize(n)

    vector<int> test2{1, 2, 3, 4, 5};
    cout << test2.size() << endl;
    test2.resize(10); //Change vector size
    cout << test2.size() << endl;
    for (int i = 0;i < test2.size(); i++)
    {
        cout << test2[i] << " ";
    } //for loop
    cout << endl;
    for (vector<int>::iterator iter = test2.begin(); iter != test2.end(); iter++){
        cout << *iter << " ";
    } //for + iterator
    cout << endl;
    for(auto num : test2){
        cout << num << " ";
    } //for+auto keyword
    cout << endl;
    test2.resize(2);//Change vector size
    cout << test2.size() << endl;
    for(auto num : test2){
        cout << num << " ";
    }
    cout << endl;
//output
5
10
1 2 3 4 5 0 0 0 0 0 
1 2 3 4 5 0 0 0 0 0 
1 2 3 4 5 0 0 0 0 0 
2
1 2

It can be found that no matter how large or small the vector size is, it will change. When the vector size is expanded, zero will be filled automatically; When the vector size is reduced, the following elements are automatically discarded.
This also involves the writing of vector traversal. The above code lists three common types: one is the for loop; The second is for + iterator; The third is for+auto keyword. The latter two are new features of C++11.

reserve(n)

    vector<int> test3{1, 2, 3};
    test3.reserve(5);
    cout << test3.size() << endl;
    cout << test3.capacity() << endl;
    vector<int> test4{1, 2, 3, 4, 5};
    test4.reserve(4);
    cout << test4.size() << endl;
    cout << test4.capacity() << endl;
//output
3
5
5
5

It can be found that, unlike resize, reserve only changes when the vector capacity is expanded. When the vector capacity is expanded, it will be expanded according to the n value; When the vector capacity is reduced, the capacity remains the same.

shrink_to_fit()

    vector<int> test5;
    for (int i = 0; i < 6;i++)
    {
        test5.push_back(i);
    }
    cout << test5.capacity() << endl;
    test5.shrink_to_fit();
    cout << test5.capacity() << endl;
    return 0;
//output
8
6

visit

  • front() / / access the first element
  • back() / / access the last element
  • at() / / access the element at the specified location
example:
#include<iostream>
#include<vector>
using namespace std;
int main()
{
    vector<int> test = {1, 2, 3, 4, 5};
    cout << test.front() << endl;
    cout << test.back() << endl;
    cout << test.at(0) << endl;
    cout << test[0] << endl;
    return 0;
}
//output
1
5
1
1

Notice that you can use vector At (index) and vector[index] are used to access the elements at the specified location. But it is better to use at().

assignment

  • assign() / / assignment (suspected to be equivalent to =)

assign(num,value) / / assign num value values to vector
assign(iterator1,iterator2) / / copy all elements in the [first,last) area of this container or other containers to the vector

example:
#include<iostream>
#include<vector>
using namespace std;
int main()
{
    vector<int> test1 = {1, 2, 3, 4, 5};
    test1.assign(3, 6);
    for(auto num : test1)
    {
        cout << num << " ";
    }
    cout << endl;
    vector<int> test2 = {5, 4, 3, 2, 1};
    test1.assign(test2.begin(), test2.begin() + 3);
    for(auto num : test1)
    {
        cout << num << " ";
    }
    cout << endl;
    return 0;
}
//output
6 6 6 
5 4 3 

Insert and delete

  • push_back() / / add an element at the end
  • pop_back() / / delete an element at the end
  • insert() / / insert one or more elements before the specified position
  • Empty() / / insert an element before the specified position and return the iterator pointing to the newly added element [C++11]
  • emplace_back(value) / / add an element at the end [C++11]
  • erase() / / delete element
example:

push_back() and pop_back()

#include <iostream>
#include <vector>
using namespace std;
int main()
{
    vector<int> test{1, 2, 3, 4, 5};
    test.push_back(6);//Tail addition
    for(auto num : test)
    {
        cout << num << " ";
    }
    cout << endl;
    test.pop_back();//Tail deletion
    for(auto num : test)
    {
        cout << num << " ";
    }
    cout << endl;
    return 0;
}
//output
1 2 3 4 5 6 
1 2 3 4 5 

insert()

insert(pos,elem) / / insert elem before the position specified by iterator pos, and return the iterator representing the position of the newly inserted element
insert(pos,n,elem) / / insert n elems before the position specified by the iterator pos, and return the iterator representing the position of the first newly inserted element
insert(pos,first,last) / / insert all elements in the [first,last) area of this container or other containers before the position specified by the iterator pos, and return the iterator at the first newly inserted element position
insert(pos,initlist) / / insert all elements in the initialization list before the position specified by the iterator pos, and return the iterator representing the position of the first newly inserted element

    vector<int> test1{1, 2, 3, 4, 5};
    test1.insert(test1.begin() + 1, 6);//First kind
    for(auto num : test1){
        cout << num << " ";
    }
    cout << endl;
    test1.insert(test1.end()-1, 3, 7);//Second
    for(auto num : test1){
        cout << num << " ";
    }
    cout << endl;
    vector<int> test2{8, 9, 10};
    test1.insert(test1.begin(), test2.begin(), test2.begin() + 2);//Third
    for(auto num : test1){
        cout << num << " ";
    }
    cout << endl;
    test1.insert(test1.end(), {12, 13});//Fourth
    for(auto num : test1){
        cout << num << " ";
    }
    cout << endl;
//output
1 6 2 3 4 5 
1 6 2 3 4 7 7 7 5 
8 9 1 6 2 3 4 7 7 7 5 
8 9 1 6 2 3 4 7 7 7 5 12 13 

Empty() and empty_ back()

Replace (pos, elem) / / insert elem before the position specified by iterator pos, and return the iterator representing the position of the newly inserted element.
Empty () is more efficient than insert (). insert() when inserting an element, first construct the element and then copy (move) it to the container; Empty() constructs the element directly at the specified location when inserting the element.
emplace_back() is better than push_back() is more efficient. The reason is the same as above.

    vector<int> test3{1, 2, 3, 4, 5};
    test3.emplace(test3.begin(), 0);
    test3.emplace_back(6);
    for(auto num : test3){
        cout << num << " ";
    }
    cout << endl;
//output
0 1 2 3 4 5 6 

erase()

erase(iterator) / / delete the element pointed to by the iterator
erase(iterator1,iterator2) / / delete the element of [iterator1,iterator2)

    vector<int> test4{1, 2, 3, 4, 5, 6, 7, 8};
    test4.erase(test4.begin());
    for (auto num : test4)
    {
        cout << num << " ";
    }
    cout << endl;
    test4.erase(test4.begin() + 1, test4.begin() + 5);
    for (auto num : test4)
    {
        cout << num << " ";
    }
    cout << endl;
//output
2 3 4 5 6 7 8 
2 7 8 

Emptying operation

  • clear()

Exchange element

  • swap()
vector1.swap(vector2);

The two vector elements must be of the same type, and the number can be different

summary

After learning it carelessly, I feel that some functions should not be used too much. Specifically, I need to contact more questions to know how to use them

Keywords: C++ STL vector

Added by alapimba on Fri, 14 Jan 2022 23:52:52 +0200