我正在尝试返回std::pair
具有int
和string
值的内容。我应该保留什么函数的返回类型?
我尝试了两者int
和char
返回类型,但都给出了错误。我在下面给出了错误:
#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;
}