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;
}
运行结果显示如下
相关推荐
wefg17 小时前
【数据结构】红黑树
数据结构·算法
饼瑶7 小时前
基于AutoDL远端服务复现具身智能论文OpenVLA
算法
Mr.Winter`7 小时前
无人船 | 图解基于MPC控制的路径跟踪算法(以全驱动无人艇WAMV为例)
人工智能·算法·机器人·自动驾驶·ros·路径规划
咪咪渝粮8 小时前
112.路径总和
java·数据结构·算法
IoT智慧学堂8 小时前
C语言运算符与表达式详解——算术、逻辑与赋值的全面理解
c语言·开发语言
高洁018 小时前
大模型-详解 Vision Transformer (ViT) (2
深度学习·算法·aigc·transformer·知识图谱
电子_咸鱼8 小时前
高阶数据结构——并查集
数据结构·c++·vscode·b树·python·算法·线性回归
Helibo448 小时前
C++pair学习笔记
c++·笔记·学习
沐知全栈开发8 小时前
深度优先遍历与连通分量
开发语言