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: Haijia
pre: Ms.
post: Jin
Ms.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: Haijia
pre: Ms.
post: Jin
Ms.HaijiaJin
刚才矫情了。