C++ vector details (I always forget it for my own use)

#include
Like arrays, you can access elements in the form of subscript [i], but remember that if you don’t allocate space for it at the beginning of the assignment, you cannot assign values ​​in the form of subscript [i], because at this time var[i] If it does not exist, you can use insert (position pointer, value) or directly arr.push_back(value).

1. Vector basic syntax:

definition:vector arr;
Definition:vector ::iterator it=arr.begin();
New element:value
//Point to the first element of the vector arr.

1.push_back adds a piece of data at the end of the array
arr.push_back(value);

2.pop_back remove the last data of the array
arr.pop_back(); Remember that there is no return value, the last element will be destroyed directly

3.at to get the data of the numbered position
value=arr.at(key) reference at(int pos) returns the reference of the element at pos

4.begin to get the pointer of the array head
it=arr.begin()

5.end gets the pointer of the last element of the array + 1
it=arr.end() returns a pointer to the next position of the last element

6.front gets a reference to the array head
value=arr.front() directly get the reference of the first element of the vector

7.back gets the reference of the last element of the array
value=arr.back() directly get the reference of the last element of the vector

8.max_size gets the maximum vector size

9.capacity The size of the current vector allocation

10.size The size of the currently used data
length=arr.size()

11.resize changes the size of the currently used data. If it is larger than the currently used data, fill the default value

12.reserve to change the size of the space allocated by the current vecotr

13.erase delete the data item pointed to by the pointer
arr.erase(it)

14.clear clear the current vector

15.rbegin returns the start pointer after the vector is reversed (in fact, the original end-1)

16.rend returns the end pointer of the vector reverse structure (in fact, the original begin-1)

17.empty judge whether the vector is empty
arr.empty();

18.swap exchange data with another vector

2. Traverse a one-dimensional vector:

for(vector<int>:: iterator it=arr.begin();it!=arr.end();it++)
{
    
    
    cout<<*it<<" ";
}
或者可以采用数组下标遍历的形式
for(int i=0;i<arr.size()-1;i++)
{
    
    
    cout<<arr[i]<<" ";
}

Traverse a two-dimensional vector

//在这里arr是二维向量;
vector<vector<int>> arr;
for(vector<vector<int>>:: iterator iter=arr.begin();iter!=arr.end();iter++)
{
    
    
    vector<int> temp=*iter;
    for(vector<int>:: iterator it=temp.begin();it!=temp.end();it++)
    {
    
    
        cout<<*it<<" ";
    }
    cout<<endl;
}
//或者也可以直接用遍历下标的形式
for(int i=0;i<arr.size();i++)
{
    
    
    vector<int> temp=arr[i];
    for(int j=0;j<temp.aize();j++)
    {
    
    
        cout<<temp[j]<<" ";
    }
    cout<<endl;
}

3. Delete elements:

//删除最后一个;
arr.pop_back();

//删除指针指向的一个;
arr.erase(it);

//删除指定的一段;
vector<int>:: iterator first, last;
arr.erase(first,last);
//其中[first,last)均为指针 

4. Add elements

(1)
arr.insert(it,value);  //表示在it指向的元素之前插入value;

(2)
arr.push_back(value);  //直接在arr最后插入value;

5. Traverse:

reference at(int pos): returns the reference of the element at pos
reference front(): returns the reference of the first element
reference back(): returns the reference of the tail element
iterator begin(): returns the vector head pointer, pointing to the first element
iterator end (): Returns the vector tail pointer, points to the next position of the last element of the vector
reverse_iterator rbegin(): reverse iterator, points to the last element
reverse_iterator rend(): reverses iterator, points to the position before the first element

6. Judgment function

bool empty() const: Determine whether the vector is empty, if it is empty, there are no elements in the vector

7. Size function

int size() const: returns the number of elements in the vector
int capacity() const: returns the maximum element value that the current vector can hold
int max_size() const: returns the maximum allowable number of vector elements

A question

Example:
Title description
Design the LRU cache structure, the size of the structure is determined during construction, assuming the size is K, and has the following two functions
set(key, value): insert the record (key, value) into the structure
get(key): Return the value value corresponding to the key
[Requirement]
The time complexity of the set and get methods is O(1)
Once a set or get operation of a certain key occurs, the record of this key is considered to be the most commonly used.
When the size of the cache exceeds K, remove the least frequently used record, that is, the oldest set or get.
If opt=1, the next two integers x, y represent set(x, y).
If opt=2, the next integer x represents get(x). If x has not appeared or has been removed, then Return -1
for each operation 2, output an answer

Insert picture description here

class Solution {
    
    
public:
    /**
     * lru design
     * @param operators int整型vector<vector<>> the ops
     * @param k int整型 the k
     * @return int整型vector
     */
    void set(int key,int value,vector<vector<int>> &lru,int k)
    {
    
    
        vector<int> a;
        a.push_back(key);
        a.push_back(value);
        if(lru.size()==k)
        {
    
    
            lru.pop_back();
            lru.insert(lru.begin(), a);
        }
        else if(lru.size()<k)
        {
    
    
            lru.insert(lru.begin(), a); //插入最前面
        }
    }
    
    int get(vector<vector<int>> &lru,int key)  //存在并且使用时候要将其放在最开始的位置;
    {
    
    
        vector<vector<int>>::iterator iter=lru.begin();
        vector<int> temp;
        for(iter=lru.begin();iter!=lru.end();iter++)
        {
    
    
            temp=*iter;
            if(temp[0]==key)
            {
    
    
                lru.erase(iter);
                lru.insert(lru.begin(), temp);
                return temp[1];
                break;
            }
            else
                continue;
        }
        return -1;
    }
    
    vector<int> LRU(vector<vector<int> >& operators, int k) {
    
    
        // write code here
        vector<int> output;
        int num=0;
        vector<vector<int>> lru;   //用于保存<=k个的当前缓存
        vector<vector<int>>::iterator iter;
        vector<int> temp;
        for(iter=operators.begin();iter!=operators.end();iter++)
        {
    
    
            temp=*iter;
            if(temp[0]==1)  //set
            {
    
    
                set(temp[1],temp[2],lru, k);
            }
            else if(temp[0]==2)  //get
            {
    
    
                output.push_back(get(lru,temp[1]));
            }
        }
        return output;
    }
};

Guess you like

Origin blog.csdn.net/qq_43978754/article/details/115231956