«

3 STL-常用的容器

时间:2023-10-31 00:14     作者:wen     分类: C++


[toc]

常用的容器

string容器

string基本概念

本质:

string和**char *** 区别

特点:

string类内部封装了很多成员方法

例如:查找find,拷贝copy,删除delete,替换replace,插入insert

string管理char * 所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责

string构造函数

构造函数原型:


示例:

#include <iostream>
#include <string>

using namespace std;

//string构造函数
void testA()
{

    string s1; //默认构造

    const char* str = "hello world";
    string s2(str);
    cout << "s2 = " << s2 << endl;

    string s3(s2); //拷贝构造
    cout << "s3 = " << s3 << endl;

    string s4(10, 'a');
    cout << "s4 = " << s4 << endl;

}

int main()
{
    testA();
    return 0;
}

string的赋值操作

功能描述:


赋值的函数原型:


示例:

#include <iostream>
#include <string>

using namespace std;

//string的赋值操作
void testA()
{
    string str1;
    str1 = "hello world";
    cout << "str1 = " << str1 << endl;

    string str2;
    str2 = str1;
    cout << "str2 = " << str2 << endl;

    string str3;
    str3 = 'a';
    cout << "str3 = " << str3 << endl;

    string str4;
    str4.assign("hello C++");
    cout << "str4 = " << str4 << endl;

    string str5;
    str5.assign("hello C++", 5);
    cout << "str5 = " << str5 << endl;

    string str6;
    str6.assign(str5);
    cout << "str6 = " << str6 << endl;

    string str7;
    str7.assign(10, 'w');
    cout << "str7 = " << str7 << endl;

}

int main()
{
    testA();
    return 0;
}

string字符串拼接

功能描述:

函数原型:


示例:

#include <iostream>
#include <string>

using namespace std;

//string字符串拼接
void testA()
{
    string str1 = "我";

    str1 += "爱玩游戏";
    cout << "str1 = " << str1 << endl;

    str1 += ':';
    cout << "str1 = " << str1 << endl;

    string str2 = "LOL DNF";
    str1 += str2;
    cout << "str1 = " << str1 << endl;

    string str3 = "I";
    str3.append(" LOVE ");
    cout << "str3 = " << str3 << endl;

    str3.append("game abcde", 5);
    cout << "str3 = " << str3 << endl;

    str3.append(str2);
    cout << "str3 = " << str3 << endl;

    str3.append(str2, 3, 4);
    cout << "str3 = " << str3 << endl;
}

int main()
{
    testA();
    return 0;
}

总结:字符串拼接的重载版本很多,初学阶段记住几种即可


string查找和替换

功能描述:

函数原型:


示例:

#include <iostream>
#include <string>

using namespace std;

//string查找和替换
//1.查找
void testA()
{

    string s = "abcdefgde";
    // 从左向右查找
    int pos = s.find("de");
    if (pos == -1)
    {
        cout << "未找到字符串" << endl;
    }
    cout << "pos = " << pos << endl;

    // 从右向左查找
    pos = s.rfind("de");
    cout << "pos = " << pos << endl;
}

//2.替换
void testB()
{
    string str1 = "abcdefg";
    str1.replace(1, 3, "AAAA");
    cout << "str1 = " << str1 << endl;
}

int main()
{
    testA();
    testB();
    return 0;
}

总结:


string字符串比较

功能描述:

比较方式:

= 返回 0

> 返回 1

< 返回 -1


函数原型:


示例:

#include <iostream>
#include <string>

using namespace std;

//string比较
void testA()
{
    string s1 = "xello";
    string s2 = "zello";
    int flag = s1.compare(s2);
    if (flag == 0)
    {
        cout << "相等" << endl;
    }
    else if (flag == 1)
    {
        cout << "s1 大于 s2" << endl;
    }
    else {
        cout << "s1 小于 s2" << endl;
    }

}

int main()
{
    testA();
    return 0;
}

总结:字符串对比主要是用于比较两个字符串是否相等,判断谁打谁小的意义并不是很大


string字符存取

string中单个字符存取方式有两种


示例:

#include <iostream>
#include <string>

using namespace std;

//string字符存取
void testA()
{
    string s = "hello";
    cout << s.size() << endl;
    //1.通过[]访问单个字节
    for (int i = 0; i < s.size(); i++)
    {
        cout << s[i] << " ";
    }
    cout << endl;
    //2.通过at访问单个字节
    for (int i = 0; i < s.size(); i++)
    {
        cout << s.at(i) << " ";
    }
    cout << endl;

    //修改单个字符
    s[2] = 'e';
    s.at(3) = 'e';
    cout << s << endl;
}

int main()
{
    testA();
    return 0;
}

string插入和删除

功能描述:

函数原型:


示例:

#include <iostream>
#include <string>

using namespace std;

//string插入和删除
void testA()
{
    string str = "hello";

    //插入
    str.insert(1, "xxx");
    cout << str << endl;

    str.insert(0, 3, 'z');
    cout << str << endl;

    //删除
    str.erase(1, 3);
    cout << str << endl;
}

int main()
{
    testA();
    return 0;
}

总结:插入和删除的起始下标都是从0开始


string子串

功能描述:

函数原型:


示例:

#include <iostream>
#include <string>

using namespace std;

//string子串
void testA()
{
    string str = "hello@sina.com";

    string s = str.substr(0, str.find('@'));
    cout << s << endl;
}

int main()
{
    testA();
    return 0;
}

vector容器

vector基本概念

功能:


vector与普通数组区别:


动态扩展:


vector容器


vector构造函数

功能描述:

函数原型:


示例:

#include <iostream>
#include <string>

using namespace std;
#include <vector>

// vector构造函数

void printVector(vector<int>& v)
{
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

void testA()
{
    vector<int> v1; //默认构造,无参构造
    for (int i = 0; i < 10; i++)
    {
        v1.push_back(i);
    }
    printVector(v1);

    //通过区间方式进行构造
    vector<int>v2(v1.begin(), v1.end());
    printVector(v2);

    //n个elem方式构造
    vector<int>v3(10, 100);
    printVector(v3);

    //拷贝构造
    vector<int>v4(v3);
    printVector(v3);
}

int main()
{
    testA();
    return 0;
}


vector赋值操作

功能描述:

函数原型:

示例:

#include <iostream>
#include <string>

using namespace std;
#include <vector>

// vector赋值操作

void printVector(vector<int>& v)
{
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

void testA()
{
    vector<int> v1; //默认构造,无参构造
    for (int i = 0; i < 10; i++)
    {
        v1.push_back(i);
    }
    printVector(v1);

    // 赋值 operator=
    vector<int> v2 = v1;
    printVector(v2);

    // assign
    vector<int> v3;
    v3.assign(v1.begin(), v1.end());
    printVector(v3);

    vector<int> v4;
    v4.assign(10, 100);
    printVector(v4);
}

int main()
{
    testA();
    return 0;
}


vector容量和大小

功能描述:


函数原型:


示例:

#include <iostream>
#include <string>

using namespace std;
#include <vector>

// vector容量和大小

void printVector(vector<int>& v)
{
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

void testA()
{
    vector<int> v1; //默认构造,无参构造
    for (int i = 0; i < 10; i++)
    {
        v1.push_back(i);
    }
    printVector(v1);

    if (v1.empty())
    {
        cout << "容器为空" << endl;
    }
    else {
        cout << "容器不为空" << endl;
        cout << "v1的容量为:" << v1.capacity() << endl;
        cout << "v1的大小为:" << v1.size() << endl;
    }

    // 重新指定大小
    v1.resize(15,100);//利用重载版本,可以指定默认填充, 参数2
    //如果重新指定的比原来的长,默认用0填充
    printVector(v1);

    //指定的更小,超出部分元素被删除
    v1.resize(5);
    printVector(v1);

}

int main()
{
    testA();
    return 0;
}


vector插入和删除

功能描述:

函数原型:


示例:

#include <iostream>
#include <string>

using namespace std;
#include <vector>

// vector插入和删除

void printVector(vector<int>& v)
{
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

void testA()
{
    vector<int> v1; //默认构造,无参构造
    for (int i = 0; i < 10; i++)
    {
        // 尾部插入
        v1.push_back(i);
    }
    printVector(v1);

    // 删除尾部一个元素
    v1.pop_back();
    printVector(v1);

    // 插入
    v1.insert(v1.begin(), 100);
    printVector(v1);

    v1.insert(v1.begin(), 2, 1000);
    printVector(v1);

    // 删除
    v1.erase(v1.begin());
    printVector(v1);

    // 清空
    v1.erase(v1.begin(), v1.end());
    printVector(v1);

    v1.clear();
    printVector(v1);
}

int main()
{
    testA();
    return 0;
}


vector数据存取

功能描述:

函数原型:


示例:

#include <iostream>
#include <string>

using namespace std;
#include <vector>

// vector数据存取

void printVector(vector<int>& v)
{
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

void testA()
{
    vector<int> v1; //默认构造,无参构造
    for (int i = 0; i < 10; i++)
    {
        // 尾部插入
        v1.push_back(i);
    }
    // operator=
    cout << v1[1] << endl;

    // at
    cout << v1.at(2) << endl;

    //获取第一个元素
    cout << "第一个元素: " << v1.front() << endl;
    //获取最后一个元素
    cout << "最后一个元素: " << v1.back() << endl;
}

int main()
{
    testA();
    return 0;
}


vector互换容器

功能描述:

函数原型:

示例:

#include <iostream>
#include <string>

using namespace std;
#include <vector>

// vector容器互换

void printVector(vector<int>& v)
{
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

void testA()
{
    vector<int> v1; //默认构造,无参构造
    for (int i = 0; i < 10; i++)
    {
        // 尾部插入
        v1.push_back(i);
    }
    cout << "交换前: " << endl;
    printVector(v1);

    vector<int> v2;
    for (int i = 10 - 1; i >= 0; i--)
    {
        v2.push_back(i);
    }
    printVector(v2);

    v1.swap(v2);
    cout << "交换后: " << endl;
    printVector(v1);
    printVector(v2);
}

//实际用途
//巧用swap可以收缩内存空间
void testB()
{
    vector<int> v;
    for (int i = 0; i < 100000; i++)
    {
        v.push_back(i);
    }
    cout << "v的容量为: " << v.capacity() << endl;
    cout << "v的大小为: " << v.size() << endl;

    v.resize(3);
    cout << "v的容量为: " << v.capacity() << endl;
    cout << "v的大小为: " << v.size() << endl;
    //收缩内存空间
    vector<int>(v).swap(v);
    cout << "v的容量为: " << v.capacity() << endl;
    cout << "v的大小为: " << v.size() << endl;
}

int main()
{
    testA();
    testB();
    return 0;
}


vector预留空间

功能描述:

函数原型:

示例:

#include <iostream>
#include <string>

using namespace std;
#include <vector>

// vector预留空间

void printVector(vector<int>& v)
{
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

void testA()
{
    vector<int> v;
    size_t previousCapacity = v.capacity();

    for (int i = 0; i < 1000000; i++)
    {
        v.push_back(i);

        if (v.capacity() != previousCapacity) {
            std::cout << "Vector 容量增加: " << previousCapacity << " -> " << v.capacity() << std::endl;
            previousCapacity = v.capacity();
        }
    }
}

void testB()
{
    vector<int> v;
    //预留空间
    v.reserve(100000);
    int* p = NULL;
    int num = 0;//计算动态扩展空间的次数
    for (int i = 0; i < 100000; i++)
    {
        v.push_back(i);

        if (p != &v[0])
        {
            p = &v[0];
            num++;
        }
    }
    cout << "num = " << num << endl;
}

int main()
{
    testB();
    return 0;
}

deque容器

deque容器基本概念

功能:


deque与vector区别:

deque容器

deque内部工作原理:

deque内部有个中控器,维护每段缓冲区的内容,缓冲区中存放真实数据

中控器维护的是每个缓冲区的地址,使得使用deque时像一片连续的内存空间

deque中控器

deque构造函数

功能描述:

函数原型:

示例:

#include <iostream>
#include <string>

using namespace std;
#include <deque>

void printDeque(const deque<int>& d)
{
    for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
    {
        //*it = 100; 容器中的数据不可被修改
        cout << *it << " ";
    }
    cout << endl;
}

void testA()
{
    deque<int> d1;
    for (int i = 0; i < 10; i++)
    {
        d1.push_back(i);
    }

    printDeque(d1);

    deque<int> d2(d1.begin(), d1.end());
    printDeque(d2);

    deque<int> d3(10, 100);
    printDeque(d3);

    deque<int> d4(d3);
    printDeque(d4);
}

int main()
{
    testA();
    return 0;
}

deque赋值操作

功能描述:

函数原型:

示例:

#include <iostream>
#include <string>

using namespace std;
#include <deque>

void printDeque(const deque<int>& d)
{
    for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

void testA()
{
    deque<int> d1;
    for (int i = 0; i < 10; i++)
    {
        d1.push_back(i);
    }

    printDeque(d1);

    // operator=赋值
    deque<int> d2;
    d2 = d1;
    printDeque(d2);

    //assign赋值
    deque<int> d3;
    d3.assign(d2.begin(), d2.end());
    printDeque(d3);

    deque<int> d4;
    d4.assign(10, 100);
    printDeque(d4);
}

int main()
{
    testA();
    return 0;
}

deque容器大小操作

功能描述:

函数原型:

示例:

#include <iostream>
#include <string>

using namespace std;
#include <deque>

void printDeque(const deque<int>& d)
{
    for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

void testA()
{
    deque<int> d1;

    for (int i = 0; i < 10; i++)
    {
        d1.push_back(i);
    }

    if (d1.empty())
    {
        cout << "空的" << endl;
    }
    else {
        printDeque(d1);
    }

    cout << "容器大小为: " << d1.size() << endl;
    //重新指定大小
    d1.resize(15);
    printDeque(d1);

    d1.resize(20, 1);
    printDeque(d1);

    d1.resize(5);
    printDeque(d1);
}

int main()
{
    testA();
    return 0;
}

deque的插入和删除

功能描述:

函数原型:

两端插入操作:

指定位置操作:

示例:

#include <iostream>
#include <string>

using namespace std;
#include <deque>

void printDeque(const deque<int>& d)
{
    for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

void testA()
{
    deque<int> d1;

    //尾插
    d1.push_back(10);
    d1.push_back(20);
    //头插
    d1.push_front(100);
    d1.push_front(200);
    printDeque(d1);

    //尾删
    d1.pop_back();
    //头删
    d1.pop_front();
    printDeque(d1);

    d1.insert(d1.begin(), 30);
    printDeque(d1);
    d1.insert(d1.begin(), 2, 40);
    printDeque(d1);

    //区间插入
    deque<int> d2;
    d2.push_back(1);
    d2.push_back(2);
    d2.push_back(3);
    d1.insert(d1.begin(), d2.begin(), d2.end());
    printDeque(d1);

    //删除
    d1.erase(d1.begin());
    printDeque(d1);
    d2.erase(d2.begin(), d2.end());
    printDeque(d2);
    //清空
    d1.clear();
    printDeque(d1);
}

int main()
{
    testA();
    return 0;
}

deque数据存取

功能描述:

函数原型:

示例:

#include <iostream>
#include <string>

using namespace std;
#include <deque>

void testA()
{
    deque<int> d1;

    d1.push_back(10);
    d1.push_back(20);

    cout << d1.at(0) << endl;
    cout << d1[1] << endl;
    cout << d1.front() << endl;
    cout << d1.back() << endl;

}

int main()
{
    testA();
    return 0;
}

deque数据排序

功能描述:

算法:

#include <iostream>
#include <string>

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

void printDeque(const deque<int>& d)
{
    for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

void testA()
{
    deque<int> d1;

    d1.push_back(1);
    d1.push_back(3);
    d1.push_back(5);
    d1.push_back(7);
    d1.push_back(9);
    d1.push_back(2);
    d1.push_back(4);
    d1.push_back(6);
    d1.push_back(8);
    //排序
    sort(d1.begin(), d1.end());
    printDeque(d1);
}

int main()
{
    testA();
    return 0;
}

案例-评委打分

案例描述

有五名选手:选手ABCDE,10个评委分别对每一名选手打分,去除最高分,去除评委中最低分,取平均数。


案例步骤

  1. 创建五名选手,放到vector容器中。
  2. 遍历vector容器,取出来每一个选手执行for循环。可以把十个打分存到deque容器中
  3. sort算法对deque容器中分数排序,去除最高分和最低分
  4. deque容器遍历一遍,累加总分
  5. 获取平均分

示例:

#include <iostream>
using namespace std;
#include <string>
#include <vector>
#include <deque>
#include <algorithm>
#include <ctime>

class Person
{
public:
    string m_name;
    int m_score;

public:
    Person(string name, int score)
    {
        this->m_name = name;
        this->m_score = score;
    }
};

void createPerson(vector<Person>& v)
{
    string nameSeed = "ABCDE";
    for (int i = 0; i < 5; i++)
    {
        string name = "选手";
        name += nameSeed[i];

        int score = 0;
        Person p(name, score);
        v.push_back(p);
    }
}

void setScore(vector<Person> &v)
{
    for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
    {
        //将评委的分数放入deque容器中
        deque<int> d;
        for (int i = 0; i < 10; i++)
        {
            int scroe = rand() % 41 + 60;
            d.push_back(scroe);
        }
        //排序
        sort(d.begin(), d.end());
        //去除最高分和最低分
        d.pop_back();
        d.pop_front();
        //取平均分
        int sum = 0;
        for (deque<int>::iterator dit = d.begin(); dit != d.end(); dit++)
        {
            sum += *dit;
        }
        int avg = sum / d.size();
        //赋值
        it->m_score = avg;
    }

}

int main()
{
    //随机数种子
    srand((unsigned int)time(NULL));

    //1.创建5名选手
    vector<Person> v;
    createPerson(v);

    //2.给5名选手打分
    setScore(v);

    //3.显示最后得分
    for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << "姓名: " << it->m_name << " 分数: " << it->m_score << endl;
    }
    return 0;
}

stack容器(栈)

stack基本概念

概念:stack是一种先进后出(First In Last Out,FILO)的数据结构,它只有一个出口

stack容器

栈中只有顶部的元素才可以被外界使用,因此栈不允许有遍历行为

stack常用接口

功能描述:栈容器常用的对外接口


构造函数:

赋值操作:

数据存取:

大小操作:

示例:

#include <iostream>
using namespace std;
#include <string>
#include <stack>

void testA()
{
    stack<int> stk;
    stk.push(10);
    stk.push(20);
    stk.push(30);
    stk.push(40);

    cout << "栈大小为: " << stk.size() << endl;

    //只要栈不为空,查看栈顶,并执行出栈操作
    while (!stk.empty())
    {
        //栈顶元素
        cout << stk.top() << endl;
        //出栈
        stk.pop();
    }

    cout << "栈大小为: " << stk.size() << endl;
}

int main()
{
    testA();
    return 0;
}


queque容器(队列)

queue基本概念

概念: Queue是一种先进选出(First In Out,FIFO)的数据结构,它有两个出口

queue容器

队列容器允许从一端新增数据,从另一端移除元素

队列中只有队头和队尾才可以被外界使用, 因此队列不允许有遍历行为

队列中进数据称为 --- 入队 push

队列中出数据称为 --- 出队 pop

queue常用接口

功能描述:

赋值操作:

数据存取:

大小操作:

示例:

#include <iostream>
using namespace std;
#include <string>
#include <queue>

void testA()
{
    queue<int> que;
    que.push(10);
    que.push(20);
    que.push(30);
    que.push(40);

    //最后一个元素
    cout << "第一个元素: " << que.front() << endl;
    cout << "最后一个元素: " << que.back() << endl;

    cout << "栈大小为: " << que.size() << endl;

    //只要队列不为空,查看队里,并执行出队操作
    while (!que.empty())
    {
        //栈顶元素
        cout << que.front() << endl;
        //出栈
        que.pop();
    }

    cout << "栈大小为: " << que.size() << endl;
}

int main()
{
    testA();
    return 0;
}


list容器(链表)

list基本概念

功能: 将数据进行链式存储

链表: (list)是一种物理存储单元上非连续的存储结构, 数据元素的逻辑顺序是通过链表中的指针链接实现的


链表的组成: 链表由一系列节点组成


节点的组成: 一个是存储数据元素的数据域, 另一个是存储下一个节点地址的指针域


STL中的链表是一个双向循环链表

list容器

由于链表的存储方式并不是连续的内存空间, 因此链表list中的迭代器只支持前移后移, 属于双向迭代器


优点: 采用动态存储分配,不会造成内存浪费和溢出。可以对任意位置进行快速的插入和删除

缺点: 链表灵活,但是空间(指针域)和时间(遍历)额外开销较大。容器遍历速度, 没有数组快, 占用空间比数组大

list有一个重用的性质, 插入操作和删除操作都不会造成原有list迭代器的失效,这在vector中是不成立。


总结:STL中List和vector是连个最常被使用的容器,各有优缺点

list构造函数

功能描述: 创建list容器

函数原型:

示例:

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

void printList(list<int>& li)
{
    for (list<int>::iterator it = li.begin(); it != li.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

void testA()
{
    list<int> li;
    //添加数据
    li.push_back(10);
    li.push_back(20);
    li.push_back(30);
    li.push_back(40);
    //遍历容器
    printList(li);

    //区间构造
    list<int> l2(li.begin(), li.end());
    printList(l2);
    //拷贝构造
    list<int>l3(l2);
    printList(l3);
    list<int> l4(4, 1000);
    printList(l4);
}

int main()
{
    testA();
    return 0;
}

list赋值和交换

功能描述: 给list容器进行赋值,以及交换list容器

函数原型:

示例:

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

void printList(list<int>& li)
{
    for (list<int>::iterator it = li.begin(); it != li.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

void testA()
{
    list<int> li;
    //添加数据
    li.push_back(10);
    li.push_back(20);
    li.push_back(30);
    li.push_back(40);
    //遍历容器
    printList(li);
    list<int> l2;
    l2.assign(li.begin(), li.end());
    printList(l2);
    list<int> l3;
    l3.assign(4, 100);
    printList(l3);
    list<int> l4;
    l4 = l3; // operator=
    printList(l4);
}

//交换
void testB()
{
    list<int> li;
    //添加数据
    li.push_back(10);
    li.push_back(20);
    li.push_back(30);
    li.push_back(40);

    list<int> l2;
    l2.assign(10, 100);
    cout << "交换前: " << endl;
    printList(li);
    printList(l2);
    cout << "交换后: " << endl;
    li.swap(l2);
    printList(li);
    printList(l2);
}

int main()
{
    testA();
    testB();
    return 0;
}

list大小操作

功能描述: 对list容器的大小进行操作

函数原型:

示例:

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

void printList(list<int>& li)
{
    for (list<int>::iterator it = li.begin(); it != li.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

void testA()
{
    list<int> li;
    //添加数据
    li.push_back(10);
    li.push_back(20);
    li.push_back(30);
    li.push_back(40);

    //判断容器是否为空
    if (li.empty())
    {
        cout << "容器为空" << endl;
    }
    else
    {
        cout << "元素个数: " << li.size() << endl;
        //遍历容器
        printList(li);

    }
    //重新指定大小
    li.resize(10, 1000);
    printList(li);
    li.resize(5);
    printList(li);
}

int main()
{
    testA();
    return 0;
}

list插入和删除

功能描述:

函数原型:

示例:

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

void printList(const list<int>& li)
{
    for (list<int>::const_iterator it = li.begin(); it != li.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

void testA()
{
    list<int> li;
    //尾插
    li.push_back(10);
    li.push_back(20);
    li.push_back(30);
    li.push_back(40);
    //头插
    li.push_front(100);
    li.push_front(200);
    printList(li);

    //尾删
    li.pop_back();
    //头删
    li.pop_front();
    printList(li);
    //insert()插入
    li.insert(li.begin(), 1000);
    li.insert(li.begin(), 2, 10000);
    printList(li);
    //删除
    li.erase(li.begin());
    printList(li);
    //移除
    li.remove(1000);
    printList(li);
    //清空
    li.clear();
    printList(li);
}

int main()
{
    testA();
    return 0;
}


list数据存取

功能描述:

函数原型:

示例:

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

void testA()
{
    list<int> li;
    //尾插
    li.push_back(10);
    li.push_back(20);
    li.push_back(30);
    li.push_back(40);
    cout << li.front() << endl;
    cout << li.back() << endl;
}

int main()
{
    testA();
    return 0;
}


list反转和排序

功能描述:

函数原型:

示例:

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

void printList(const list<int>& li)
{
    for (list<int>::const_iterator it = li.begin(); it != li.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

void testA()
{
    list<int> li;
    //尾插
    li.push_back(20);
    li.push_back(10);
    li.push_back(40);
    li.push_back(30);
    printList(li);
    //反转
    li.reverse();
    printList(li);

    //排序
    li.sort(); //默认升序
    printList(li);
    //倒叙
    li.sort([](int v1, int v2) 
        {
            return v1 > v2;
        });
    printList(li);
    //所有不支持随机访问迭代器的容器,不支持标准算法
    //sort(li.begin(), li.end());
}

int main()
{
    testA();
    return 0;
}


排序案例

案例描述:将Person自定义数据类型进行排序,Person中属性有姓名、年龄、身高

排序规则:按照年龄进行升序,如果年龄相同按照身高进行降序

示例:

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

class Person
{
public:
    string m_name;//姓名
    int m_age;//年龄
    int m_height;//身高

    Person(string name, int age, int height)
    {
        this->m_age = age;
        this->m_height = height;
        this->m_name = name;
    }
};

void printList(const list<Person>& li)
{
    for (list<Person>::const_iterator it = li.begin(); it != li.end(); it++)
    {
        cout << "姓名:" << it->m_name << " 年龄: " << it->m_age << " 身高: " << it->m_height << endl;
    }
}

bool comparePerson(Person& p1, Person& p2)
{
    if (p1.m_age == p2.m_age)
    {
        return p1.m_height > p2.m_height;
    }
    else {
        // 按照年龄升序
        return p1.m_age < p2.m_age;
    }
}

void testA()
{
    list<Person> L;
    Person p1("刘备", 35, 175);
    Person p2("曹操", 45, 180);
    Person p3("孙权", 40, 170);
    Person p4("赵云", 25, 190);
    Person p5("张飞", 35, 160);
    Person p6("关羽", 35, 200);
    L.push_back(p1);
    L.push_back(p2);
    L.push_back(p3);
    L.push_back(p4);
    L.push_back(p5);
    L.push_back(p6);
    printList(L);

    //排序后
    cout << "排序后: " << endl;
    L.sort(comparePerson);
    printList(L);
}

int main()
{
    testA();
    return 0;
}


set/multiset容器(集合)

set基本概念

简介:

本质:

set和multiset区别:

set构造和赋值

功能描述: 创建set容器以及赋值

构造:

赋值:

示例:

#include <iostream>
using namespace std;
#include <string>
#include <set>

void printSet(const set<int>& li)
{
    for (set<int>::const_iterator it = li.begin(); it != li.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

void testA()
{
    set<int> s1;
    //插入数据 只有insert方式
    s1.insert(10);
    s1.insert(20);
    s1.insert(30);
    s1.insert(30);
    s1.insert(40);
    //插入元素会自动排序, 且set容器不允许有重复值
    printSet(s1);

    //拷贝构造
    set<int>s2(s1);
    printSet(s2);
    //赋值
    set<int>s3;
    s3 = s1;
    printSet(s3);
}

int main()
{
    testA();
    return 0;
}

set大小和交换

功能描述:

函数原型:

示例:

#include <iostream>
using namespace std;
#include <string>
#include <set>

void printSet(const set<int>& st)
{
    for (set<int>::const_iterator it = st.begin(); it != st.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

void testA()
{
    set<int> s1;
    //插入数据 只有insert方式
    s1.insert(10);
    s1.insert(20);
    s1.insert(30);
    s1.insert(30);
    s1.insert(40);

    if (s1.empty())
    {
        cout << "容器为空" << endl;
    }
    else {
        //插入元素会自动排序, 且set容器不允许有重复值
        printSet(s1);
        cout << "容器大小为: " << s1.size() << endl;
    }

    set<int> s2;
    s2.insert(100);
    s2.insert(200);
    s2.insert(300);
    s2.insert(400);
    cout << "交换前: " << endl;
    printSet(s1);
    printSet(s2);
    cout << "交换后: " << endl;
    s1.swap(s2);
    printSet(s1);
    printSet(s2);

}

int main()
{
    testA();
    return 0;
}


set插入和删除

功能描述:

函数原型:

示例:

#include <iostream>
using namespace std;
#include <string>
#include <set>

void printSet(const set<int>& st)
{
    for (set<int>::const_iterator it = st.begin(); it != st.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

void testA()
{
    set<int> s1;
    //插入数据 只有insert方式
    s1.insert(10);
    s1.insert(20);
    s1.insert(30);
    s1.insert(30);
    s1.insert(40);
    printSet(s1);

    // 删除
    s1.erase(30);
    printSet(s1);
    s1.erase(s1.begin());
    printSet(s1);
    // 清空
    s1.clear();
    printSet(s1);
}

int main()
{
    testA();
    return 0;
}


set查找和统计

功能描述:

函数原型:

示例:

#include <iostream>
using namespace std;
#include <string>
#include <set>

void testA()
{
    set<int> s1;
    //插入数据 只有insert方式
    s1.insert(10);
    s1.insert(20);
    s1.insert(30);
    s1.insert(30);
    s1.insert(40);

    //查找
    set<int>::iterator pos = s1.find(30);
    if (pos != s1.end())
    {
        cout << *pos << endl;
    }
    else {
        cout << "没找到元素" << endl;
    }

    //统计
    //对与set而言,统计结果要么是0 要么是1
    cout << s1.count(30) << endl;
}

int main()
{
    testA();
    return 0;
}


set和multiset区别

学习目标:

区别:

示例:

#include <iostream>
using namespace std;
#include <string>
#include <set>

void printSet(const multiset<int>& st)
{
    for (set<int>::const_iterator it = st.begin(); it != st.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

void testA()
{
    set<int> s1;
    //插入数据 只有insert方式
    pair<set<int>::iterator,bool> ret = s1.insert(10);
    if (ret.second)
    {
        cout << "插入成功" << endl;
    }
    else {
        cout << "插入失败" << endl;
    }
    ret = s1.insert(10);
    if (ret.second)
    {
        cout << "插入成功" << endl;
    }
    else {
        cout << "插入失败" << endl;
    }

    multiset<int> s2;
    s2.insert(10);
    s2.insert(10);
    s2.insert(10);
    s2.insert(10);
    printSet(s2);
}

int main()
{

    testA();
    return 0;
}


pair对组创建

功能描述:

两种创建方式:

示例:

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

void testA()
{
    //第一种方式
    pair<string, int> p("Tom", 20);
    cout << "姓名: " << p.first << " 年龄: " << p.second << endl;
    //第二种
    pair<string, int>p2 = make_pair("Jerry", 30);
    cout << "姓名: " << p2.first << " 年龄: " << p2.second << endl;
}

int main()
{
    testA();
    return 0;
}


set容器排序

学习目标:

主要技术的:

示例一 set存放内置数据类型

#include <iostream>
using namespace std;
#include <string>
#include <set>

class MyCompare
{
public:
    bool operator()(int v1,int  v2) const
    {
        return v1 > v2;
    }
};

void testA()
{
    set<int> s1;
    s1.insert(10);
    s1.insert(30);
    s1.insert(20);
    s1.insert(50);
    s1.insert(40);
    for (set<int>::iterator it = s1.begin(); it != s1.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;

    set<int,MyCompare> s2;

    s2.insert(10);
    s2.insert(30);
    s2.insert(20);
    s2.insert(50);
    s2.insert(40);
    for (set<int,MyCompare>::iterator it = s2.begin(); it != s2.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

int main()
{
    testA();
    return 0;
}

示例二 set存放自定义数据类型

#include <iostream>
using namespace std;
#include <string>
#include <set>

class Person
{
public:
    string m_name;
    int m_age;

    Person(string name, int age)
    {
        this->m_age = age;
        this->m_name = name;
    }
};

class MyCompare
{
public:
    bool operator()(const Person &v1, const Person &v2) const
    {
        return v1.m_age > v2.m_age;
    }
};

void testA()
{
    set<Person,MyCompare> s;
    Person p1("刘备", 24);
    Person p2("关羽", 28);
    Person p3("张飞", 25);
    Person p4("赵云", 21);

    s.insert(p1);
    s.insert(p2);
    s.insert(p3);
    s.insert(p4);

    for (set<Person, MyCompare>::iterator it = s.begin(); it != s.end(); it++)
    {
        cout << "姓名: " << it->m_name << " 年龄: " << it->m_age << endl;
    }
}

int main()
{
    testA();
    return 0;
}


map/multimap容器

map基本概念

简介:

本质:

优点:

map和multimap区别:

map构造和赋值

功能描述:

构造:

赋值:

示例:

#include <iostream>
using namespace std;
#include <string>
#include <map>

void printMap(map<int, int>& m)
{
    for (map<int,int>::iterator it = m.begin(); it != m.end(); it++)
    {
        cout << "key: " << it->first << " value: " << it->second << endl;
    }
}

void testA()
{
    map<int, int> m;

    m.insert(pair<int, int>(1, 10));
    m.insert(pair<int, int>(3, 30));
    m.insert(pair<int, int>(2, 20));
    m.insert(pair<int, int>(4, 40));

    printMap(m);

    //拷贝构造
    map<int, int> m2(m);
    printMap(m2);
    //赋值
    map<int, int> m3;
    m3 = m2;
    printMap(m3);
}

int main()
{
    testA();
    return 0;
}

map大小和交换

功能描述:

函数原型:

示例:

#include <iostream>
using namespace std;
#include <string>
#include <map>

void printMap(map<int, int>& m)
{
    for (map<int,int>::iterator it = m.begin(); it != m.end(); it++)
    {
        cout << "key: " << it->first << " value: " << it->second << endl;
    }
}

void testA()
{
    map<int, int> m;

    m.insert(pair<int, int>(1, 10));
    m.insert(pair<int, int>(3, 30));
    m.insert(pair<int, int>(2, 20));
    m.insert(pair<int, int>(4, 40));

    if (m.empty())
    {
        cout << "空的" << endl;
    }
    else {
        cout << "容器大小为: " << m.size() << endl;

    }

    //交换容器
    map<int, int> m1;

    m1.insert(pair<int, int>(5, 10));
    m1.insert(pair<int, int>(6, 30));
    m1.insert(pair<int, int>(7, 20));
    m1.insert(pair<int, int>(8, 40));
    cout << "交换前:  " << endl;
    printMap(m);
    printMap(m1);
    cout << "交换前:  " << endl;
    m.swap(m1);
    printMap(m);
    printMap(m1);
}

int main()
{
    testA();
    return 0;
}

map插入和删除

功能描述:

函数原型:

示例:

#include <iostream>
using namespace std;
#include <string>
#include <map>

void printMap(map<int, int>& m)
{
    for (map<int,int>::iterator it = m.begin(); it != m.end(); it++)
    {
        cout << "key: " << it->first << " value: " << it->second << endl;
    }
}

void testA()
{
    map<int, int> m;
    //插入
    //第一种
    m.insert(pair<int, int>(1, 10));
    m.insert(pair<int, int>(3, 30));
    m.insert(pair<int, int>(2, 20));
    m.insert(pair<int, int>(4, 40));
    //第二种
    m.insert(make_pair(5, 50));
    //第三种
    m.insert(map<int, int>::value_type(6, 60));
    //第四种
    m[7] = 70;
    //删除
    m.erase(m.begin());
    m.erase(4);
    printMap(m);
    //清空
    m.erase(m.begin(), m.end());
    m.clear();
    printMap(m);
}

int main()
{
    testA();
    return 0;
}

map查找和统计

功能描述:

函数原型:

示例:

#include <iostream>
using namespace std;
#include <string>
#include <map>

void testA()
{
    map<int, int> m;
    //插入
    m.insert(pair<int, int>(1, 10));
    m.insert(pair<int, int>(3, 30));
    m.insert(pair<int, int>(2, 20));
    m.insert(pair<int, int>(4, 40));

    map<int, int>::iterator it = m.find(3);
    if (it != m.end())
    {
        cout << "key: " << it->first << " value: " << it->second << endl;
    }
    else {
        cout << "没找到" << endl;
    }
    //对于map来说,要么是1,要么是0,map不允许插入重复的key
    cout << m.count(2) << endl;

    multimap<int, int> m1;
    m1.insert(pair<int, int>(1, 10));
    m1.insert(pair<int, int>(1, 10));
    cout << m1.count(1) << endl;
}

int main()
{
    testA();
    return 0;
}

map容器排序

学习目标:

主要技术点:

示例:

#include <iostream>
using namespace std;
#include <string>
#include <map>

class MyCompare
{
public:
    bool operator()(int v1, int v2) const
    {
        return v1 > v2;
    }
};

void printMap(map<int, int, MyCompare>& m)
{
    for (map<int,int, MyCompare>::iterator it = m.begin(); it != m.end(); it++)
    {
        cout << "key: " << it->first << " value: " << it->second << endl;
    }
}

void testA()
{
    map<int, int, MyCompare> m;
    //插入
    m.insert(pair<int, int>(1, 10));
    m.insert(pair<int, int>(3, 30));
    m.insert(pair<int, int>(2, 20));
    m.insert(pair<int, int>(4, 40));
    cout << "排序后:" << endl;
    printMap(m);
}

int main()
{
    testA();
    return 0;
}

案例-员工分组

案例描述

实现步骤

  1. 创建10名员工,放到vector中
  2. 遍历vector容器,取出每个员工,进行随机分组
  3. 分组后将员工部门编号作为key,具体员工作为value,放入到multimap容器中
  4. 分部门显示员工信息

案例代码:

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

#define CHEHUA 0
#define MEISHU 1
#define YANFA 2

class Worker
{
public:
    string m_name;
    int m_salary;
};

void createWorker(vector<Worker>& v)
{
    string nameSeed = "ABCDEFGHIJ";
    for (int i = 0; i < 10; i++)
    {
        Worker worker;
        worker.m_name = "员工";
        worker.m_name += nameSeed[i];

        worker.m_salary = rand() % 10000 + 10000; //10000~19999

        v.push_back(worker);
    }
}

void setGroup(vector<Worker>& v, multimap<int, Worker>& m)
{
    for (vector<Worker>::iterator it = v.begin(); it != v.end(); it++)
    {
        //产生随机部门编号
        int deptId = rand() % 3;//0 1 2

        m.insert(make_pair(deptId, *it));
    }
}

void showWorkerByGroup(multimap<int, Worker>& m)
{
    cout << "策划部门:" << endl;
    multimap<int, Worker>::iterator pos = m.find(CHEHUA);
    int count =  m.count(CHEHUA);
    int index = 0;
    for (; pos != m.end()&&index < count; pos++,index++)
    {
        cout << "姓名:" << pos->second.m_name << " 工资: " << pos->second.m_salary << endl;

    }
    cout << "美术部门:" << endl;
    pos = m.find(MEISHU);
    count = m.count(MEISHU);
    index = 0;
    for (; pos != m.end() && index < count; pos++, index++)
    {
        cout << "姓名:" << pos->second.m_name << " 工资: " << pos->second.m_salary << endl;

    }
    cout << "研发部门:" << endl;
    pos = m.find(YANFA);
    count = m.count(YANFA);
    index = 0;
    for (; pos != m.end() && index < count; pos++, index++)
    {
        cout << "姓名:" << pos->second.m_name << " 工资: " << pos->second.m_salary << endl;

    }
}

void printWorker(vector<Worker>& v)
{
    for (vector<Worker>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << "姓名:" << it->m_name << " 工资: " << it->m_salary << endl;
    }
}

int main()
{
    srand((unsigned int)time(NULL));

    //1.创建10名员工,放到vector中
    vector<Worker> vWorker;
    createWorker(vWorker);
    //printWorker(vWorker);

    //2.遍历vector容器,取出每个员工,进行随机分组
    //3.分组后将员工部门编号作为key,具体员工作为value,放入到multimap容器中
    multimap<int, Worker> mWorker;
    setGroup(vWorker, mWorker);
    //4.分部门显示员工信息
    showWorkerByGroup(mWorker);

    return 0;
}

标签: C/C++ c++提高编程