Click here to Skip to main content
15,667,847 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a list of tuple written in c++. each tuple consists of two elements. I want to find a value in the second element of the tuples in the list. how can I find it?
the list is as follow:
list<std::tuple<int, int> > d;

i want it find for example the number "2" in d.item[2].

What I have tried:

i found some references about finding a value in a list of tuples . but, those tuples had consisted on just one element. but, my case has two elements in each tuple.
Updated 18-Apr-17 10:20am
Richard MacCutchan 18-Apr-17 15:02pm    
You need to go through the list looking for the value you want to find.
Maciej Los 18-Apr-17 16:01pm    
i found some references about - use that logic!
Philippe Mori 18-Apr-17 18:55pm    
If you want to do such search, then your design is somewhat suspicious. Using a map where the key would be the second item of your pair and the value the first one would be more sensible. If you really want to search such item, then you can specify a predicate when doing your search. In any case, reading the documentation might be a good idea.

1 solution

Check the documentation: std::tuple -[^]

If i understand you well...
All you need to do is to loop through the elements on the list[^], then to use std::get() function.

Think of it!

d(0) - returns the first element on the list
std::get<1>(d(0)) - returns second element of tuple of first element on the list
Share this answer
Member 11807654 19-Apr-17 1:58am    
Maciej Los 19-Apr-17 4:16am    
You're very welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900