Basic data structures and STL

A container

1、vector

STL vector is a dynamic array, the index can be done in constant time, an intermediate insert or delete a linear time required, the time complexity is O (n)

vector<int> b(a);  //用a定义b
vector<int> a(100);  //a有100个值为0的元素
vector<int> a(100, 6)  //a有100个值为6的元素
 
vector<string> a(10, "hello"); 
vector<string> b(a.begin(), a.end());  // b是a的复制
 
a.insert(a.begin() + i, k);  // 在第i个元素前插入k
a.erase(a.begin() + i, a.bejin() + j);  //删除区间[i, j - 1]的元素
a.erase(a.begin() + 2);  // 删除第三个元素
reverse(a.begin(), a.end());  //翻转数组

hdu 4841

//#include <bits/stdc++.h>
#include <vector>
#include <iostream>
using namespace std;
 
int main(){
    vector<int> table;
    int n, m;
    while(cin >> n >> m){
        table.clear();
        for(int i = 0; i < 2 * n; i++) table.push_back(i);
        int pos = 0;
        for(int i = 0; i < n; i++){
            pos = (pos + m - 1) % table.size();
            table.erase(table.begin() + pos);
        }
        int j = 0;
        for(int i = 0; i < 2 * n; i++){
            if(!(i % 50) && i) cout << endl;
            if(j < table.size() && i == table[j]){
                j++;
                cout << "G";
            }
            else
                cout << "B";
        }
        cout << endl << endl;
    }
    return 0;
}

2、stack、queue

Stack storage space is needed, if the depth is too large, or stored into the stack array is too large, the total number will exceed the system to allocate space on the stack, so that it will burst stack that stack overflow
can manually write stacks to solve this problem

// stack
s.push(item);
s.top();
s.pop();
 
s.size();
s.empty();
 
// queue
q.push(item);
q.front();  //返回队首元素,但不会删除
q.pop();  // 删除队首元素
q.back();  // 返回队尾元素

hdu 1062

//#include <bits/stdc++.h>
#include <iostream>
#include <stack>
 
using namespace std;
int main(){
    int n;
    char ch;
    scanf("%d", &n); getchar();
    while(n--){
        stack<char> s;
        while(true){
            ch = getchar();
            if(ch == ' ' || ch == '\n' || ch == EOF){
                while(!s.empty()){
                    printf("%c", s.top());
                    s.pop();
                }
                if(ch == '\n' || ch == EOF) break;
                printf(" ");
            }
            else s.push(ch);
        }
        printf("\n");
    }
    return 0;
}

hdu 1702

//#include <bits/stdc++.h>
#include <iostream>
#include <stack>
#include <string>
#include <queue>
#include <stack>
using namespace std;
int main(){
    int t, n, temp;
    cin >> t;
    while(t--){
        string str, str1;
        queue<int> Q;
        stack<int> S;
        cin >> n >> str;
        for(int i = 0; i < n; i++) {
            if (str == "FIFO") {
                cin >> str1;
                if (str1 == "IN") {
                    cin >> temp;
                    Q.push(temp);
                }
                if (str1 == "OUT") {
                    if (Q.empty()) cout << "None" << endl;
                    else {
                        cout << Q.front() << endl;
                        Q.pop();
                    }
                }
            } else {
                cin >> str1;
                if (str1 == "IN") {
                    cin >> temp;
                    S.push(temp);
                }
                if (str1 == "OUT") {
                    if (S.empty()) cout << "None" << endl;
                    else {
                        cout << S.top() << endl;
                        S.pop();
                    }
                }
            }
        }
    }
    return 0;
}

3、list

list is a doubly linked list data structure, its memory space may be discontinuous, for accessing data through a pointer, which can efficiently remove and insert anywhere, insert, and delete operations are done in constant time.
the advantages and disadvantages of vector and list the contrary, they have different application scenarios.
(1) vector: fewer insertions and deletions, random access elements frequently.

(2) list: frequent insertions and deletions, less random access.

hdu 1276

//#include <bits/stdc++.h>
#include <iostream>
#include <stack>
#include <string>
#include <queue>
#include <stack>
#include <list>
using namespace std;
int main(){
    int t, n;
    cin >> t;
    while(t--){
        cin >> n;
        int k = 2;
        list<int> mylist;
        list<int>:: iterator it;
        for(int i = 1; i <= n; i++) mylist.push_back(i);
        while(mylist.size() > 3){
            int num = 1;
            for(it = mylist.begin(); it != mylist.end();){
                if(num++ % k == 0){
                    it = mylist.erase(it);
                }
                else it++;
            }
            k == 2? k = 3: k = 2;
        }
        for(it = mylist.begin(); it != mylist.end(); it++){
            if(it != mylist.begin()){
                cout << " ";
            }
            cout << *it;
        }
        cout << endl;
    }
    return 0;
}

4、set

set is a collection. STL is set with a binary search tree implementation, set each element appears only once, and is sorted. The time complexity of accessing elements is O (logn) very efficient.

a.insert(item);  //插入
a.erase(item);  //删除
a.find(k);  //返回一个迭代器,指向k
a.lower_bound();  // 返回一个迭代器,指向键值不小于k的第一跟元素
a.upper_bound();  // 返回一个迭代器,指向键值大于k的第一个元素

hdu 2094

//#include <bits/stdc++.h>
#include <iostream>
#include <stack>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <list>
using namespace std;
int main(){
    set<string> A, B;
    string s1, s2;
    int n;
    while (cin >> n && n){
        for(int i = 0; i < n; i++){
            cin >> s1 >> s2;
            A.insert(s1);
            A.insert(s2);
            B.insert(s2);
        }
        if(A.size() - B.size() == 1){
            cout << "Yes" << endl;
        }
        else cout << "No" << endl;
        A.clear();
        B.clear();
    }
    return 0;
}

5、map

Implemented using the STL map lookup, the complexity is O (logN)
HDU 2648

//#include <bits/stdc++.h>
#include <iostream>
#include <stack>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <list>
#include <map>
using namespace std;
int main() {
    int n, m, p;
    map<string, int> shop;
    while (cin >> n){
        string s;
        for(int i = 1; i <= n; i++) cin >> s;
        cin >> m;
        while(m--){
            for(int i = 1; i <= n; i++){
                cin >> p >> s;
                shop[s] += p;
            }
            int rank = 1;
            map<string, int>::iterator it;
            for(auto it: shop){
                if(it . second > shop["memory"])
                    rank ++;
            }
            cout << rank << endl;
        }
        shop.clear();
    }
    return 0;
}

Second, the commonly used functions

1、sort()

Complexity of O (nlogn)
which is sorted range [first, last), comprising a first, not including the last.

//#include <bits/stdc++.h>
#include <iostream>
#include <stack>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <list>
#include <map>
#include <algorithm>
using namespace std;
 
bool my_less(int i, int j) {return (i < j);}  // 自定义升序
bool my_greater(int i, int j){return (i > j);}  // 自定义降序
int main(){
    vector<int> a = {3, 7, 2, 5, 6, 8, 5, 4};
    sort(a.begin(), a.begin() + 4);  // 对前四个排序
    sort(a.begin(), a.end());  // 默认升序排列
    sort(a.begin(), a.end(), less<int>());
    sort(a.begin(), a.end(), my_less);
    sort(a.begin(), a.end(), greater<int>());
    sort(a.begin(), a.end(), my_greater);
    for(int i = 0; i < a.size(); i++){
        cout << a[i] << " ";
    }
    return 0;
}

sort () can also sort of structure

struct Student{
    char name[256];
    int score;
}
 
bool cmp(struct Student* a, struct Student* b){
    return a -> score > b -> score;
}
 
vector<struct struct*> list;
 
sort(list.begin(), list.end(), cmp);

2, next_permulation ()

STL seeking to provide the next permutation and combination function next_permulation (). Such as a sequence of three characters a, b, c composition, next_permulation () can return a combination of six lexicographical ordering, i.e. abc, acb, bac, bca, cab, cba.
bool next_permulation (first, last);
Return Value: If there is no next permutations returns false, otherwise it returns true, execute each next_permulation () will place the new arrangement of the original space.

hdu 1027

//#include <bits/stdc++.h>
#include <iostream>
#include <stack>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <list>
#include <map>
#include <algorithm>
using namespace std;
int a[1001];
int main(){
    int n, m;
    while(cin >> n >> m){
        for(int i = 1; i <= n; i++) a[i] = i;
        int b = 1;
        do{
            if(b == m) break;
            b++;
        }while(next_permutation(a + 1, a + 1 + n));
        for(int i = 1; i < n; i++){
            cout << a[i] << " ";
        }
        cout << a[n] << endl;
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/lihello/p/11520951.html