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;
}
运行结果显示如下
相关推荐
取加若则_7 小时前
Linux进程调度:双队列高效管理
linux·算法
维度攻城狮9 小时前
ros2参数通信案例
开发语言·windows·python·ros2·参数通信
Tisfy9 小时前
LeetCode 961.在长度 2N 的数组中找出重复 N 次的元素:5种语言x5种方法(及其变种) —— All By Hand
数据结构·数学·算法·leetcode·题解
清水白石0089 小时前
Python 与尾递归:为何不优化?如何优雅绕过?
开发语言·python
王大傻09289 小时前
使用python for循环与ord() + chr()实现字符串加密
开发语言·python
蜕变的土豆9 小时前
vcpkg使用教程
c++
小O的算法实验室9 小时前
2024年ESWA SCI1区TOP,容错文化概率粒子群算法+多 AGV 路径规划,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
Louis Maos9 小时前
堆与栈分配的本质区别
java·开发语言
略无慕艳意10 小时前
C++ 中的 vector
c++
WW_千谷山4_sch10 小时前
洛谷P1120&UVA307 [CERC 1995] 小木棍
c++·算法·深度优先