How to search a list for specific value and get the index in C++

1 Answer

0 votes
#include <iostream>
#include <list>

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

int main()
{
	list<int> lst{ 1, 3, 8, 23, 88, 12, 99, 7 };

	list<int>::iterator pos;

	pos = find(lst.begin(), lst.end(), 88);

	if (pos != lst.end()) {
		cout << "found in index: " << distance(lst.begin(), pos) << endl;
	}
	else {
		cout << "not found" << endl;
	}
}


/*
run:

found in index: 4

*/

 



answered Jan 20, 2018 by avibootz

Related questions

3 answers 702 views
1 answer 176 views
1 answer 162 views
1 answer 182 views
...