c++ STL总结

#define  _CRT_SECURE_NO_WARNINGS
#pragma warning(disable : 4996)

#include <iostream>
using namespace std;

#include <vector>
#include <algorithm>
#include <string>
#include <deque>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>

class Student {
public:
    Student(int _age, char* _name) {
        this->age = _age;
        strcpy(this->name, _name);
    }
    void printS() {
        cout << this->age<<"+"<<this->name << endl;
    }
public:
    int age;
    char name[100];

};
struct sort_func {  //伪函数,类似java选择器
    bool operator() (const Student &s1, const Student &s2) {
        return s1.age < s2.age;
    }

};
int main(){

//-----------------------------------string------------------------------------------------
/*
    //初始化
    string s1 = "abc"; //char* 转为string
    string s2("abc");
    string s3(4, 'a');
    cout << s1 << "+" << s2 << "+" << s3 << endl;
    for (int i = 0; i < s1.length()+3; i++) {  //数组遍历
        //cout << s1[i] << endl;
        try {    
            cout << s1.at(i) << endl; //数组和at区别是:at可以抛异常
        }
        catch (...) {
            cout << "发生异常" << endl;
        }
    }
    for (string::iterator it = s1.begin(); it != s1.end(); it++) { //迭代器遍历
        cout << *it << endl;
    }
*/

/*
   //copy
    string s1 = "abcdef";
    char buf[100] = {0};
    s1.copy(buf, 6, 0); //从0开始的6个字符
    cout << buf << endl;

    //连接
    string s2 = "abc";
    string s3 = "def";
    //s2 = s2 + s3;
    s2.append(s3);
    cout << s2 << endl;

    //查找,替换
    string s4 = "aaahddaaajafaaa";

    //int index = s4.find("aaa", 0);//找不到输出-1
    //cout << index << endl;

    int offset = 0; 
    int num = 0;
    while (s4.find("aaa", offset) != -1) {
        int index = s4.find("aaa", offset);
        s4.replace(index, 3, "AAA");//从index开始替换3个
        offset = index + 3;
        num ++;
    }
    for (string::iterator it = s4.begin(); it != s4.end(); it++) {
        cout << *it << endl;
    }
*/

/*
    //删除,插入
    string s1 = "aaahddaaajafaaa";
    int offset = 0;
    while (s1.find("aaa", offset) != -1) {
        int index = s1.find("aaa", offset);
        s1.erase(index, 3); //从index开始的3个字符
        offset = index + 3;
    }

    s1.insert(0, "AAA");//从0开始插入
    for (string::iterator it = s1.begin(); it != s1.end(); it++) {
        cout << *it << endl;
    }
*/

/*
    string s1 = "asdsjf";
    transform(s1.begin(), s1.end(), s1.begin(), toupper);//转为大写
    transform(s1.begin(), s1.end(), s1.begin(), tolower);//转为小写
    for (string::iterator it = s1.begin(); it != s1.end(); it++) {
        cout << *it << endl;
    }
*/


//-----------------------------------vector------------------------------------------------
/*
    //初始化
    vector<int> v1;
    v1.push_back(1);
    v1.push_back(2);
    v1.push_back(3);

    int size = v1.size(); //统计元素个数
    int num = count(v1.begin(), v1.end(), 3);//统计3的个数
    int last = v1.back();//获取最后一个数
    int first = v1.front();//获取第一个数
    v1.pop_back(); //删除最后一个数

    for (int i = 0; i < v1.size(); i++) {
    cout << v1[i] << endl;
    }
    for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++) { //遍历
    cout << *it << endl;
    }
    for (vector<int>::reverse_iterator it = v1.rbegin(); it != v1.rend(); it++) { //逆向遍历
    cout << *it << endl;
    }
*/

/*
    //删除
    vector<int> v2(10);
    for (int i = 0; i < 10; i++) {
    v2[i] = i + 1;
    }
    v2.erase(v2.begin(), v2.begin()+3);  //删除区间
    v2.erase(v2.begin()); //删除指定位置
    for (vector<int>::iterator it = v2.begin(); it != v2.end(); ) { //删除等于7的数
        if (*it = 7) {
        it = v2.erase(it);
        }
    }
*/

/*
    //插入
    vector<int> v1;
    v1.push_back(1);
    v1.push_back(2);
    v1.push_back(3);

    vector<int> v2;
    v2.push_back(4);
    v2.push_back(5);
    v2.push_back(6);

    v1.insert(v1.begin(), 4); //{4,1,2,3}
    v1.insert(v1.begin(), 2, 33); //{33,4,1,2,3}
    v1.insert(v1.end(), v2.begin(), v2.end()); //{33,4,1,2,3,4,5,6}

    //求下标
    vector<int>::iterator it = find(v1.begin(), v1.end(), 3);
    cout << "3的下标是:" << distance(v1.begin(), it) << endl; //distance通过两个指针的距离求下标
*/

//-----------------------------------deque------------------------------------------------

/*
    deque<int> d1;
    d1.push_back(1);
    d1.push_back(2);
    d1.push_back(3);
    d1.push_back(4);
    d1.push_front(1);
    d1.pop_front();
    d1.pop_back();

    for (deque<int>::iterator it = d1.begin(); it != d1.end(); it++) {
        cout << *it << endl;
    }
*/

//-----------------------------------stack------------------------------------------------
/*

    //先进后出
    stack<int> s1;
    for (int i = 0; i < 10; i++) {
        s1.push(i);
    }
    cout << s1.size() << endl;
    while (!s1.empty()) {
        int m = s1.top(); //栈顶
        cout << m << endl;
        s1.pop();   //删除栈顶
    }
*/

//-----------------------------------queue------------------------------------------------

/*
    //先进先出
    queue<int> q1;
    q1.push(1);
    q1.push(2);
    q1.push(3);
    q1.push(4);

    cout << "队列长度:" << q1.size() << endl;

    while (!q1.empty()) {
        int tmp = q1.front(); //队头
        cout << tmp << endl;
        q1.pop(); //删除队头
    }

    priority_queue<int> pq; //默认最大值优先级队列,从大到小
    //priority_queue<int, vector<int>, less<int>> pq; //相当于这么写
    //priority_queue<int, vector<int>, greater<int>> pq; //最小值优先级队列

    pq.push(1);
    pq.push(2);
    pq.push(3);
    pq.push(4);
    cout << "队长:" << pq.size() << endl;
    cout << "队头" << pq.top() << endl;
*/

//-----------------------------------list------------------------------------------------
/*
    list<int> li;
    li.push_back(1);
    li.push_back(2);
    li.push_back(3);
    li.push_back(4);
    li.push_back(4);
    cout << "list长度:" << li.size() << endl;

    //插入
    int N = 3;
    list<int>::iterator it = li.begin();
    for (int i = 0; i < N; i++) {
        it++; //list中it不能加数
    }
    li.insert(it, 0); //在第N个位置前插入

    //删除
    li.erase(li.begin());//删除该位置数据
    li.erase(li.begin(), li.end());
    li.remove(4);//删除所有值为4的数

    for (list<int>::iterator it = li.begin(); it != li.end(); it++) { //遍历
            cout << *it << endl;
        }
*/


//-----------------------------------set------------------------------------------------
/*
    set<int> s1; //默认从小到大
    set<int, less<int>> s2; //从大到小


    //插入元素
    for (int i = 0; i < 5; i++) {
        s1.insert(rand());
    }
    s1.insert(100);
    s1.insert(100); //只插入一次

    for (set<int>::iterator it = s1.begin(); it != s1.end(); it++) { //set默认从小到大排列
        cout << *it << endl;
    }

    //删除集合
    while (!s1.empty()) {
        set<int>::iterator it = s1.begin();
        cout << *it << endl;
        s1.erase(s1.begin());
    }
*/

/*
    Student stu1(1, "alice");
    Student stu2(3, "bob");
    Student stu3(2, "petter");

    set<Student, sort_func> s1;

    s1.insert(stu1);
    s1.insert(stu2);
    s1.insert(stu3);

    for (set<Student, sort_func>::iterator it = s1.begin(); it != s1.end(); it++) {
        cout << it->age << "+" << it->name << endl;
    }
*/

/*
    set<int> s1;
    for (int i = 0; i < 5; i++) {
        s1.insert(i+1);
    }

    cout << "值为3的个数:" << s1.count(3) << endl;

    set<int>::iterator it0 = s1.find(3); //返回3的迭代器位置
    set<int>::iterator it1 = s1.lower_bound(3); //返回大于等于3的迭代器位置
    set<int>::iterator it2 = s1.upper_bound(3); //返回大于等于3的迭代器位置
*/


//-----------------------------------set------------------------------------------------

    //初始化
    map<int, string> map1;
    map1.insert(pair<int, string>(1,"hello1"));
    map1.insert(pair<int, string>(2, "hello2"));

    map1.insert(make_pair(3, "hello3"));

    map1[4] = "hello4";
    map1[5] = "hello5";

    for (map<int, string>::iterator it = map1.begin(); it != map1.end(); it++) { //遍历
        cout << it->first << "+" << it->second << endl;
    }

    删除
    //while (!map1.empty()) {
    //  map<int, string>::iterator it = map1.begin();
    //  map1.erase(it);
    //}

    //查找
    map<int, string>::iterator it = map1.find(3); //键
    cout << it->first << "+" << it->second << endl;

    pair<map<int, string>::iterator, map<int, string>::iterator> mypair = map1.equal_range(5);//返回两个迭代器,形成pair
    //第一个迭代器 >=5 的位置
    //第一个迭代器 = 5 的位置

    if (mypair.first == map1.end()) {
        cout << "unfind" << endl;
    }
    else {
        cout << mypair.first->first << "+" << mypair.first->second << endl;
    }


    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值