我有這個讀取輸入行&的小程序打印其中的單詞以及它們各自的出現次數。我想根據它們的出現對地圖中存儲這些值的元素進行排序。我的意思是,只出現一次的單詞將被命令在開頭,然後是出現兩次的單詞等等。我知道謂詞應該返回一個bool值,但我不知道參數應該是什麼。它應該是兩個迭代器到地圖嗎?如果有人能解釋這一點,將不勝感激。先謝謝你。從字符串到int的映射的謂詞
#include<iostream>
#include<map>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::map;
int main()
{
string s;
map<string,int> counters; //store each word & an associated counter
//read the input, keeping track of each word & how often we see it
while(cin>>s)
{
++counters[s];
}
//write the words & associated counts
for(map<string,int>::const_iterator iter = counters.begin();iter != counters.end();iter++)
{
cout<<iter->first<<"\t"<<iter->second<<endl;
}
return 0;
}
我將添加有一個簡明的語法要做到這一點:`的std ::矢量<性病::對> V(counters.begin(),counters.end()) ;` –
2011-02-07 15:37:27