基本的なデータ構造とSTL

コンテナ

1、ベクトル

STLベクトルは動的配列で、インデックスは、一定時間で行われ、中間インサートまたは必要線形時間を削除することができ、時間の複雑さは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
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、リスト

リストには、そのメモリ空間が効率的に削除して、どこにでも挿入し、挿入し、操作が一定時間内に行われ、削除することができますポインタを介して、データにアクセスするために、不連続であってもよい、二重にリンクされたリストデータ構造です。
長所と短所ベクトルのと反対を一覧表示するには、それらは異なるアプリケーションシナリオを持っています。
(1)ベクトル:少数の挿入および欠失、しばしばランダムアクセス要素。

(2)リスト:頻繁に挿入および欠失、あまりランダムアクセス。

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、セット

セットはコレクションです。STLは、二分探索木の実装と設定され、設定された各要素は一度だけ表示され、ソートされます。要素にアクセスする際の複雑さはO(LOGN)非常に効率的です。

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、マップ

STLのマップ検索を使用して実装され、複雑さはO(logN個)である
2648 HDU

//#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;
}

第二に、一般的に使用される機能

1、ソート()

O(nlogn)の複雑
最後含まない、最初を含む)最初、最後のソート範囲です。

//#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;
}

ソート()も一種の構造のことができます

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()

次の順列と組み合わせ、機能next_permulationを提供しようとしているSTL()。このような三文字のシーケンス、B、Cの組成物として、next_permulation()は、6つの辞書式順序、すなわち、ABC、ACB、BAC、BCA、の組み合わせを返すことができる CAB、CBAを。
BOOL next_permulation(最初、最後);
戻り値:なし次の順列が存在しない場合、それ以外の場合はtrueを返す、falseを返し、(各next_permulationを実行する)元の空間の新しい配置を配置します。

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;
}

おすすめ

転載: www.cnblogs.com/lihello/p/11520951.html