C++学习笔记——(八)map容器、for_each、预定义函数

注:编码工具是CLion+Cygwin64

目录

map

multimap

for_each

对象存入后和从容器中取出的细节

预定义函数


map

        map会对key进行排序。

#include <iostream>

using namespace std;

// 先导入头文件
#include <map>

int main(){
    map<int, string> m;
    // map的集中插入元素的方式
    m.insert(pair<int, string>(100, "百"));
    m.insert(make_pair(1, "一"));
    m.insert(map<int, string>::value_type(10, "十"));
    m[1000] = "千";
    // map不能通过insert方式插入重复的key
    auto it = m.insert(make_pair(100, "百川东到海"));
    if(it.second){
        cout << "覆盖成功" << endl;
    }else{
        cout << "覆盖失败" << endl;
    }
    // 通过迭代器遍历map
    for(auto it = m.begin(); it != m.end(); it ++){
        cout << it->first << ", " << it->second << endl;
    }
    printf("\n");
    // 覆盖成功,修改了1000对应的值
    m[1000] = "千山万水";
    for(auto it = m.begin(); it != m.end(); it ++){
        cout << it->first << ", " << it->second << endl;
    }

    // 通过find函数可以查找key对应的值
    map<int, string>::iterator find_it = m.find(99);
    if(find_it != m.end())
    {
        cout << "map中函数key为99的键值对:" << find_it->first << ", " << find_it->second << endl;
    }
    find_it = m.find(1000);
    if(find_it != m.end())
    {
        cout << "map中函数key为1000的键值对:" <<  find_it->first << ", " << find_it->second << endl;
    }
    return 0;
}

输出:

覆盖失败
1, 一
10, 十
100, 百
1000, 千

1, 一
10, 十
100, 百
1000, 千山万水
map中函数key为1000的键值对:1000, 千山万水

multimap

        和map功能类似,可以存储多个key相同的键值对。鉴于这一特性,可以用于分组。

#include <iostream>

using namespace std;

// 先导入头文件
#include <map>

int main(){
    multimap<int, string> m;

    m.insert(make_pair(1, "一"));
    m.insert(make_pair(2, "贰"));
    m.insert(make_pair(3, "三"));
    m.insert(make_pair(2, "二"));
    m.insert(make_pair(1, "壹"));

    for(auto it = m.begin(); it != m.end(); it ++)
    {
        cout << it->first << ", " << it->second << endl;
    }
    cout << endl;

    cout << "查找key为2的键值对并打印" << endl;
    auto it = m.find(2);
    while(it != m.end())
    {
        cout << it->first << ", " << it->second << endl;
        it ++;
    }
    cout << endl << "只输出key为2的键值对" << endl;
    it = m.find(2);
    while(it != m.end() && it->first == 2)
    {
        cout << it->first << ", " << it->second << endl;
        it ++;
    }
    return 0;
}

输出:

1, 一
1, 壹
2, 贰
2, 二
3, 三

查找key为2的键值对并打印
2, 贰
2, 二
3, 三

只输出key为2的键值对
2, 贰
2, 二

for_each

        要使用for_each,先要导入头文件。

        用for_each遍历容器元素,要传入一个谓词,这个谓词可以是一个仿函数,也可以是一个普通函数。

        仿函数是指像调用函数一样调用对象,需要重载类的圆括号运算符。

#include <iostream>

using namespace std;

// 先导入头文件
#include <map>
// 算法包
#include <algorithm>

// 仿函数
class PrintElement {
public:
    void operator()(pair<int, int> element){
        cout << element.first << ", " << element.second << endl;
    }
};

// 普通函数
void printElement(pair<int, int> element){
    cout << element.first << ", " << element.second << endl;
}

int main() {
    map<int, int> m;
    m[1] = 1;
    m[2] = 2;
    m[3] = 3;

    cout << "用仿函数遍历map" << endl;
    for_each(m.begin(), m.end(),PrintElement());
    cout << endl << "用普通函数遍历map" << endl;
    for_each(m.begin(), m.end(), printElement);
    return 0;
}

输出:

用仿函数遍历map
1, 1
2, 2
3, 3

用普通函数遍历map
1, 1
2, 2
3, 3

对象存入后和从容器中取出的细节

        对象存入容器时,会调用拷贝构造函数创建副本存入容器;
        从容器中获取对象时,也会调用拷贝构造函数创建副本。

#include <iostream>

using namespace std;

// 先导入头文件
#include <vector>

class Man {
public:
    string name;

    Man(string name) : name(name) {}

    Man(const Man & man)
    {
        this->name = man.name;
        cout << "调用了拷贝构造函数" << endl;
    }

    ~Man(){
        cout << "调用了析构函数" << endl;
    }
};



int main() {
    vector<Man> v;
    Man man("name");
    v.push_back(man);
    Man man2 = v.front();
    man.name = "张三";
    cout << "man2.name = " << man2.name << endl;
    return 0;
}

输出:

调用了拷贝构造函数
调用了拷贝构造函数
man2.name = name
调用了析构函数
调用了析构函数
调用了析构函数

预定义函数

        在算法包里有一些预定义函数,也称为内置函数,可以直接使用。

#include <iostream>

using namespace std;

#include <algorithm>

int main(){

    plus<int> p;
    cout << "100 + 100 = " << p(100, 100) << endl;
    plus<string> p2;
    cout << "\"100\" + \"100\" = " << "\"" << p2("100", "100")<< "\"" << endl;
    minus<int> p3;
    cout << "789 - 123 = " << p3(789, 123) << endl;
    multiplies<int> p4;
    cout << "10000 x 10000 = " << p4(10000, 10000) << endl;
    divides<double> p5;
    cout << "3 ÷ 2 = " << p5(3, 2) << endl;
    return 0;
}

输出:

100 + 100 = 200
"100" + "100" = "100100"
789 - 123 = 666
10000 x 10000 = 100000000
3 ÷ 2 = 1.5

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值