C++中STL常见用法

有C语言基础,通过短时间学习stl达到方便刷算法题的水平。
文中可能有些许错误,欢迎指出。

1、cin与cout





2、sort函数

在< algorithm >头文件中。

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

int main() {
    int a[] = { 5,3,12,-33,-5,6,99 };
    sort(a, a + 7);
    for (int i = 0; i < 7; i++) {
        cout << a[i] << " ";
    }
    cout << endl;
    return 0;
}




3、string类库基本操作

3.1 初始化及打印

    string ss = "abcdefgggggg";
    cout << ss << endl; //abcdefgggggg
    cout << ss[1]<<endl;//b

3.2 输入一行字符串

使用cin,读取到空格就会停止读取。
使用< string >头文件中的getline函数,可以读取空格,直接获取一行数据。

    string ss;
    getline(cin, ss);//hello world
    cout << ss;//hello world

3.3 +=运算符

+=运算符对字符串和字符也有效

  • += 字符串  直接追加
  • += 字符   追加一个字符
  • += 数字   数字转变为其ascii码对应字符
    string ss;
    ss += "hello";
    ss += " world";
    ss += '5';
    ss += 97;//a的ascii码为97
    int b = 4;
    ss += (b + '0');//0字符的ascii码向右移动5位
    cout << ss;
        输出结果为:hello world5a4

3.4 排序

在< algorithm >头文件中。

    string ss = "55412138";
    sort(ss.begin(), ss.end());
    cout << ss;
        输出结果为:11234558

ss.begin()是字符串头部的指针,ss.end()是尾部的指针。

3.5 erase函数

    string ss = "56421973";
    ss.erase(ss.begin()); //6421973删除第一个
    ss.erase(--ss.end()); //642197删除最后一个
    cout << ss << endl;

3.6 substr函数

    string ss = "56421973";
    ss = ss.substr(2, 4);//从ss[2]取4位,即为4219
    ss = ss.substr(1, -1);//从ss[1]取到最后,即为219
    cout << ss;

3.7 四种循环

1.for循环

    string ss = "56421973";
    for (int i = 0; i < ss.length(); i++)
        cout<<ss[i];

2.迭代器

    string ss = "56421973";
    for (string::iterator it = ss.begin(); it != ss.end(); it++) 
        cout << *it;

3.迭代器化简

    string ss = "56421973";
    for (auto it = ss.begin(); it != ss.end(); it++)
        cout << *it;

4.利用C++ 11新特性for循环

    string ss = "56421973";
    for (auto x:ss)
        cout << x;




4、vector函数

4.1 构造vector

    vector<int> v1;//定义一个空vector,初始为0
    vector<int> v2(5);//定义一个大小为5的vector,初始为0
    vector<int> v3(5, 6);//定义一个大小为6的vector,初始为6
    vector<int> v4{ 1,2,3,4,5 };//定义的vector中数字为1,2,3,4,5
    for (auto x : v3)
        cout << x;

4.2 用[ ]或at()取元素

    vector<int> v{ 1,2,3,4,5 };
    cout << v[2]<<endl;
    cout << v.at(2);
        输出结果为: 3\n3

4.3 方法

  • push_back() 追加内容
    vector<int> v{ 1,2,3,4,5 };
    v.push_back(6);
    v.push_back(7);
    for (auto x : v)
        cout << x;
        输出结果: 1234567
  • resize 进行重置大小
    不赋值默认为0
    vector<int> v{ 1,2,3,4,5 };
    v.resize(10);
    for (auto x : v)
        cout << x;
        输出结果:1234500000
  • erase 删除元素,复杂度为O(n)
    操作同 string
    v.erase(v.begin());//删除第一个元素
    v.erase(--v.end());//删除最后一个元素
  • 获取第一个元素,获取最后一个元素
    cout << v.front();
    cout << v[0];
    cout << *v.begin();

    cout << v.back();
    cout << v[v.size() - 1];
    cout << *--v.end();

4.4 排序

    vector<int> v{ 5,1,2,4,3,-6,8 };
    sort(v.begin(), v.end(), less<int>());//从小到大排序
    sort(v.begin(), v.end(), greater<int>());//从大到小排序
    for (auto x : v) cout << x;

4.5 循环

    vector<int> v{ 1,2,3,4,5 };
    for (int i = 0; i < v.size(); i++) cout << v[i];
    cout << endl;
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++) cout << *it;//迭代器循环
    cout << endl;
    for (auto it = v.begin(); it != v.end(); it++) cout << *it;//迭代器简化循环
    cout << endl;
    for (auto x : v) cout << x;//c++11新特性




5、stack栈

  • 构造
    stack<int> s;
  • 操作
    s.push() 入栈
    s.pop()  出栈(无返回值)
    s.top()  取栈顶元素
    s.size() 栈元素个数
    stack<int> s;
    s.push(2);
    s.push(3);
    cout<<s.top();
    cout << s.size();
    s.pop();
  • 进制转换(十进制转二进制)
int itob(int decimal) {
    stack<int> s;
    int res = 0;
    while (decimal != 0) {
        s.push(decimal % 2);
        decimal /= 2;
    }
    while (!s.empty()) {
        res = res * 10 + s.top();
        s.pop();
    }
    return res;
}
  • 逆序单词(拓展sstream,stoi,itoa)

输入一行字符串,将字符串逆序打印
输入:hello world my name is steve yu
输出:yu steve is name my world hello

#include<iostream>
#include<stack>
#include<sstream>
using namespace std;
int main() {
    string str;
    stack<string> s;
    getline(cin, str);
    stringstream ss;
    ss << str;
    while (ss >> str) 
        s.push(str);
    while (!s.empty()) {
        cout << s.top();
        s.pop();
        if (s.size() != 0)
            cout << " ";
    }
return 0;
}
  • 字符串转数字

方法1

    string s = "1234";
    int i;
    stringstream ss;
    ss << s;
    ss >> i;
    cout << i;

方法2

    string s = "1234";
    int i = stoi(s);
    cout << i;
  • 数字转字符串

方法1

    int a = 1234;
    string out;
    stringstream ss;
    ss << a;
    ss >> out;
    cout << out << endl;

方法2(c++11新特性)

    int a = 1234;
    cout << to_string(a) << endl;




6、queue队列

6.1 构造

    queue<int> q;

6.2 push和back

    queue<int> q;
    q.push(5);
    q.push(6);
    cout << q.front() << endl;
    q.pop();
    cout << q.front() << endl;
    cout << q.size() << endl;




7、map(unordered_map pair)

map:树状表   unordered_map:哈希表

  • map 
    map<int, int> m;//有序的,树状结构(底层)
    m[6] = 3;
    m[7] = 8;
    m[4] = 9;
    for (auto it = m.begin(); it != m.end(); it++)
        cout << it->first << " " << it->second << endl;
    for (auto tmp : m)
        cout << tmp.first << " " << tmp.second << endl;
按照左边从小到大排序输出:4 9
              6 3
              7 8
  • unordered_map
    unordered_map<int, int> m;//无序的,哈希结构(底层)
    m[6] = 3;
    m[7] = 8;
    m[4] = 9;
    for (auto it = m.begin(); it != m.end(); it++)
        cout << it->first << " " << it->second << endl;
    for (auto tmp : m)
        cout << tmp.first << " " << tmp.second << endl;
无序输出:6 3
      7 8
       4 9
  • pair
    map转成vector进行排序
bool cmp(pair<int, int> a, pair<int, int> b) {
    return a.first > b.first;
}
int main() {
    unordered_map<int, int> m;//无序的,哈希结构(底层)
    m[6] = 3;
    m[7] = 8;
    m[4] = 9;
    vector<pair<int, int>> v(m.begin(), m.end());
    sort(v.begin(), v.end(), cmp);
    for (auto tmp : v) {
        cout << tmp.first << tmp.second << endl;
    }
    return 0;
}
按照左边从大到小排序: 78
                     63
                     49




8、set(unordered_set)

集合
s.insert() 添加元素,元素不能重复
s.size() 集合大小

    set<int> s;//树状结构,其元素按照从小到大排序
    unordered_set<int> s2;//哈希结构,无序,快
    s.insert(4);
    s.insert(1);
    s.insert(2);
    s.insert(6);
    s.insert(6);
    cout << s.size() << endl;
    for (auto tmp : s)
        cout << tmp << " ";
    cout << endl;

    for (auto it = s.begin(); it != s.end(); it++)
        cout << *it << " ";
    cout << endl;
输出结果:   4
            1 2 4 6
            1 2 4 6




9、deque 双端队列

    deque<int> d;
    //4 9 1 2
    d.push_back(1);
    d.push_back(2);
    d.push_front(9);
    d.push_front(4);
    d.pop_back();
    d.pop_front();
    for (auto tmp : d) cout << tmp << " ";
    cout << endl;
    for (auto it = d.begin(); it != d.end(); it++) cout << *it <<" ";

排序

    sort(d.begin(), d.end(), greater<int>());




10、list 双向链表

push_back() 和 emplace_back() 相同

    list<int> li;
    li.push_back(6);
    li.push_front(5);
    li.emplace_front(9);
    li.emplace_back(10);
    li.insert(++li.begin(), 2);
    for (auto tmp : li) cout << tmp << " ";
    cout << endl;
    for (auto it = li.begin(); it != li.end(); it++) cout << *it << " ";
    
输出:9 2 5 6 10




11、文档资料

英文http://www.cplusplus.com/reference/stl/

C语言中文网http://c.biancheng.net/stl/map/



参考文献:https://www.cnblogs.com/littlepage/p/12113748.html

猜你喜欢

转载自www.cnblogs.com/aabyss/p/12284034.html