+ 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
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
7 Respuestas
+ 3
@kurwius Thank You!
26th Oct 2017, 11:42 AM
Solo Wanderer 4315
Solo Wanderer 4315 - 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
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
@~swim~ With find_if(), right? I got it. Thanks!
30th Oct 2017, 9:24 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar