c++ primer 5.1.1节练习答案

练习5.20

 1 int main()
 2 {
 3     vector<string> str;
 4     string str1;
 5     while (cin >> str1)
 6     {
 7         auto last = str.end();
 8         if (str.size() != 0 && str1 == (*(last-1)))
 9         {
10             cout << str1 << endl;
11             break;
12         }
13         else
14             str.push_back(str1);
15     }
16     system("pause");
17     return 0;
18 }
时间: 2024-08-05 11:11:50

c++ primer 5.1.1节练习答案的相关文章

c++ primer 6.5.1节练习答案

练习6.40 a)正确 b)错误,一旦某个形参被赋予了默认值,他后面的所有形参都必须有默认值. 练习6.41 a)错误,ht没有默认实参,而a的实参列表里也没有给出实参: b)合法,调用init(24 ,10 ,' '): c)虽然合法,但是与程序猿的设计初衷不符,*会转换成十进制的数43,相当于调用init(14, 43, ' '): 练习6.42 1 string make_plural(size_t ctr, const string &word, const string &end

c++ primer 5.5.3节练习答案

练习5.22 1 int main() 2 { 3 int sz = get_size(); 4 while (sz <= 0) 5 { 6 sz = get_size(); 7 continue; 8 } 9 system("pause"); 10 return 0; 11 }

c++ primer 5.5.2节练习答案

练习5.21 1 #include <iostream> 2 #include <string> 3 #include <cstring> 4 #include <vector> 5 using namespace std; 6 int main() 7 { 8 vector<string> str; 9 string str1; 10 while (cin >> str1) 11 { 12 auto last = str.end()

c++ primer 6.5.2节练习答案

练习6.43 a)放在头文件,因为内联函数和constexpr函数一般写在头文件中 b)函数的定义一般写在源文件,而声明放在头文件 练习6.44 1 inline bool isShorter(const string &s1, const string &s2) 2 { 3 return s1.size() < s2.size(); 4 } 练习6.45 内联函数一般用于优化规模小.流程直接.频繁调用的函数. 练习6.46 不行,isShorter函数如果定义成constexpr函

c++ primer 5.4.4节练习答案

练习5.18 a)循环体少了花括号: b)不允许在条件部分定义变量: c)condition使用的变量必须定义在循环体之外: 练习5.19 1 int main() 2 { 3 string str1, str2; 4 int i; 5 do 6 { 7 cout << "please enter two string:"; 8 if (cin >> str1 >> str2) 9 { 10 if (str1.size() < str2.siz

c++ primer 6.2.4节练习答案

练习6.21 1 int max(const int x, const int *y) 2 { 3 if (x > *y) 4 return x; 5 else 6 return *y; 7 } 8 9 int main() 10 { 11 int a1, a2; 12 while (cin >> a1 >> a2) 13 { 14 cout << "the max is " << max(a1, &a2) <<

c++ primer 6.2.3节练习答案

练习6.16 1 bool is_empty(const string &s) { return s.empty(); } 练习6.17 1 bool have_upper(const string &s) 2 { 3 for (auto c : s) 4 { 5 if (isupper(c)) 6 return true; 7 } 8 return false; 9 } 10 11 void to_lower(string &s) 12 { 13 for (auto &c

c++ primer 5.4.1节练习答案

练习5.14 1 int main() 2 { 3 vector<string> str; 4 string str1,str2; 5 int max = 0; 6 while (cin >> str1) 7 { 8 int cnt = 0; 9 str.push_back(str1); 10 auto it = str.begin(); 11 while (it != str.end()) 12 { 13 if (*it == str1) 14 ++cnt; 15 ++it; 1

c++ primer 4.11.1节练习答案

练习4.34 a) float->bool b) int->float->double c) char->int->double 练习4.35 a) char->int->char b) int->unsigned int->float c) float->unsigned int->double d) int->float->double->char