zoukankan      html  css  js  c++  java
  • 第九章-顺序容器--用迭代器查找某个数值是否在向量中,注意程序必须处理未找到给定值的情况

    #include<fstream>
    #include <vector>
    #include<string>
    #include<iostream>
    #include <sstream>
    #include <stdexcept>
    using namespace std;
    //返回一个迭代器指向找到的元素
    vector<int>::iterator search_vec(vector<int>::iterator beg, vector<int>::iterator end, int val){
    	for (; beg != end; beg++){
    		if (*beg == val)
    			return beg;		
    	}
    	return end;//搜索失败,返回end 迭代器
    }
    
    
    int main()
    {
    	vector<int> ilist = { 1, 2, 3, 4, 5, 6, 7 };
    	cout << search_vec(ilist.begin(), ilist.end(), 3) - ilist.begin() << endl;
    	cout << search_vec(ilist.begin(), ilist.end(), 8) - ilist.begin() << endl;
    	system("pause");
    	return 0;
    }
    

      

  • 相关阅读:
    POJ-2393
    POJ-1328
    POJ-2376
    CF-811B
    CF-811A
    CF-816B
    P1111 修复公路
    P2777 [AHOI2016初中组]自行车比赛
    P1889 士兵站队
    P1459 三值的排序 Sorting a Three-Valued Sequence
  • 原文地址:https://www.cnblogs.com/bananaa/p/7722554.html
Copyright © 2011-2022 走看看