I haven't followed all of your code and issues, but if your
basic question is how to check a vector of structs to find
an element that meets a condition in one of the struct
members - using std::find_if with a predicate:
// find the first struct in a vector with a double
// member <= a given value
#include <iostream> // std::cout
#include <algorithm> // std::find_if
#include <vector> // std::vector
#include <iomanip>
struct MyStruct
{
double price;
};
double threshold = 0.0;
bool PriceRanges(MyStruct ms)
{
return (ms.price <= threshold);
}
int main() {
std::vector<MyStruct> myvector;
MyStruct mystruxt;
mystruxt.price = 35.00;
myvector.push_back(mystruxt);
mystruxt.price = 41.00;
myvector.push_back(mystruxt);
mystruxt.price = 22.50;
myvector.push_back(mystruxt);
mystruxt.price = 11.00;
myvector.push_back(mystruxt);
threshold = 30.34;
//threshold = 10.00;
std::vector<MyStruct>::iterator it =
std::find_if(myvector.begin(), myvector.end(), PriceRanges);
if (it != myvector.end())
{
std::cout << std::fixed;
std::cout << std::setprecision(2);
std::cout << "The first value <= threshold of "
<< threshold << " is " << it->price << '\n';
}
else
{
std::cout << "No struct found in vector with a Price <= "
<< threshold << std::endl;
}
return 0;
}
Obviously, a lambda could be used instead of a predicate function
for those so-inclined.
- Wayne