Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return iterator of a sublist c++

Tags:

c++

iterator

list

We have a list

std::list<int> list;

// fill part of the list with 5
list.push_back(5);
list.push_back(5);
list.push_back(5);

// fill part of the list with 10
list.push_back(10);
list.push_back(10);
list.push_back(10);

// iterator that starts with 5
std::list<int>::iterator iterFiveBegin = list.begin();


//
std::list<int>::iterator iterEnd = list.end();

How can I get the iterator std::list<int>::iterator iterTenBegin of the list where it starts with "10"?

like image 580
Vyacheslav Avatar asked Nov 30 '25 19:11

Vyacheslav


1 Answers

Firstly, don't use variable name list, try intList instead.

You may use std::find

std::list<int>::iterator it = std::find (intList.begin(), intList.end(), 10);

Per std::find documentation:

std::find
Return value
Iterator to the first element satisfying the condition or last if no such element is found.

like image 188
taocp Avatar answered Dec 03 '25 11:12

taocp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!