transform函数问题,求解答
程序代码:
#include <iostream> #include <string> #include <cstdlib> #include <vector> #include <set> #include <map> #include <iterator> #include <algorithm> #include <cctype> using namespace std; char ToLower (char ch) { return tolower (ch); } string & ToLower (string & st); void Display (const string & s); int main () { vector<string> words; vector<string> temps; string temp; cout << "Enter words (enter quit to quit) : " << endl; //输入单词,输入quit退出输入,将输入的单词插入到vector容器中; while (cin >> temp && temp != "quit") { words.push_back (temp); } //使用算法for_each ()来显示vector容器中的内容; cout << "Now show the words you just entered : " << endl; for_each (words.begin (),words.end (),Display); cout << endl; //使用transform方法,对每个vector容器中的元素进行ToLower ()函数 并插入到set集合中 set<string> wordset; temps.resize (words.size ()); transform (words.begin (),words.end (),temps.begin (),ToLower);//代码来自c++ primer plus,此条无法编译通过。 vector<string>::iterator it; for (it = words.begin ();it != words.end ();it ++) { wordset.insert (*it); } //使用算法for_each()输出集合中每个元素; cout << "Alphabetic list of words : " << endl; for_each (wordset.begin (),wordset.end (),Display); cout << endl; //将set容器中的元素复制到map关联容器中; map<string,int> wordmap; set<string>::iterator iter1; for (iter1 = wordset.begin ();iter1 != wordset.end ();iter1 ++) { wordmap.insert (pair <string,int> (*iter1,count (words.begin (),words.end (),*iter1))); } //统计单词出现次数; for (iter1 = wordset.begin ();iter1 != wordset.end ();iter1 ++) { wordmap[*iter1] = count (words.begin (),words.end (),*iter1); } //显示各个单词的出现频率; for (iter1 = wordset.begin ();iter1 != wordset.end ();iter1 ++) { cout << *iter1 << " " << wordmap[*iter1] << endl; } system ("pause"); return 0; } string & ToLower (string & st) { transform (st.begin (),st.end (),st.begin (),tolower); return st; } void Display (const string & s) { cout << s << " " ; }
求大神解释一下