Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
Lets say I have structure
C++
struct MyData{
  long id;
  whcar_t name[64];
  wchar_t data[256];
};

typedef std::vector<mydata>  MyData_t;

MyData_t dataArray;

dataArray contains data
C++
{ {1;L"First";L"Data1"}, {2;L"Second";L"Data2"}, {4;L"Fourth";L"Data4"}}

Now I want to find out is id(for example 3) used in this array.

How can I do that with std::adjacent_find() ?

What I have tried:

std::adjacent_find(dataArray.begin(), dataArray.end(), ????? )
Posted
Updated 18-Jun-21 3:31am
v2
Comments
CPallini 18-Jun-21 9:34am    
adjacent_find searches for two consecutive equal values. Are you sure is what you need?

1 solution

You need to provide a custom predicate function to do the comparisons. See std::adjacent_find - cppreference.com[^].
 
Share this answer
 
Comments
NoviceEx 18-Jun-21 9:56am    
and I was not able to do that, probably I do not get something

if I use std::find_if then I can do it

const auto& found = std::find_if(dataArray.begin(),
dataArray.end(),
[&Id](const MyData &md) { return md.id == Id; } );
if( found == dataArray.end() )
return Id;

How to do that with std::adjacent_find()?
Richard MacCutchan 18-Jun-21 10:02am    
You do not have two adjacent items with the same id.

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