+ 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?
7 Respuestas
+ 3
@kurwius Thank You!
+ 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;
}
+ 3
@0xDEADBEEF Thank You very much!
+ 1
@~swim~
With find_if(), right?
I got it. Thanks!