3.9

天梯赛 L2-49 鱼与熊掌

image

set 集合 unordered_set无序集合

// 构建首字母缩写到句子列表的映射
1.unordered_map<string, vector<string>> ini_to_str;

for (int i = 0; i < N; ++i)
{
string s;
getline(cin, s);
string initial = suo(s);
ini_to_str[initial].push_back(s);
}

// 原代码中的 auto 用法 unordered_map<string, int> mp;

auto it = mp.find("李四"); // auto 推导 it 的类型为 unordered_map<string, int>::iterator

image

 

2.vector<unordered_set<int>> st(n + 1); 并不是二维数组  ——》数组里套集合”

自动去重!

一个一维的 vector 容器,但这个 vector 里的每个元素不是普通的 int/string,而是一个unordered_set<int>(无序集合)

image

不是所有带 .begin()/.end() 的场景都是 “复制数值”,核心取决于你调用的函数 / 构造函数的逻辑

image

 

读入有空格的字符串:

string sentence; // 读取一整行输入(直到回车为止,自动忽略最后的回车符)

getline(cin, sentence);

 

总结一下vector,map,stack,set等容器的的核心成员函数:

image

 

image

 

image

 

image

image

 

 

image

 

image

 

posted @ 2026-03-09 21:54  ervgiu28hg34y827  阅读(6)  评论(0)    收藏  举报