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;
}
运行结果显示如下
相关推荐
charlie1145141915 分钟前
深探std::vector:三指针、扩容与迭代器失效
开发语言·c++·开源项目
看我眼色行事^ \/ ^14 分钟前
2024.05.11 360春招WEB前端编程题
算法
laplaya15 分钟前
ROS常用消息之PointCloud2
c++
一只肥瘫瘫25 分钟前
编码器 PLL 角度与速度观测器原理
人工智能·算法
asyxchenchong88831 分钟前
R语言混合效应(多水平/层次/嵌套)模型及贝叶斯实现技术应用
开发语言·r语言
熊猫钓鱼>_>38 分钟前
从“串数据“焦虑到 Space 自由,我用 Agent Bucket 智能体桶管游戏素材
开发语言·人工智能·游戏·agent·bucket·workbuddy·space
Nil2081 小时前
leetcode 146LRU缓存
算法·leetcode·缓存
沙盘客1 小时前
典型应用与趋势:作战实验、AI 决策与数字孪生
c++·人工智能·经验分享
heimeiyingwang1 小时前
【架构实战】Kubernetes网络模型深度解析:从Pod通信到Ingress网关实战
开发语言·架构·php