金海佳学C++primer9.45/9.46

来源:互联网 发布:mac pages 编辑:程序博客网 时间:2024/06/10 20:16

string加前缀和后缀

Practice 9.45

#include <iostream>#include <queue>#include <string>#include <vector>#include <algorithm>#include <list>#include <iterator>#include <cmath>#include <cstring>#include <forward_list>#include <sstream>using namespace std;string add_pre_post(string s, string pre, string post) {    s.insert(0, pre);    s.append(post);    return s;}int main() {    string s, pre, post;    cin >> s >> pre >> post;    cout << "s: " << s << endl;    cout << "pre: " << pre << endl;    cout << "post: " << post << endl;    cout << add_pre_post(s, pre, post) << endl;    return 0;   }

Output

Practice

s: Haijiapre: Ms.post: JinMs.HaijiaJin
#include <iostream>#include <queue>#include <string>#include <vector>#include <algorithm>#include <list>#include <iterator>#include <cmath>#include <cstring>#include <forward_list>#include <sstream>using namespace std;string add_pre_post_2(string s, string pre, string post) {    s.insert(0, pre);    s.insert((int)s.length(), post);    return s;}int main() {    string s, pre, post;    cin >> s >> pre >> post;    cout << "s: " << s << endl;    cout << "pre: " << pre << endl;    cout << "post: " << post << endl;    cout << add_pre_post_2(s, pre, post) << endl;    return 0;   }

Output

s: Haijiapre: Ms.post: JinMs.HaijiaJin

刚才矫情了。

原创粉丝点击