C++ Primer第五版_第九章习题答案(41~50)
文章目录
-
-
- 练习9.41
- 练习9.42
- 练习9.43
- 练习9.44
- 练习9.45
- 练习9.46
- 练习9.47
- 练习9.48
- 练习9.49
- 练习9.50
-
练习9.41
编写程序,从一个vector初始化一个string。
vector<char> v{ 'h', 'e', 'l', 'l', 'o' };
string str(v.cbegin(), v.cend());
练习9.42
假定你希望每次读取一个字符存入一个string中,而且知道最少需要读取100个字符,应该如何提高程序的性能?
使用 reserve(100)
函数预先分配100个元素的空间。
练习9.43
编写一个函数,接受三个string参数是s、oldVal 和newVal。使用迭代器及insert和erase函数将s中所有oldVal替换为newVal。测试你的程序,用它替换通用的简写形式,如,将"tho"替换为"though",将"thru"替换为"through"。
#include <iostream>
#include <string>using namespace std;void replace(string& s, const string& oldVal, const string& newVal)
{auto curr = s.begin();while (curr != s.end() - oldVal.size()){if (oldVal == string(curr, curr + oldVal.size())){curr = s.erase(curr, curr + oldVal.size());curr = s.insert(curr, newVal.begin(), newVal.end());curr += newVal.size();}else{++curr;}}
}int main()
{string s("To drive straight thru is a foolish, tho courageous act.");replace(s,"tho","though");replace(s, "thru", "through");cout << s;
}
练习9.44
重写上一题的函数,这次使用一个下标和replace。
#include <iostream>
#include <string>using namespace std;void replace(string& s, const string& oldVal, const string& newVal)
{for (size_t pos = 0; pos <= s.size() - oldVal.size(); /* */){if (s[pos] == oldVal[0] && s.substr(pos, oldVal.size()) == oldVal){s.replace(pos, oldVal.size(), newVal);pos += newVal.size();}else{++pos;}}}int main()
{string s("To drive straight thru is a foolish, tho courageous act.");replace(s, "tho", "though");replace(s, "thru", "through");cout << s;
}
练习9.45
编写一个函数,接受一个表示名字的string参数和两个分别表示前缀(如"Mr.“或"Mrs.”)和后缀(如"Jr.“或"III”)的字符串。使用迭代器及insert和append函数将前缀和后缀添加到给定的名字中,将生成的新string返回。
#include <iostream>
#include <string>using std::string;
using std::cin;
using std::cout;
using std::endl;string add_pre_and_suffix(string name, string const& pre, string const& su)
{name.insert(name.begin(), pre.cbegin(), pre.cend());return name.append(su);
}int main()
{string name("Huang");cout << add_pre_and_suffix(name, "Mr.", " Jr.") << endl;return 0;
}
练习9.46
重写上一题的函数,这次使用位置和长度来管理string,并只使用insert。
#include <iostream>
#include <string>std::string add_pre_and_suffix(std::string name, std::string const& pre, std::string const& su)
{name.insert(0, pre);name.insert(name.size(), su);return name;
}int main()
{std::string name("Huang");std::cout << add_pre_and_suffix(name, "Mr.", " Jr.");return 0;
}
练习9.47
编写程序,首先查找string"ab2c3d7R4E6"中每个数字字符,然后查找其中每个字母字符。编写两个版本的程序,第一个要使用find_first_of,第二个要使用find_first_not_of。
#include <iostream>
#include <string>using namespace std;int main()
{string numbers("0123456789");string s("ab2c3d7R4E6");cout << "numeric characters: ";for (int pos = 0; (pos = s.find_first_of(numbers, pos)) != string::npos; ++pos){cout << s[pos] << " ";}cout << "\\nalphabetic characters: ";for (int pos = 0; (pos = s.find_first_not_of(numbers, pos)) != string::npos; ++pos){cout << s[pos] << " ";}return 0;}
练习9.48
假定name和numbers的定义如325页所示,numbers.find(name)返回什么?
返回 string::npos
练习9.49
如果一个字母延伸到中线之上,如d 或 f,则称其有上出头部分(ascender)。如果一个字母延伸到中线之下,如p或g,则称其有下出头部分(descender)。编写程序,读入一个单词文件,输出最长的既不包含上出头部分,也不包含下出头部分的单词。
#include <iostream>
#include <string>
#include <fstream>using namespace std;void find_not_in(const string& s,string& result)
{string not_in("dfpg");int size;if (s.find_first_of(not_in) == string::npos){result = result.size() < s.size() ? s : result;}
}int main()
{ifstream ifs("./data/letter.txt");if (!ifs)return -1;string longest;for (string curr; ifs >> curr; ){find_not_in(curr, longest);}cout << longest << endl;return 0;
}
练习9.50
编写程序处理一个vector,其元素都表示整型值。计算vector中所有元素之和。修改程序,使之计算表示浮点值的string之和。
#include <iostream>
#include <vector>
#include <string>using namespace std;int sum_of_int(const vector<string>& v)
{int sum = 0;for (auto i : v){sum += stoi(i);}return sum;
}float sum_of_float(const vector<string>& v)
{float sum = 0;for (auto i : v){sum += stof(i);}return sum;
}int main()
{vector<string> v = { "1", "2", "3", "4.5" };cout << sum_of_int(v) << endl;cout << sum_of_float(v) << endl;return 0;
}