金海佳学C++primer 练习9.4/9.5
生活随笔
收集整理的這篇文章主要介紹了
金海佳学C++primer 练习9.4/9.5
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
給定區(qū)間查找值
Practice9.4
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <list> #include <iterator> #include <cmath> #include <cstring> #include <forward_list> using namespace std;void print(vector<int> ivec) {for(auto i : ivec) {cout << i << " ";}cout << endl; }bool find_value(vector<int>::iterator b, vector<int>::iterator e, int value){// "it != e" !!!// [ )for(auto it = b; it != e; it++) {if(*it == value) {return true;}}return false; }int main() {vector<int> ivec = {0, 1, 3, 4, 5, 6, 7, 8};auto b = ivec.begin();auto e = ivec.begin();e++, e++, e++, e++;cout << "b -----> " << *b << endl;cout << "e -----> " << *e << endl;/* test1 */cout << find_value(b,e,3) << endl;/* test2 */cout << find_value(b,e,12) << endl; return 0; }Output
b -----> 0 e -----> 5 1 0Practice9.5
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <list> #include <iterator> #include <cmath> #include <cstring> #include <forward_list> using namespace std;void print(vector<int> ivec) {for(auto i : ivec) {cout << i << " ";}cout << endl; }vector<int>::iterator find_value_2(vector<int>::iterator b, vector<int>::iterator e, int value){// "it != e" !!!// [ )for(auto it = b; it != e; it++) {if(*it == value) {return it;}}cout << "Find nothing" << endl;return b; }int main() {vector<int> ivec = {0, 1, 3, 4, 5, 6, 7, 8};auto b = ivec.begin();auto e = ivec.begin();e++, e++, e++, e++;cout << "b -----> " << *b << endl;cout << "e -----> " << *e << endl;/* test1 */cout << *(find_value_2(b,e,3)) << endl;/* test2 */cout << *(find_value_2(b,e,12)) << endl; return 0; }Oupout
b -----> 0 e -----> 5 3 Find nothing 0Keep on going never give up
總結(jié)
以上是生活随笔為你收集整理的金海佳学C++primer 练习9.4/9.5的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【java】Selenium爬虫控制谷歌
- 下一篇: 申请美国计算机科学博士,美国计算机博士申