-1

I have an STL set of type:

std::set< std::pair< double, std::pair< unsigned, vector< unsigned > > > > X

I know I could make things simpler by changing my data structure but that's not an option here for me.

How can I search for an element of type unsigned only in the first elements of the inner pair in my set?

I tried the following approach, but doesn't work.

auto it = std::find_if(X.begin(), X.end(), [value_searching]
(const std::pair<double, std::pair< unsigned, vector< unsigned > >& elem) {
return elem->second.first == value_searching
        });
  • 1
    Only a pointer or iterator should be left of `->`. `elem` is neither - it's a `pair`. – aschepler Apr 07 '17 at 00:49
  • What would be the alternate approach for this case? @aschepler – Fabia Bushra Tamanna Apr 07 '17 at 00:58
  • Such complex structure never make sense in real live... It make the code hard to maintain as you have multiple first and second... Also, it might not be as efficient as you would like... why do you use a set if you dont use the key to find items. – Phil1970 Apr 07 '17 at 01:01

1 Answers1

2

You need to use . instead of ->, because elem is a reference, not a pointer, to a pair.

auto it = std::find_if(X.begin(), X.end(), [value](auto& elem) {
  return elem.second.first == value;
});
Joseph Thomson
  • 9,888
  • 1
  • 34
  • 38