STL 中把list中的数据transform到set中会报错
[code=C/C++][/code]
#include <iostream>
#include <iterator>
#include <algorithm>
#include <string>
#include <set>
using namespace std;
string punctRemover(const string& strIn); // fuction prototype
string addstr(string& str){return str += "asdf";}
int main()
{
string str("h*\'e&l\)\"l#o*");
str = addstr(str);
cout << str << endl;
set<string> set1;
set<string> set2;
set<string>::iterator it;
set1.insert("h@ello");
set1.insert("Hellp");
set2.
//transform(set1.begin(),set1.end(),set2.begin(),punctRemover);
transform(set1.begin(),set1.end(),inserter(set2,set2.begin()),addstr);
//transform(set1.begin(),set1.end(),set2.begin(),addstr);
for(it = set1.begin(); it != set1.end(); ++it)
{
cout << *it << endl;
}
for(it = set2.begin(); it != set2.end(); ++it)
{
cout << *it << endl;
}
return 0;
}
/*
void addstr(string& str)
{
string temp("asdf");
str += temp;
}
*/
string punctRemover(const string& strIn)
{
string temp;
for(int i = 0; i < strIn.size();++i)
{
if(!ispunct(strIn[i]))
temp += strIn[i];
}
return temp;
}
error: class set has no member named 'transform' 如何解决呢??
怎么放到set中呢??求解啊~~
[解决办法]
transform不是set的成员函数,但是它是可以直接使用的嘛。只要包含了库了就可以直接使用了。
- C/C++ code
string punctRemover(const string& strIn); // fuction prototypestring addstr(string& str){return str += "asdf";}int main(){ string str("h*\'e&l\)\"l#o*"); str = addstr(str); cout << str << endl; set<string> set1; set<string> set2; set<string>::iterator it; set1.insert("h@ello"); set1.insert("Hellp"); //transform(set1.begin(),set1.end(),set2.begin(),punctRemover); transform(set1.begin(),set1.end(),inserter(set2,set2.begin()),addstr); //transform(set1.begin(),set1.end(),set2.begin(),addstr); for(it = set1.begin(); it != set1.end(); ++it) { cout << *it << endl; } for(it = set2.begin(); it != set2.end(); ++it) { cout << *it << endl; } return 0;}/*void addstr(string& str){ string temp("asdf"); str += temp;}*/string punctRemover(const string& strIn){ string temp; for(int i = 0; i < strIn.size();++i) { if(!ispunct(strIn[i])) temp += strIn[i]; } return temp;}