![](/style/images/good.png)
![](/style/images/bad.png)
Compare two consecutive elements in std :: list
source link: https://www.codesd.com/item/compare-two-consecutive-elements-in-std-list.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Compare two consecutive elements in std :: list
I'd like to compare two consecutive elements in a std::list while iterating through the list. What is the proper way to access element i+1 while my iterator is at element i? Thanks Cobe
Boost has a utility called next
(and its inverse, prior
) for just that purpose.
*itr == *next(itr)
Edit: But, if we step back to look at the forest, the real question is, why custom-write your adjacent_find
function? (I recommend Nicola Bonelli's answer to be accepted.) That's part of the STL, and doesn't require using Boost, if your code doesn't use Boost (thanks to the commenters for pointing this out).
Related Articles
How to Find a Sublist of Two Consecutive Elements in a List Using Linq
How do I access the first element of std :: list?
Generic solution to compare two known generic and type lists only during runtime
Iterator to last element in std :: list
Python: how to compare the last element of the list with the character of the string
ArrayIndexOutOfBoundsException while finding the maximum difference between two consecutive elements in the array
Compare two ArrayList & lt; String & gt; lists in Java
Comparing the nth element of the list with another nth element, then exchange if the instruction is true PYTHON 3.4.3
calculate the largest sum of consecutive elements (in a list)
Associate consecutive elements in a list during iteration
Algorithm: allows to sort a table with a minimum of permutation of two consecutive elements
Scala group consecutive elements in the list where the function is true
Stylization of the second of two consecutive elements & lt; dd & gt; in a definition list
How do I compare two tables and return the list of deleted / updated / inserted items in JavaScript?
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK