How to find the first element that greater than N in vector using C++

1 Answer

0 votes
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

using std::cout;
using std::endl;

int main()
{
	std::vector<int> vec = { 3, 1, 4, 2, 5, 8, 6, 9 };
	std::vector<int>::iterator pos;

	int n = 4;
	pos = std::find_if(vec.begin(), vec.end(), std::bind2nd(std::greater<int>(), n));
	
	cout << distance(vec.begin(), pos) + 1 << endl;

	return 0;
}

/*
run:

5

*/

 



answered Feb 21, 2018 by avibootz
...