练习9.41:
编写程序,从一个vector<char>初始化一个string。
解答:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main(){
vector<char> vc{'H', 'e', 'l', 'l', 'o'};
string str(vc.begin(), vc.end());
cout << str << endl;
}练习9.42:
假定你希望每次读取一个字符存入一个string中,而且知道最少需要读取100个字符,应该如何提高程序的性能?
解答:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main(){
vector<string> vstr, result_vstr;
string temp;
while(cin >> temp){
vstr.push_back(temp);
}
for (int i = 0; i < vstr.size(); i++){
for (int j = 0; j < vstr.at(i).size(); j++){
result_vstr.push_back(vstr.at(i).substr(j, 1));
}
}
for (auto i: result_vstr){
cout << i << " ";
}
cout << endl;
}
在我的理解中,需要将IO和程序处理分离,这样就会提高处理的速度。
这里没有和传统的下标方式对比,不过,根据之前的理论描述,应该也差不了多少。
练习9.43:
编写一个函数,接受三个string参数s、oldVal和newVal。使用迭代器及insert和erase函数将s中所有oldVal替换为newVal。测试你的程序,用它替换通用的简写形式,如,将"tho"替换为"though",将"thru"替换为"through"。
解答:
#include <iostream>
#include <string>
using namespace std;
void myReplace(string& s, string oldVal, string newVal){
size_t pos = s.find(oldVal);
if (string::npos == pos){
return;
}
s.erase(pos, oldVal.size());
s.insert(pos, newVal);
}
int main(){
string str("I tho that, thru I miss the hotel.");
myReplace(str, "tho", "though");
cout << str << endl;
myReplace(str, "thru", "through");
cout << str << endl;
}
这里我没有用迭代器,我使用string的成员函数find去查找oldVal,返回下标值。
之后,进行旧值擦除和新值的插入。
这里可以使用algorithm中的find函数,来对旧值进行查找,并返回迭代器指针。
练习9.44:
重写上一题的函数,这次使用一个下标和replace。
解答:
#include <iostream>
#include <string>
using namespace std;
void myReplace(string& s, string oldVal, string newVal){
size_t pos = s.find(oldVal);
if (string::npos == pos){
return;
}
s.replace(pos, oldVal.size(), newVal);
}
int main(){
string str("I tho that, thru I miss the hotel.");
myReplace(str, "tho", "though");
cout << str << endl;
myReplace(str, "thru", "through");
cout << str << endl;
}
因为上题就使用的类似方式写的,所以简单的修改成replace即可。
replace可接受多种参数形式,这里使用的是通用的。
练习9.45:
编写一个函数,接受一个表示名字的string参数和两个分别表示前缀(如"Mr."或"Ms.")和后缀(如"Jr."或"III")的字符串。使用迭代器及insert和append函数将前缀和后缀添加到给定的名字中,将生成的新string返回。
解答:
#include <iostream>
#include <string>
using namespace std;
string myReplace(const string& name, string prefix, string suffix){
string temp_name = name;
temp_name.insert(0, prefix + " ");
temp_name.append(" " + suffix);
return temp_name;
}
int main(){
string name_str("Timmy");
cout << myReplace(name_str, "Mr.", "Jr.") << endl;
}
个人感觉这道题没有这么麻烦,应该是仅为了练习insert和append函数设计的。
使用string自带的“+”法运算就可以, 比如 prefix + name + suffix,就可以完成这个函数实现的功能。
练习9.46:
重写上一题的函数,这次使用位置和长度来管理string,并只使用insert。
解答:
#include <iostream>
#include <string>
using namespace std;
string myReplace(const string& name, string prefix, string suffix){
string temp_name = name;
temp_name.insert(0, prefix + " ");
temp_name.insert(temp_name.size() - 1," " + suffix);
return temp_name;
}
int main(){
string name_str("Timmy");
cout << myReplace(name_str, "Mr.", "Jr.") << endl;
}
根据题目,这两题都不是处理一段文字中的名字,而就是单纯的给出名字加前后缀,所以实现也相对简单。
本文提供了几个关于C++中字符串处理的练习题解答,包括从vector<char>初始化string、提高读取字符到string的效率、字符串替换以及字符串前后缀添加等常见操作。

5279

被折叠的 条评论
为什么被折叠?



