C++ //练习 11.9 定义一个map,将单词与一个行号的list关联,list中保存的是单词所出现的行号。

C++ Primer(第5版) 练习 11.9

练习 11.9 定义一个map,将单词与一个行号的list关联,list中保存的是单词所出现的行号。

环境:Linux Ubuntu(云服务器)
工具:vim
代码块
cpp 复制代码
/*************************************************************************
	> File Name: ex11.9.cpp
	> Author: 
	> Mail: 
	> Created Time: Wed 03 Apr 2024 08:59:10 AM CST
 ************************************************************************/

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

int main(){
    map<string, list<int>> words;
    list<int> num = {1, 2, 3, 4, 5, 6};
    string word = "compare";

    for(const auto n : num){
        words[word].push_back(n);
    }

    cout<<"words    Line"<<endl;
    for(const auto w: words){
        for(const auto l : w.second){
            cout<<w.first<<"    "<<l<<endl;
        }
    }

    return 0;
}
运行结果显示如下
相关推荐
c238569 分钟前
Bug 猎手入门指南
c++·算法·bug
chouchuang26 分钟前
day-030-综合练习-笔记管理器
开发语言·笔记·python
Reart37 分钟前
Leetcode 213.打家劫舍2(内含闲谈,打劫真是技术活,好题,716)
后端·算法
云空1 小时前
《Three.js 3D实例大全》
开发语言·javascript·3d·three.js
techdashen1 小时前
Go 1.26 新增 `bytes.Buffer.Peek`:只看数据,不移动读取位置
开发语言·后端·golang
Reart2 小时前
Leetcode 198.打家劫舍(716)
后端·算法
Jerry2 小时前
LeetCode 110. 平衡二叉树
算法
C137的本贾尼2 小时前
第七篇:消息队列(MQ)——就是个带存储的异步通信管道
java·开发语言·中间件
玖玥拾2 小时前
C++ 数据结构 八大基础排序算法专题
数据结构·c++·算法·排序算法
Tim_102 小时前
【C++】017、new/delete与malloc/free的区别
java·数据结构·算法