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;
}
运行结果显示如下
相关推荐
Navigator_Z1 分钟前
LeetCode //C - 1328. Break a Palindrome
c语言·算法·leetcode
电科_银尘9 分钟前
【大模型算法与工程 46】vLLM部署入门:模型服务、请求与运行观测
算法·vllm
NendoZen12 分钟前
Bubble Tea V2构建Go TUI应用教程
开发语言·golang·xcode
AskHarries42 分钟前
社区运营打法
算法
pan_junbiao1 小时前
Go 语言变量的声明与初始化
开发语言·javascript·golang
泡海椒1 小时前
Java 生成接口调用量趋势 PDF:jquick-pdf 多重折线图(JChartType.MultipleLine)落地
java·开发语言·pdf
syf0611131 小时前
Java 多线程编程:从原理到实战
java·开发语言
敲上瘾11 小时前
Qt 主窗口QMainWindow:菜单栏、工具栏、状态栏与浮动窗口等
开发语言·c++·qt·ui·qt6.3