I am trying to use for each loop in c++.
this->Functions is a vector.
std::vector< std::shared_ptr<Sum_Function> > Functions;
After reading i came across two different ways to do for each.
bool Container::HasFunction(std::string stdstrFunctionName )
{
for (auto &func : this->Functions)
{
if (func->getFunctionName() == stdstrFunctionName)
return true;
}
return false;
}
///////////////////////////////////////////////////////////////////////////////////
bool Container::HasFunction(std::string stdstrFunctionName )
{
for (auto it = this->Functions.begin(); it != this->Functions.end(); ++it)
{
auto& func = *it;
if (func->getFunctionName() == stdstrFunctionName)
return true;
}
return false;
}
my question is that these both are nearly doing the same stuff , is there any difference between the two.
Or just different flavors for the same thing.
return std::any_of(Functions.begin(), Functions.end(), [&](const auto& f) { return f->getFunctionName() == stdstrFunctionName;} );