# std :: find_if or copy_if

3,786 Reputation points
2023-12-20T19:25:07.2466667+00:00

Hello,

The goal, I would like to have all values smaller than the threshold in a vector.
Is it possible via find_if and copy_if?

for what is setprecision?

``````// ** H
bool PriceRanges(MyStruct ms)
{
return (ms.Price <= Threshold);
}

struct MyStruct
{
double Price;
};

double Threshold = 0.0;

// ** CPP

std::vector<MyStruct> myvector;

MyStruct mystruct;

mystruct.Price = 35.00;
myvector.push_back(mystruct);

mystruct.Price = 41.00;
myvector.push_back(mystruct);

mystruct.Price = 22.50;
myvector.push_back(mystruct);

mystruct.Price = 11.00;
myvector.push_back(mystruct);

Threshold = 30.34;

std::vector<MyStruct>::iterator i7 = std::find_if(myvector.begin(), myvector.end(), [=](const MyStruct& ms) {return PriceRanges(ms); });

if (i7 != myvector.end()) // not work
{
//TRACE1("Fixed = %f", std::fixed);
//TRACE1("Fixed = %f", std::setprecision(2));
TRACE1("i7->price = %f", i7->Price);
}

// ** maybe better here
std::vector<MyStruct> myvectorResult2;
std::copy_if(myvector.begin(), myvector.end(), std::back_inserter(myvectorResult2), [](const MyStruct& d) { return d.Price < 23.33; });

for (auto it = myvectorResult2.begin(); it != myvectorResult2.end(); ++it)
{
TRACE1("it->price = %f\n", it->Price);
}

``````
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,646 questions