日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > c/c++ >内容正文

c/c++

C++primer 9.2.1节练习

發(fā)布時(shí)間:2025/6/17 c/c++ 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++primer 9.2.1节练习 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

練習(xí)9.3

迭代器begin和end必須指向相同的容器,end可以與begin指向相同的位置,但不能指向begin之前的位置;

練習(xí)9.4

1 #include <iostream> 2 #include <fstream> 3 #include <sstream> 4 #include <vector> 5 #include <iterator> 6 7 using namespace std; 8 9 bool checkValue(vector<int>::iterator it, vector<int>::iterator it1, int a); 10 11 int main() 12 { 13 vector<int> num{ 1,2,3,4,5,6 }; 14 cout << checkValue(num.begin(), num.end(), 7) << endl; 15 system("pause"); 16 return 0; 17 } 18 19 bool checkValue(vector<int>::iterator it, vector<int>::iterator it1, int a) 20 { 21 while (it != it1) 22 { 23 if (*it == a) 24 return true; 25 else 26 ++(it); 27 } 28 return false; 29 }

練習(xí)9.5

1 #include <iostream> 2 #include <fstream> 3 #include <sstream> 4 #include <vector> 5 #include <iterator> 6 7 using namespace std; 8 9 const vector<int>::iterator *checkValue (vector<int>::iterator it, vector<int>::iterator it1, int a); 10 11 int main() 12 { 13 vector<int> num{ 1,2,3,4,5,6 }; 14 cout << (checkValue(num.begin(), num.end(), 7)) << endl; 15 system("pause"); 16 return 0; 17 } 18 19 const vector<int>::iterator *checkValue (vector<int>::iterator it, vector<int>::iterator it1, int a) 20 { 21 while (it != it1) 22 { 23 try 24 { 25 if (*it == a) 26 return &it; 27 else 28 ++(it); 29 if (it == it1) 30 throw runtime_error("error"); 31 } 32 catch (runtime_error err) { 33 cout << err.what() << endl; 34 } 35 } 36 return &it1; 37 }

利用拋出異常的方法來(lái)處理未找到定值的情況;

練習(xí)9.6

迭代器支持的算術(shù)運(yùn)算不能用于list容器,因?yàn)閘ist容器不是按照順序存儲(chǔ)的,他是個(gè)雙向鏈表;

改成while(iter1 != iter2)

轉(zhuǎn)載于:https://www.cnblogs.com/wuyinfenghappy/p/7326968.html

總結(jié)

以上是生活随笔為你收集整理的C++primer 9.2.1节练习的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。