Using std::find() on a vector of COORDs... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Using std::find() on a vector of COORDs...

I have a vector of COORDs (The COORDs defined in windows.h), to store some points for the console. Now I wish to compare the current position of the cursor with these points. To compare, I used std::find. But it seems the COORD structure does not have an equality operator overload. So find cannot work. So, is there some general find, that can even accept a function to use for comparisions? Or will I have to redeclare a new class with the equality operator overloaded so that I can use std::find?

26th Oct 2017, 10:56 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
4 Answers
+ 3
@kurwius Thank You!
26th Oct 2017, 11:42 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 3
Use std:find_if with a lambda #include <algorithm> #include <Windows.h> #include <vector> #include <iostream> int main() { COORD value; value.X = 100; value.Y = 25; std::vector<COORD> coords; //coords.push_back(value); auto it = std::find_if(coords.cbegin(), coords.cend(), [&value](const COORD& coord) {return value.X == coord.X && value.Y == coord.Y;}); std::cout << "is " << (it == coords.cend() ? "not in vector" : "in vector"); return 0; }
26th Oct 2017, 11:42 AM
aklex
aklex - avatar
+ 3
@0xDEADBEEF Thank You very much!
26th Oct 2017, 11:43 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 1
@~swim~ With find_if(), right? I got it. Thanks!
30th Oct 2017, 9:24 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar