banner
苍术

苍术

不探索,无进步 [email protected]

c++ STL 庫 (未完成)

Structure Diagram#

Structure Diagram

set(集合)#

An internally automatically ordered container without duplicate elements

#include <set>
set<int> name;
set<double> name;
set<char> name;
set<struct node> name;
set<set<int> > name;//Note: there should be a space between > >
set<int> arr[10];

Accessing Elements in a set Container#

set can only be accessed through iterators (except for vector and string, all other STL containers do not support the access method *(it+i)):

set<int>::iterator it;
set<char>::iterator it;
    for (set<int>::iterator it = st.begin(); it != st.end(); it++)
    {
        cout << *it << endl;
    }

Common Functions#

insert(value)#

    st.insert('C');

find(value)#

find(value) returns the iterator corresponding to the value in the set, which is the pointer (address) of the value

cout << *(st.find(2)) << endl;

erase(it)#

st.erase(st.find(100)); //find 100 using find() function, then erase it using erase()

erase(iteratorBegin , iteratorEnd)#

Left-closed and right-open

    st.erase(it, st.end());

size()#

    cout << st.size() << endl;

vector(矢量)#

Variable-length array

#include <vector>

vector<int> name;
vector<double> name;
vector<char> name;
vector<struct node> name;
vector<vector<int> > name;//Note: there should be a space between > >

In a two-dimensional array, its one-dimensional form is the address. For example:

    int arr[3][2];//Define a 3-row 2-column address
    cout<<arr[0]<<endl; //Output the address of the first row of arr

output:

0x61fe00 //The address of the first row of arr

Accessing Elements in a vector Container#

(1) Accessing by Subscript

    cout<<vi[0]<<endl;

(2) Accessing by Iterator

An iterator can be understood as a pointer

vector<type>::iterator variableName;

vector<int>::iterator it;
vector<double>::iterator it;

    vector<int>::iterator it=v.begin();
    for (int i = 0; i < v.size(); i++)
    {
       cout<<it[i]<<" ";
    }

    vector<int>::iterator it=v.begin();
    for (int i = 0; i < v.size(); i++)
    {
       cout<<*(it+i)<<" ";
    }

    //The iterator of vector does not support the writing of it<v.end(), so the loop condition can only be it!=v.end()
    for (vector<int>::iterator it=v.begin(); it!=v.end();it++)
    {
        cout<<*it<<" ";
    }


Analysis of Common Functions in vector#

(1) push_back(item)#

        v.push_back(i);

(2) void pop_back()#

        v.push_back(i);

(3) size()#

    cout<<v.size()<<endl;

(4) clear()#

    v.clear();

(5) insert()#

insert(__position,__x);

v.insert(v.begin()+2,-1); //Insert -1 at the position of v[2]

(6) erase() Left-closed and right-open#

erase(__position); //Delete an element
   v.erase(v.begin()+3);

erase(__positionBegin,__positionEnd);//Delete elements within a range
    v.erase(v.begin()+1,v.begin()+4);
載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。