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;
}
运行结果显示如下
相关推荐
.千余15 分钟前
【C++】手写双向链表:list容器模拟实现
开发语言·c++·笔记·学习·其他
QuZero20 分钟前
Guava Cache Deep Dive
java·后端·算法·guava
随意起个昵称26 分钟前
线性dp-LIS题目4(A Twisty Movement)
算法·动态规划
liulilittle36 分钟前
过冲:拥塞控制的呼吸与盲行
linux·网络·c++·tcp/ip·计算机网络·tcp·通信
Felven36 分钟前
B. Fair Numbers
数据结构·算法
人道领域40 分钟前
【LeetCode刷题日记】93.复原IP地址
java·开发语言·算法·leetcode
caimouse40 分钟前
Reactos 第 3 章 内存管理 — 【中篇】Hyperspace、系统空间、API 与异常
c语言·开发语言·windows·架构
jarreyer1 小时前
【算法记录1】模型训练问题
算法
Felven1 小时前
D. Friends and the Restaurant
算法
摇滚侠1 小时前
JavaWeb 全套教程 Listener 112-113
java·开发语言·servlet·tomcat·intellij-idea