C++primer 10.2.1节练习
生活随笔
收集整理的這篇文章主要介紹了
C++primer 10.2.1节练习
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
練習10.3
1 #include<iostream> 2 #include<string> 3 #include<vector> 4 #include <stack> 5 #include <algorithm> 6 #include <numeric> 7 #include <list> 8 9 using namespace std; 10 11 12 int main() 13 { 14 vector<int> vec{ 1,2,3,4,5,6,7,8,9,10 }; 15 int val = 0; 16 auto num = accumulate(vec.cbegin(), vec.cend(), val); 17 cout << num << endl; 18 system("pause"); 19 return 0; 20 }練習10.4
最后返回的值精度會丟失,但編譯器不會提示有錯誤,因為accumulate的第三個參數的類型決定了函數中使用哪個加法運算符一級返回值的類型;
練習10.5
如果寫成 char * 會發出警告,表示字符串可以修改,而例子中的字符串不允許修改,更好的方法是寫成const char *;?
1 #include<iostream> 2 #include<string> 3 #include <iostream> 4 #include <string> 5 #include <vector> 6 #include <algorithm> 7 using namespace std; 8 9 int main() 10 { 11 const char *s1 = "good"; 12 const char *s2 = "boy"; 13 vector<const char *> roster1, roster2; 14 roster1.push_back(s1); 15 roster1.push_back(s2); 16 roster2.push_back(s1); 17 roster2.push_back(s2); 18 19 bool flag = equal(roster1.cbegin(), roster1.cend(), roster2.cbegin()); 20 21 if (true == flag) 22 cout << "same..." << endl; 23 else 24 cout << "not same..." << endl; 25 system("pause"); 26 return 0; 27 }?
轉載于:https://www.cnblogs.com/wuyinfenghappy/p/7350573.html
總結
以上是生活随笔為你收集整理的C++primer 10.2.1节练习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP header的几种用法
- 下一篇: 2017.8.12 联考题