栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++stl相关操作

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++stl相关操作

vector的一些操作

vector v;
    cout << "vector" << endl;
    //增
    cout << "增" << endl;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    v.push_back(4);
    for (auto i : v) {
        cout << i << " ";
    }
    cout << endl;
    //插,需要使用迭代器
    auto it = v.begin();
    cout << "插" << endl;
    v.insert(it+2, 100);
    for (auto i : v) {
        cout << i << " ";
    }
    cout << endl;
    //删 需要使用迭代器
    cout << "删" << endl;
    auto it1 = v.begin();
    v.erase(it1+2);
    for (auto i : v) {
        cout << i << " ";
    }
    cout << endl;
    //查
    cout << "查" << endl;
    auto find_it= find(v.begin(), v.end(), 2);
    cout << *find_it << endl;

 

deque的一些操作

deque d;
    cout << "deque" << endl;
    //增
    cout << "增" << endl;
    d.push_back(11);
    d.push_back(12);
    d.push_back(13);
    //删
    for (auto i : d) {
        cout << i << " ";
    }
    cout << endl;
    cout << "删" << endl;
    auto itd = d.begin();
    d.erase(itd + 2);
    for (auto i : d) {
        cout << i << " ";
    }
    cout << endl;
    //查
    cout << "查" << endl;
    auto find_d= find(d.begin(), d.end(), 12);
    if (find_d != d.end())
    {
        cout << "查找成功!" << endl;
    }
    //插
    cout << "插" << endl;
    auto in_d = d.begin();
    d.insert(in_d + 2, 100);
    for (auto i : d) {
        cout << i << " ";
    }
    cout << endl;

list的一些操作

//list
    list l;
    cout << "lsit" << endl;
    //增
    cout << "增" << endl;
    l.push_back(11);
    l.push_back(12);
    l.push_back(13);
    //删
    for (auto i : l) {
        cout << i << " ";
    }
    cout << endl;
    cout << "删" << endl;
    auto itl = l.begin();
    //需要先移动到执行的位置下
    advance(itl, 2);
    l.erase(itl);
    for (auto i : l) {
        cout << i << " ";
    }
    cout << endl;
    //查
    cout << "查" << endl;
    auto find_l = find(l.begin(), l.end(), 12);
    if (find_l != l.end())
    {
        cout << "查找成功!" << endl;
    }
    //插
    cout << "插" << endl;
    auto in_l = l.begin();
    advance(in_l, 2);
    l.insert(in_l , 100);
    for (auto i : l) {
        cout << i << " ";
    }
    cout << endl;

set的一些操作

//不允许重复的key值
    set s;
    cout << "list" << endl;
    //增
    cout << "增" << endl;
    s.insert(1);
    //不允许重复元素
    s.insert(1);
    s.insert(2);
    s.insert(3);
    s.insert(4);
    for (auto i : s) {
        cout << i << " ";
    }
    cout << endl;
    //删
    auto its = s.begin();
    advance(its, 2);
    s.erase(its);
    cout << "删" << endl;
    for (auto i : s) {
        cout << i << " ";
    }
    cout << endl;
    //查
    cout << "查" << endl;
    auto find_s = s.find(2);
    if (find_s != s.end()) {
        cout << "查询成功" << endl;
    }
    cout << "multiset:" << endl;
    multiset muls;
    muls.insert(1);
    muls.insert(10);
    muls.insert(1);
    muls.insert(100);
    for (auto muli : muls) {
        cout << muli << " ";
    }
    cout << endl;

map的一些操作

map m;
    //map有多种插入方法
    cout << "map" << endl;
    cout << "增" << endl;
    m[1] = 2;
    m.insert(map::value_type(2, 22));
    m.insert(make_pair(3, 33));
    m.insert(pair(4, 44));
    for (auto it : m) {
        cout << "key:" << it.first << "  value:" << it.second << endl;
    }
    cout << "删" << endl;
    auto it_m = m.begin();
    advance(it_m, 2);
    m.erase(it_m);
    for (auto it : m) {
        cout << "key:" << it.first << "  value:" << it.second << endl;
    }
    cout << "查" << endl;
    auto find_m = m.find(2);
    if (find_m != m.end()) {
        cout << "查询成功!" << endl;
    }
    multimap mulm;
    cout << "multimap" << endl;
    mulm.insert(make_pair(11, 23));
    mulm.insert(make_pair(11, 24));
    mulm.insert(make_pair(12, 23));
    for (auto it : mulm) {
        cout << "key:" << it.first << "  value:" << it.second << endl;
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/853931.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号