std :: find_if or copy_if

Markus Freitag 3,791 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);
}
	
Developer technologies C++
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.