当前位置:网站首页>Most detailed collation of vector basis of STL

Most detailed collation of vector basis of STL

2022-06-21 20:48:00 Radish says vegetables

Preface :

Vector Is a kind of dynamic array that can store any type , Is a sequential container , It can be used sort Sort it , The underlying data structure is an array , Elements can be accessed randomly .Vectors Contains a series of continuously stored elements , It behaves like an array . visit Vector Any element in or adding an element from the end can be done within constant level time complexity , The location of the element looking for a specific value or Vector Inserting elements in is linear time complexity .

notes : If you want to use vector, You need to add vector The header file , namely #include<vector>, besides , You also need to add using namespace std;

vector The definition of

C++ Middle structure vector You can use any of the following parameter methods :

grammar :

1.vector();

2.vector(size_type num, const TYPE &val);

3.vector(const vector &from);

4.vector(input_iterator start, input_iterator end);

1· No parameter - Construct an empty vector

2· Number (num) And the value (val) - Construct an initial num The values are val The elements of the Vector

3·vector(from) - Construct a relationship with vector from same vector

4· iterator (start) And iterators (end) - Construct an initial value of [start,end) Interval element Vector( notes : Semi open interval )

give an example , Suppose you construct a containing 3 The values are 4 Of vector

vector<int> v1(3,4);

visit vector Methods for elements in containers

1. Access by subscript

It's the same as accessing an ordinary array , For a defined as vector<typename> v Of vector Containers , Use it directly v[index] Just visit , among index∈[0,v.size()−1]

2. Access via iterator

An iterator can be understood as something like a pointer

vector<typename>::iterator it; Define iterator , Got the iterator it after , Can pass *it To visit vector The elements in

3.v[i] and *(v.begin()+i) It is equivalent.

4. Iterator to implement two kinds of self addition and self subtraction operations

5.begin() The action of a function is to take v First element address of ,end() Function is not meant to take v Tail element address of , But take the next address of the tail element address . It serves as the iterator end flag , Do not store any elements

6. In common use STL In the container , Only in vector and string in , Only allowed to use v.begin()+3 This iterator is written with an integer

vector Common functions

vector Time complexity of common functions :

push_back(), The time complexity is O(1)

pop_back(), The time complexity is O(1)

size(), The time complexity is O(1)

clear(), The time complexity is O(N)

insert(), The time complexity is O(N)

erase(), The time complexity is O(N)

vector Interpretation of common functions :

push_back(x) Is in the vector Add an element after x

pop_back() Used to delete vector The tail element

size() Used to obtain vector The number of elements in , The return is unsigned type , But generally speaking %d It won't be a big problem , This is true for all STL The containers are the same

clear() For emptying vector All elements in

insert(it,x) Used to direct to vector Any iterator of it Insert an element at x

erase():

         Delete single element ,erase(it) That is, delete the iterator as it The element of .

         Delete all elements in an interval ,erase(first,last) Delete immediately [first,last) All the elements inside

         Empty vector You can also use v.erase(v.begin(),v.end());

Other related functions :

Some function usage examples :

Here is a vector Sample code , The contents are as follows :

#include <vector>

#include <iostream>

using namespace std;

int main()

{

    typedef vector<int> vecInt;

    vecInt vecint;

    // Add data

    for (int i = 0; i < 5; i++)

    {

        vecint.push_back(i+1);

    }

    // Traversing elements

    vecInt::iterator it;

    cout << "vecint is: " << endl;

    for (it = vecint.begin(); it != vecint.end(); it++)

    {

        cout << *it << endl;

    }

    // Query data

    cout << "the second element is: " << vecint.at(1) << endl;

    // Delete data , Delete the last element

    it = vecint.end();

    it = it - 1;                // We need to pay attention to ,(it - 1) Corresponds to the last element

    vecint.erase(it);

    // Traverse the elements again , Observe whether the deletion operation is successful

    cout << "after del, vecint is: " << endl;

    for (it = vecint.begin(); it != vecint.end(); it++)

    {

        cout << *it << endl;

    }

    // insert data , Insert data before the last element

    it = vecint.end();

    it = it - 1;

    vecint.insert(it, 100);

   

    // Traverse the elements again , Observe whether the insertion operation is successful

    cout << "after insert, vecint is: " << endl;

    for (it = vecint.begin(); it != vecint.end(); it++)

    {

        cout << *it << endl;

    }

   

    return 0;

}

The result of the above code is as follows :


 

vector Advanced usage of

Dynamic arrays can store more than just basic data types , It can also store custom data types , For example, structure .

struct Student {

string name; // name

int age; // Age

};

int main() {

    vector<Student> class1; // class

    Student stu1, stu2;// Student 1, Student 2

    stu1.name = "luobo";

    stu1.age = 12;

    stu2.name = "xiaoming";

    stu2.age = 25;

    class1.push_back(stu1);

    class1.push_back(stu2);

    return 0;

}

It can be used in the following ways vector Define a two-dimensional array :

vector<vector<int>> vecvec = { {1, 2, 8}, {2, 4, 9}, {4, 7, 10}, {6, 8, 11}};

Actually , We can quickly build such a dynamic array through a constructor , Using constructor is equivalent to using loop , By using constructors appropriately , Can reduce the amount of code .

原网站

版权声明
本文为[Radish says vegetables]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211856337854.html