How to use upper_bound() with vector in C++

1 Answer

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

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

int main()
{
	vector<int> vec{ 1, 2, 7, 8, 9};
	vector<int>::iterator p;

	p = upper_bound(vec.begin(), vec.end(), 3);

	cout << "position: " << (p - vec.begin()) << " value: " << *p << endl;

	return 0;
}



/*
run:

position: 2 value: 7

*/

 



answered May 1, 2018 by avibootz
edited Jul 14, 2023 by avibootz

Related questions

...