c++11 std 之美

第一次感觉到了程序之美.
就是下面这个简单的代码.

#include <map>
#include <set>
#include <string>
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <sstream>

int main(int argc, char *argv[])
{
    std::map<std::string, std::vector<std::string>> families{
        {"Ding", {"red", "green", "blue"}},
        {"Wang", {"white"}},
    };

    std::cout << "add new family:" << std::endl;

    std::string line;
    while (std::getline(std::cin, line))
    {
        std::stringstream sst(line);
        std::istream_iterator<std::string> ist(sst), iste;
        const auto la = *ist;
        std::copy(++ist, iste, std::back_inserter(families[la]));
    }
    std::cout << std::endl;
    for (auto &last : families)
    {
        std::cout << last.first << " -> " << std::ends;
        std::for_each(last.second.cbegin(), last.second.cend(), [](const std::string &first) { std::cout << first << " "; });
        std::cout << "\t#" << std::endl;
    }

    return 0;
}

实现逻辑不复杂,就是在原有的map 里面去添加新的内容.输入来自控制台.每行对应一个 pair<string,vector> , 每行第一个元素给 pair.first, 后面的元素给到 pair.second.

看看实现,非常的巧妙.

  1. 在 getline 之后,通过 stringstream 把 string 转成 stream.
  2. 通过 copy 操作,用插入迭代器 直接从 stringstream 赋值到 vector. 同时,提取获取 stringstream 第一个参数作为 map 的key, 完成 map 的新元素添加.
  3. 通过范围 for 遍历出 map 的 pair.
  4. 再通过 for_each + lambda 直接输出 vector 的内容.

以上程序,感觉最好的地方就是下面这两行:

 std::copy(++ist, iste, std::back_inserter(families[la])); // 一行代码实现多个操作

std::for_each(last.second.cbegin(), last.second.cend(), [](const std::string &first) { std::cout << first << " "; }); // lambda 简化操作
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值