vector

vector<type> Name;    //定义一个不定长数组
A.push_back(x);       //将元素x插入在A的最后
A.pop_back();         //将最后一个元素删除
A.insert(x,pos);      //将元素x插入在pos位置
A.erase(pos);         //将pos位置的元素删除
A.erase(pos1,pos2);   //将pos1和pos2之间的元素全部清空
A.resize(n);          //将A的大小定为n
A.size();             //返回A的元素个数
A.begin();            //返回一个迭代器,指向A的第一个元素的位置
A.end();              //返回一个迭代器,指向A的最后一个元素的位置

queue

queue<type> Name;   //定义一个队列
Q.push(x);          //将x放入队列中
Q.front();          //取出队列的头元素
Q.pop();            //将队列的头元素出队
Q.empty();          //判断队列是否为空,返回布尔值

stack

stack<type> Name;   //定义一个栈
S.push(x);          //将元素x压入栈中
S.top();            //取出栈顶元素的值 
S.pop();            //弹出栈顶元素
S.empty();          //判断栈是否为空

HashSet

// C++ program to demonstrate various function of unordered_set
#include <bits/stdc++.h>
using namespace std;

int main()
{
    // declaring set for storing string data-type
    unordered_set<string> stringSet;

    // inserting various string, same string will be stored
    // once in set
    stringSet.insert("code");
    stringSet.insert("in");
    stringSet.insert("c++");
    stringSet.insert("is");
    stringSet.insert("fast");

    string key = "slow";

    //  find returns end iterator if key is not found,
    //  else it returns iterator to that key
    if (stringSet.find(key) == stringSet.end())
        cout << key << " not found\n\n";
    else
        cout << "Found " << key << endl << endl;

    key = "c++";
    if (stringSet.find(key) == stringSet.end())
        cout << key << " not found\n";
    else
        cout << "Found " << key << endl;

    // now iterating over whole set and printing its
    // content
    cout << "\nAll elements : ";
    unordered_set<string> :: iterator itr;
    for (itr = stringSet.begin(); itr != stringSet.end(); itr++)
        cout << (*itr) << endl;
}

HashMap

// C++ program to demonstrate functionality of unordered_map
#include <bits/stdc++.h>
using namespace std;

int main()
{
    // Declaring umap to be of <string, double> type
    // key will be of string type and mapped value will
    // be of double type
    unordered_map<string, double> umap;

    // inserting values by using [] operator
    umap["PI"] = 3.14;
    umap["root2"] = 1.414;
    umap["root3"] = 1.732;
    umap["log10"] = 2.302;
    umap["loge"] = 1.0;

    // inserting value by insert function
    umap.insert(make_pair("e", 2.718));

    string key = "PI";

    // If key not found in map iterator to end is returned
    if (umap.find(key) == umap.end())
        cout << key << " not found\n\n";

    // If key found then iterator to that key is returned
    else
        cout << "Found " << key << "\n\n";

    key = "lambda";
    if (umap.find(key) == umap.end())
        cout << key << " not found\n";
    else
        cout << "Found " << key << endl;

    //    iterating over all value of umap
    unordered_map<string, double>:: iterator itr;
    cout << "\nAll Elements : \n";
    for (itr = umap.begin(); itr != umap.end(); itr++)
    {
        // itr works as a pointer to pair<string, double>
        // type itr->first stores the key part  and
        // itr->second stroes the value part
        cout << itr->first << "  " << itr->second << endl;
     }
}

results matching ""

    No results matching ""