C++Primer第五版 第九章習題答案(41~50)
41:
#include<iostream>
#include<string>
#include<vector>
using namespace std;
void main()
{
vector<char> c{ 'a', 'b', 'c', 'd', 'e' };
string s1(c.begin(),c.end());
cout << s1 << endl;
system("pause");
}
42:用reverse() 操作提前預留好記憶體,這樣就不用在執行過程中進行修改記憶體操作了。
43:特別注意元素新增或刪除後,迭代器的變化。
知識點: insert(p, t): 在迭代器 p 前插入元素 t ,返回新新增元素的迭代器。 [注] 還有形如:insert(p, n, t)
insert(p, b, e): 在迭代器 p 前插入迭代器 [b, e)範圍的元素,返回新新增第一個元素的迭代器。
[注] 還有形如insert(p, n, t): 在 p 前插入 n 個元素 t ;和 insert(p, il): 在 p 前插入元素列表 il 。
erase(p): 刪除迭代器 p 所指的元素,返回被刪元素的下一元素迭代器。(p 是尾後迭代器,則會報錯)。
erase(b, e): 刪除迭代器 [b, e) 範圍的元素,返回一個指向最後一個被刪元素之後元素的迭代器。
#include<iostream> #include<string> using namespace std; void func(string &s, string &oldval, string &newval) { string::iterator beg = s.begin(); int old_size = oldval.size(), new_size = newval.size(); auto b = newval.begin(), //獲取替換物件的迭代器 e = newval.end(); while (beg != s.end()-old_size+1) { string sample(beg, beg + old_size); if (sample == oldval) { beg = s.erase(beg, beg + old_size); beg = s.insert(beg, b, e); //需用迭代器表示範圍,beg = s.insert(beg, newval);是不可行的,因為newval不是char型別。 beg += old_size; } else ++beg; } cout << s << endl; } void main() { string s{ "Y are a good boy!" }; string oldval = "Y"; string newval = "You"; func(s, oldval, newval); system("pause"); }
輸出:
You are a good boy! 請按任意鍵繼續. . .
44:用下標和 replace() 會方便很多:
#include<iostream>
#include<string>
using namespace std;
void func(string &s, string &oldval, string &newval)
{
int old_size = oldval.size();
for (int i = 0; i < s.size() - old_size; ++i)
{
string sample = s.substr(i, old_size); //獲取要檢測樣本
if (sample == oldval)
s.replace(i, old_size, newval);
}
cout << s << endl;
}
void main()
{
string s{ "Y are a good boy!" };
string oldval = "Y";
string newval = "You";
func(s, oldval, newval);
system("pause");
}
輸出:
You are a good boy! 請按任意鍵繼續. . .
45:
#include<iostream>
#include<string>
using namespace std;
string func(string s, string &prefix, string &suffix)
{
auto b = s.begin();
b = s.insert(b, prefix.begin(), prefix.end());
s.append(suffix);
return s;
}
void main()
{
string s("golden_moon");
string prefix("Ms.");
string suffix = "III";
cout << func(s, prefix, suffix) << endl;
system("pause");
}
輸出:
Ms.golden_moonIII 請按任意鍵繼續. . .
46:
#include<iostream>
#include<string>
using namespace std;
string func(string s, string &prefix, string &suffix)
{
s.insert(0, prefix);
s.insert(s.size(), suffix);
return s;
}
void main()
{
string s("golden_moon");
string prefix("Ms.");
string suffix = "III";
cout << func(s, prefix, suffix) << endl;
system("pause");
}
輸出:
Ms.golden_moonIII 請按任意鍵繼續. . .
47:
#include<iostream>
#include<string>
using namespace std;
void main()
{
string name("ab2c3d7R4E6");
string numbers = "0123456789";
string letters{ "abcdRE" };
string::size_type pos = 0;
while ((pos = name.find_first_of(numbers, pos)) != string::npos)
{
cout << "find number: " << name[pos] << " at index: " << pos << endl;
++pos;
}
pos = 0;
while ((pos = name.find_first_not_of(numbers, pos)) != string::npos)
{
cout << "find letter: " << name[pos] << " at index: " << pos << endl;
++pos;
}
system("pause");
}
48:在numbers 中找不到 name,此時返回的值是 string::npos = -1,即:4294967295。(由於npos 是 unsigned 型別)。
49:************************************************明天做********************************************************
50:
#include<iostream>
#include<string>
#include<vector>
using namespace std;
void main()
{
vector<string> in{ "1.1", "2.1", "3.1", "4.1", "5.1" };
int total_int = 0;
double total_double = 0;
for (auto a : in)
{
total_int += stoi(a);
total_double += stod(a);
}
cout << "the total of int is: " << total_int << endl;
cout << "the total of double is: " << total_double << endl;
system("pause");
}