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;
}
运行结果显示如下
相关推荐
血小板要健康3 分钟前
队列 + 宽搜(BFS):二叉树层序遍历 算法总结
java·数据结构·笔记·算法·leetcode·宽度优先
Felven8 分钟前
A. Riptide
算法·c 算法
雪芽蓝域zzs10 分钟前
第十六节:递归组件实现无限层级折叠侧边栏菜单
开发语言·javascript·ecmascript
m0_7393128715 分钟前
四元数、李群SO(3)/李代数so(3)的作用及应用场景
算法·机器人·自动驾驶
神明不懂浪漫37 分钟前
【第二章】库、表、增删改查操作
开发语言·数据库·经验分享·笔记
传奇开心果编程38 分钟前
【Rust入门知识点学与练】第11课:HashMap 键值对集合
开发语言·学习·rust
artificiali1 小时前
880 第4章多元
人工智能·算法
yume_sibai1 小时前
03-Rust 函数式编程特性(闭包 + Iterator + Option/Result + 链式调用)
开发语言·后端·rust
xxwxx__1 小时前
深入理解 C++ 继承:从基础语法到底层原理全解析
c++
Felven1 小时前
B. Deja Vu
数据结构·算法