====== C - C++ Lists - Search lists for certain elements ======
===== if_for_each_element.hpp =====
#pragma once
#include
#include
std::string get_first_uppercase_entry(std::vector list);
std::vector get_all_uppercase_entries(std::vector list);
----
===== if_for_each_element.cpp =====
#include "if_for_each_element.hpp"
#include // for std::find_if and std::count_if
#include // for std::isupper
std::string get_first_uppercase_entry(std::vector list)
{
std::vector result;
auto pos = std::find_if(
list.begin(),
list.end(),
[&](std::string entry)
{
size_t numberOfUpperCaseChars = std::count_if(entry.begin(), entry.end(), [](unsigned char c)
{
return std::isupper(c);
});
return numberOfUpperCaseChars == entry.length();
}
);
return pos == list.end() ? "" : *pos;
}
std::vector get_all_uppercase_entries(std::vector list)
{
std::vector result;
std::for_each(list.begin(), list.end(), [&](std::string entry)
{
size_t numberOfUpperCaseChars = std::count_if(entry.begin(), entry.end(), [](unsigned char c)
{
return std::isupper(c);
});
if (numberOfUpperCaseChars == entry.length())
{
result.emplace_back(entry);
}
});
return result;
}
----
===== References =====
https://en.cppreference.com/w/cpp/algorithm/find
https://en.cppreference.com/w/cpp/algorithm/for_each