讨教一个关于stl方面的问题
时间:2008-07-24 14:55:10
来源:论坛整理 作者: 编辑:chinaitzhe
.......
.......
string str;
vector <string> vect;
transform(str.begin(),str.end,vect.end(),fun());//fun暂时不表
.......
........
编译的时候报错,我想问一下各位达人,是不是transform中指向的容器类型必须相同才行啊?还望指教啊~~~
网友回复:
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ // 第二个end忘了写() 第三个应该是begin()吧 第四个不用写() transform(str.begin(),str.end(),vect.begin(),fun);
网友回复:
是
网友回复:貌似想法有问题
网友回复:我的想法是将str字符串中的每一个字符,经fun之后,生成字符串,然后添加到vect容器中,各位达人,有什么办法可以解决我的难题啊,我已经想了好几天了,茶不思饭不想的,咋办类。。。。。。。。。。。。。:(
网友回复:这样可以吗:
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ #include <string> #include <vector> #include <iostream> #include <algorithm> using namespace std; class fun { public: fun() {} // 用得到的字符组成新的string string operator() (char& e) { string s(5, e); return s; } }; int main() { string str("abc"); vector<string> vect(str.size()); transform(str.begin(), str.end(), vect.begin(), fun()); for (vector<string>::iterator it = vect.begin(); it != vect.end(); it) { cout << *it << endl; } return 0; } /* aaaaa bbbbb ccccc 请按任意键继续. . . */
网友回复:飞鱼,你用什么编译的。为啥我这样运行时总是down了。
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; string fun(char ch) { string temp(1, ch); return temp; } int main() { string str = "abcdefg"; vector <string> vect; vect.reserve(str.size()); transform(str.begin(), str.end(), vect.begin(), fun); vector<string>::iterator iter = vect.begin(); while (iter != vect.end()) { cout << *iter << endl; } cin.get(); return 0; }
网友回复:太太太太太太太太太太太太太太太太太太太太太太太太太太太太太太太太太太太太太太太太太太感谢了
网友回复:用dev,把飞鱼程序中最后的'return 0;'改成'system("pause");'就可以了
网友回复:也谢谢楼上的各位!
网友回复:我用VC 6.0,Dev Cpp都运行过的,最后down掉。
网友回复:改成'system("pause");'就可以了
网友回复:晕 ,我用的vc2008
网友回复:#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
class fun:public binary_function <char,string,string>{
public:
fun() {}
// 用得到的字符组成新的string
string operator() (char& e,string& ee) {
stringstream s;
s < <ee < <e;
return s.str();
}
};
int main(){
string str("abcdefg"),ss("hh");
vector <string> vect(str.size());
transform(str.begin(), str.end(), vect.begin(), bind2nd(fun(),ss));
for (vector <string>::iterator it = vect.begin(); it != vect.end(); it) {
cout < < *it < < endl;
}
system("pause");
}
我想把"hh"跟str="abcdefg"中的每一个字符都组合起来,例如:hha,hhb,hhc,hhd,hhe,hhf,hhg
但我上面写的程序编译不通过,不知为什么,请教一下,谢谢哈
网友回复:#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <functional>
using namespace std;
class fun: public binary_function <char,string,string>{
public:
fun() {}
// 用得到的字符组成新的string
string operator() (char& e,const string& ee) const {
stringstream s;
s < <ee < <e;
return s.str();
}
};
int main(){
string str("abcdefg"),ss("hh");
vector <string> vect;
transform(str.begin(), str.end(), back_inserter(vect), bind2nd(fun(),ss));
copy(vect.begin(), vect.end(), ostream_iterator <string>(cout, "\n"));
system("pause");
}
网友回复:换了种方式执行了,折腾死了
- C/C code
Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ #include <string> #include <vector> #include <iostream> #include <sstream> #include <algorithm> #include <functional> using namespace std; class fun //: public binary_function<char, string, string> { private: string sbase; public: fun(const string& s) : sbase(s) {} // 用得到的字符组成新的string string operator()(char e) { stringstream s; s << sbase << e; return s.str(); } }; int main(){ string str("abcdefg"),ss("hh"); vector <string> vect(str.size()); transform(str.begin(), str.end(), vect.begin(), fun(ss)); for (vector <string>::iterator it = vect.begin(); it != vect.end(); it) { cout << *it << endl; } system("pause"); }
网友回复:强人实在是太多了,小弟今天学到了很多,谢谢xkyx兄和johntitor兄,感动ing
关键字:讨教,一个,stl,方面,问题,
下一篇:下面没有链接了











文章评论
共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面