C++实现算法常用的STL---整理

algorithm

  min(a,b)和max(a,b)

#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    cout << max(9,4) <<endl;  //9
    cout << min(9,4) <<endl;  //4
}

  

  sort快排

#include<algorithm>   //注意包含algorithm头文件
#include<iostream>
using namespace std;
int main()
{
    int arr[] = {1,9,8,4,3,6,0,11};
    int length = sizeof(arr) / 4;

    //快速排序
    //sort的排序范围是[start, end),默认使用从小到大排序。
    sort(arr, arr + 3);   //只排序前3个
    sort(arr + 2, arr + length); //排序第2个元素之后的元素。
    sort(arr, arr + length); //排序整个数组
    sort(arr, arr + length, greater<int>()); //从大到小排序整个数组
    return 0;
}

  

  binary_search二分查找

int arr[] = {0,3,5,7,10,15,19,20,22,24,27};
int length = sizeof(arr) / 4;
bool isFind = true;

isFind = binary_search(arr + 2, arr + length, 19); //在[start, end)中进行二分查找key
cout<< isFind << endl;

 

vector(数组)

#include<iostream>
#include<vector>
using namespace std;

void printVector(vector<int>& v) {
    for (vector<int>::iterator begin = v.begin(); begin != v.end(); begin++) {
        cout << *begin << " ";
    }
    cout << endl;
}
int main() {
    vector<int> v;
    cout << "isEmpty:" << v.empty() << endl;
    v.push_back(9);   //在数组的最后添加一个元素
    v.push_back(12);
    v.push_back(15);
    cout << "Size:" << v.size() << endl; // 3

    v.pop_back();   //删除最后一个元素
    printVector(v); //输出 9 12

    cout << "v[1] = " << v.at(1) << endl;  // v[1] = 12

    v.insert(v.begin() + 1, 99);  // 在第v[1]位置插入99元素
    v.insert(v.begin() + 1, 5, 88); //从v[1]开始,插入5个88
    printVector(v);  //9 88 88 88 88 88 99 12

    v.erase(v.begin()); //删除第i个数
    printVector(v); //88 88 88 88 88 99 12

    cout << "first:" << v.front() << endl;


}

  

stack(栈)

#include<stack>   //注意包含stack头文件
#include<iostream>
using namespace std;
int main()
{
    stack<int> s; //声明stack中的类型,以及栈名称
    s.push(3);
    s.push(4);    //入栈
    int length = s.size(); //获取栈元素数量
    int top = s.top();    //获取栈顶元素(不出栈)
    s.pop();    //出栈
    bool isEmpty = s.empty();
    return 0;
}

 

queue(普通队列)

#include<iostream>
#include<queue>   //注意包含queue头文件
using namespace std;
int main()
{
    queue<string> q;
    string s;
    q.push("hello");  //入队
    q.push("world");

    cout<< q.front() << endl;   //hello 获取队首元素值,但是不出队
    cout<< q.back() << endl;    //world 获取队尾元素值,但是不出队

    q.pop();   //队首元素出队
    cout<< q.front() << endl;   //world
    cout<< "size " << q.size() << endl;
    cout<< "isEmpty " << q.empty() <<endl;
    return 0;
}

  

deque(双向队列)

#include<iostream>
#include<deque>
using namespace std;

void printDeque(deque<int> & dq) {
    for (deque<int>::iterator begin = dq.begin(); begin != dq.end(); begin++) {
        cout << *begin << " ";
    }
    cout << endl;
}
int main() {
    deque<int> dq;
    dq.push_back(5);
    dq.push_back(6);
    dq.push_front(8);
    dq.push_front(7);
    printDeque(dq);  //7 8 5 6

    dq.pop_back();
    dq.pop_front();
    printDeque(dq);  // 8 5

    cout << dq.front() << endl;   // 8
    cout << dq.back() << endl;    // 5

    dq.insert(dq.begin() + 1, 9);
    printDeque(dq); //8 9 5

    dq.insert(dq.begin(), 5, 88);
    printDeque(dq); // 88 88 88 88 88 8 9 5
    cout << "size:" << dq.size() << endl; //8
}

 

list(双向链表)

#include<iostream>
#include<list>
using namespace std;

void printList(list<int> l){
    list<int>::iterator p;
    for (p = l.begin(); p != l.end(); p++) {
        cout << *p << " ";
    }
    cout << endl;
}
int main(){
    list<int> l;
    l.push_front(6);  //从左边入队
    l.push_front(7);
    l.push_back(8);   //从右边入队
    printList(l);  //7 6 8

    cout << l.front() << endl;  //7 返回左边第一个元素的值(不删除元素)
    cout << l.back() << endl;  // 8 返回有边第一个元素的值(不删除元素)
    cout << l.size() << endl;  //返回元素的总个数

    l.reverse();  //进行翻转
    printList(l);    //8 6 7

    l.pop_front(); //删除左边第一个元素
    l.pop_back();  //删除右边第一个元素
    printList(l);  //6

    l.push_back(6);
    printList(l);  //6 6
    l.remove(6);   //删除值为6的所有元素
    cout << l.empty() << l.size() << endl;  //1 0

    l.push_back(4);
    l.push_back(1);
    l.push_back(3);
    l.sort();   //排序
    printList(l);   //1 3 4
}

  

set/multiset(集合)

  multiset/set使用平衡二叉树的数据结构,插入和查找时间复杂度都是log n。

  multiset和set的用法相同,只有一个区别:

    1、multiset中可以出现重复的元素。

    2、set中不会出现重复的元素,即使添加重复的元素,也会自动去重。

#include<iostream>
#include<set>   //multiset和set都要包含set头文件
using namespace std;
int main()
{
    int arr[10] = {5,1,2,4,6,4,3,5,8,8};//有重复的元素
    int i;
    multiset<int> ms; //创建一个空格multiset集合
    for (i = 0; i < 10; i++) {
        ms.insert(arr[i]);
    }

    multiset<int>::iterator p;  //声明一个迭代器,类似于指针
    for (p = ms.begin(); p != ms.end(); p++) {
        //ms.begin()  返回一个迭代器,指向multiset的第一个元素
        //ms.end()   返回一个迭代器,指向multiset最后一个元素的后面一个位置
        cout << *p << " ";    //1 2 3 4 4 5 5 6 8 8
    }
    cout << endl;
    int length = ms.size();   //集合中元素的数量
    bool isEmpty = ms.empty();  // 集合是否为空
    int cnt = ms.count(8);   //计算一个数出现的次数
    cout<< length << " " << isEmpty << " " << cnt << endl;  // 10  0   2

    //查找元素,如果找到的话,返回一个迭代器指向找到的元素。如果没有找到的话,就返回multiset中元素总个数size
    p = ms.find(8);
    if (*p != ms.size()) {
        cout<< "found " << *p << endl;   //8
        ms.erase(*p);   //删除集合中所有的8,不是只删除一个。
        cout<< "after delete , the size is "<< ms.size() << endl;
    } else {
        cout<<"not found"<<endl;
    }
    return 0;
}

  

map/multimap(映射、字典)

   map和multimap的都是使用hash算法。

  区别在于,map中的key只能出现一次,而multimap可以出现很多次。

#include<iostream>
#include<map>   //multimap和map都要包含set头文件
using namespace std;
int main()
{
    map<string, string> m;
    m["one"] = "hello";
    m["two"] = "world";
    m.insert(pair<string, string>("three", "C++"));

    bool isEmpty = m.empty();
    int length = m.size();
    string s = m["one"];  //找到的话,就返回对应的值
    cout<< s <<endl;   // hello
    s = m["four"];    //未找到的话,就返回一个类型零值
    cout<< s <<endl;  //返回空字符串

    map<string, string>::iterator p;
    p = m.find("one");
    cout<< p->second << endl; //输出one对应的值--> hello

    m.erase(p);  //删除某个key
    for (p = m.begin(); p != m.end(); p++) {
        cout<< p->second << " ";  // C++ world
    }

    m.clear(); //清空map
    return 0;
}

  

原文地址:https://www.cnblogs.com/-beyond/p/9441759.html