std::multimap 功能
时间: 2023-04-11 16:01:02 浏览: 110
std::multimap 是 C++ STL 中的一个容器,它可以存储多个键值相同的元素,并且可以按照键值进行排序。它的功能类似于 std::map,但是 std::map 只能存储一个键值对应的元素,而 std::multimap 可以存储多个。
相关问题
#include <iostream> #include <map> #include <vector> #include <algorithm> void brotherWord(std::vector<std::string> strVect, std::string word, int k) { std::multimap<std::string, int> rtnMap; //存放是兄弟单词的容器 std::vector<std::string>::iterator iter = strVect.begin(); while(iter != strVect.end()) { std::string curWord = *iter; if(curWord != word) //兄弟单词不能一模一样 { std::string tempword = word; std::sort(tempword.begin(), tempword.end()); std::string tempcurword = curWord; std::sort(tempcurword.begin(), tempcurword.end()); if(tempcurword == tempword) //是兄弟单词 { rtnMap.insert(std::pair<std::string, int>(curWord, 1)); } } iter++; } // print(rtnMap); std::vector<std::string> rtnVect; std::multimap<std::string, int>::iterator iterMap = rtnMap.begin(); while(iterMap != rtnMap.end()) { rtnVect.push_back(iterMap->first); iterMap++; } std::cout << rtnVect.size() << std::endl; if(rtnVect.size() > 1) std::cout << rtnVect[k-1] << std::endl; } int main() { int n; std::cin >> n; std::vector<std::string> strVect; while(n--) { std::string temp; std::cin >> temp; strVect.push_back(temp); } std::string word; std::cin >> word; int k; std::cin >> k; brotherWord(strVect, word, k); return 0; }注释
这段代码是一个查找兄弟单词的程序。它首先从输入中读取一个整数n,表示要输入的单词数量。然后依次读取n个单词,存储在一个名为strVect的字符串向量中。接下来,程序读取一个单词word和一个整数k。函数brotherWord将在strVect中查找与word是兄弟单词的单词,并将它们存储在一个名为rtnVect的字符串向量中。如果rtnVect中的单词数量大于1,则输出第k个单词。
整体来说,这段代码实现了一个简单的兄弟单词查找功能。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)