C++primer第5版課後練習習題答案9.5

#include <iostream>
#include <vector>
#include <iterator>
using namespace std;
vector<int>::const_iterator& getval(vector<int>::const_iterator& ibegin, vector<int>::const_iterator& iend, int val)
{
	while (ibegin != iend)
	{
		if (*ibegin == val)
		{
			return ibegin;
		}

		++ibegin;
	}

	return iend;
}
vector<int>::iterator& getval(vector<int>::iterator& ibegin, vector<int>::iterator& iend, int val)
{
	while (ibegin != iend)
	{
		if (*ibegin == val)
		{
			return ibegin;
		}

		++ibegin;
	}

	return iend;
}
int main()
{
	vector <int> vint = {12, 32, 434, 5, 45, 45, 5, 6, 6, 567};
	int val = 567;
	auto i = vint.cbegin();
	auto j = vint.cend();

	if (getval(i, j, val)!=j)
	{
		cout << "get it:" << *i << endl;
	}
	else
	{
		cout << "not found :" << val << endl;
	}

	system("pause");


	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章