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;
}
运行结果显示如下
相关推荐
冻柠檬飞冰走茶14 小时前
PTA基础编程题目集 7-35有理数均值(C++语言实现)
开发语言·数据结构·c++·算法·均值算法
wangchen_015 小时前
C++正则表达式
开发语言·c++·正则表达式
何以解忧,唯有..15 小时前
Python 元组(tuple)详解:使用、遍历与排序
开发语言·python
KANGBboy15 小时前
Python eval安全隐患
开发语言·python
IT爱学堂15 小时前
java版数据结构和算法+AI算法和技能学习指南
java·开发语言
民乐团扒谱机15 小时前
【微实验】倒谱算法(Cepstrum)深度解析:原理、数学推导与代码实现
人工智能·算法·语音识别
淼澄研学16 小时前
Python结合大模型挖掘搜题长尾关键词实操指南
开发语言·python
Nil20816 小时前
leetcode 189轮转数组
数据结构·算法·leetcode
FfHUCisI16 小时前
Golang - 信号量模式(Semaphore Pattern)
开发语言·后端·golang
-dzk-16 小时前
【动态规划】LC 118.杨辉三角
算法·动态规划