Vector of C++STL (Basic part 2)

Q3: What member functions does vector contain?

member function function function
begin() Returns an iterator pointing to the first element in the container
end() Returns an iterator pointing to the position after the last element of the container, usually used in conjunction with begin()
size() returns the actual number of elements
resize() Change the number of actual elements
empty() Determine whether there is an element in the container, if there is no element, return true; otherwise, return false
insert() Insert one or more elements at the specified position
erase() remove an element or a block of elements
clear() Remove all elements, container size becomes 0
swap() swaps all elements of two containers
push_back() Add an element to the end of the sequence
pop_back() removes elements from the end of the sequence

Note: The vector container contains many member functions, only some of them are listed here

1.push_back() and pop_back functions

vector v1={
    
    1,2,3,4,5};
      v1.push_back(6);//在末尾添加6
      for (int i = 0; i < v1.size(); ++i) {
    
    
          cout<<v1.at(i)<<' ';
      }
      **结果:1 2 3 4 5 6
vector v1={
    
    1,2,3,4,5};
      v1.pop_back();//不需要参数
      for (int i = 0; i < v1.size(); ++i) {
    
    
       cout<<v1.at(i)<<' ';
      }
      **结果·:1 2 3 4

2.swap() exchange function

The swap function can be used to exchange the content of two vector containers , see the example:

int a[5]={
    
    1,2,3,4,5};
    vector<int> v1,v2;   
    v1.assign(a,a+5);   //初始化v1={1,2,3,4,5}
    v2.assign(3,6);     //初始化v2={6,6,6}
    v1.swap(v2);        //swap()实现vector容器交换
    //以下为输出容器元素,检测结果
    for (int i = 0; i < v1.size(); ++i) {
    
    
        cout<<v1[i]<<' ';
    }
    cout<<endl;
    for (int i = 0; i < v2.size(); ++i) {
    
    
        cout<<v2[i]<<' ';
    }
    cout<<endl;
    **结果:6 6 6 
           1 2 3 4 5
           //实现了交换

3.insert() insert function

  • The easiest way to insert data at a specified location is v.insert(pos,x);herePointer for position posTo determine, x is the data to be inserted, and there are other insertion forms, see examples:
vector<int> v1, v2;
    v1 = {
    
    1, 2, 3, 4, 5};
    //指定位置插入元素
    v1.insert(v1.begin() + 2, 100);//第一个元素为指针
    for (int i = 0; i < v1.size(); ++i) {
    
    
        cout << v1.at(i) << ' ';
    }
    cout << endl;
    //**结果:1 2 100 3 4 5  //指针指向的位置变成指定数据,后面的元素依次后移

    //指定位置插入多个元素
    v1.insert(v1.begin(), 3, 99);
    for (int i = 0; i < v1.size(); ++i) {
    
    
        cout << v1.at(i) << ' ';
    }
    cout << endl;
    //**结果:99 99 99 1 2 100 3 4 5

    //指定位置插入区间数据
    v2 = {
    
    1, 2, 3, 4, 5};
    int b[5] = {
    
    10, 20, 30, 40, 50};
    v2.insert(v2.begin() + 1, b + 1, b + 3);
    for (int i = 0; i < v2.size(); ++i) {
    
    
        cout << v2.at(i) << ' ';
    }
    cout << endl;
    //**结果:1 20 30 2 3 4 5 

4.empty() judgment empty function

vector<int> v1, v2;
    v1 = {
    
    1, 2, 3, 4, 5};
    cout << "v1=" << v1.empty() << endl;
    cout << "v2=" << v2.empty() << endl;
    **结果:v1=0//代表空
           v2=1

5.resize() to change the container length function

  • Reduce the length, if there is an element in the reduced position, the element will be deleted
vector<int> v1; //如果长度缩小,会将后面的元素删除
    v1 = {
    
    1, 2, 3, 4, 5};
    v1.resize(3);
    for (int i = 0; i < v1.size(); ++i) {
    
    
        cout << v1[i] << ' ';
        **结果:1 2 3 //原本是5个元素,当长度减小为3,原本后面的元素就被删除了
    }
  • increase the length
vector<int> v1, v2;
    int a[5] = {
    
    1, 2, 3, 4, 5};
    v2.assign(a, a + 5);
    v2.resize(8);  //传入的参数要大于原来的容器长度
    for (int i = 0; i < v2.size(); ++i) {
    
    
        cout << v2[i] << ' ';
    }
    cout << endl;
    //结果:1 2 3 4 5 0 0 0   //增加的位置会自动补0

You can also specify the value of the extended area

vector<int> v1, v2;
    int a[5] = {
    
    1, 2, 3, 4, 5};
    v2.assign(a, a + 5);
    v2.resize(8,10);   //第一个参数为要扩展的长度,第二个参数为指定数值
    for (int i = 0; i < v2.size(); ++i) {
    
    
        cout << v2[i] << ' ';
    }
    cout << endl;
    **结果:1 2 3 4 5 10 10 10

6.erase() delete function

vector<int> v1, v2;
    v1 = {
    
    1, 2, 3, 4, 5};
    v1.erase(v1.begin() + 2);//注意传入的是指向要删除数据的指针!
    for (int i = 0; i < v1.size(); ++i) {
    
    
        cout << v1.at(i) << ' ';
    }
    cout << endl;
    //**结果:1 2 4 5

7.clear() clear function

vector<int> v1, v2;
    v1 = {
    
    1, 2, 3, 4, 5};
    v1.clear();
    cout << v1.empty();
    cout << endl;
    //结果:1 代表容器v1为空

Guess you like

Origin blog.csdn.net/weixin_74334323/article/details/130179394