gpt4 book ai didi

c++ - 如何返回std::pair的内容?

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:26:51 31 4
gpt4 key购买 nike

我正在尝试返回具有 intstring 值的 std::pair 的内容。我应该保留什么函数返回类型?

我尝试同时使用 intchar 返回类型,但都给出了错误。我在下面给出了错误:

#include<iostream>
#include<algorithm>
#include<map>
#include<vector>

std::pair<int,std::string>client()
{

std::vector<std::string> most { "lion","tiger","kangaroo",
"donkey","lion","tiger",
"lion","donkey","tiger"
};
std::map<std::string, int> src;
for(auto x:most)
++src[x];

std::multimap<int,std::string,std::greater<int> > dst;

std::transform(src.begin(), src.end(), std::inserter(dst, dst.begin()),
[] (const std::pair<std::string,int> &p) {
return std::pair<int,std::string>(p.second, p.first);
}
);

std::multimap<int,std::string>::iterator it = dst.begin();

for(int count = 0;count<3 && it !=dst.end();++it,++count)
std::cout<<it->second<<":"<<it->first<<std::endl;

return *it;
}

int main()
{
std::multimap<int,std::string>::const_iterator rec;
rec= client(); // Error no match for ‘operator=’ in ‘rec = client()()’
std::multimap<int,std::string>::iterator it = rec.begin(); //error: ‘std::multimap<int, std::basic_string<char> >::const_iterator’ has no member named ‘begin’

for(int count = 0;count<3 && rec !=it.end();++it,++count) // error: has no member named 'end'
std::cout<<rec->second<<":"<<rec->first<<std::endl;

}

最佳答案

只需返回 intstd::string 本身因为 multimap 包含它作为它的元素

std::pair<int,std::string> client(){
//...
}

关于c++ - 如何返回std::pair的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18559452/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com