c:c_lists:search_lists_for_certain_elements
Table of Contents
C - C++ Lists - Search lists for certain elements
if_for_each_element.hpp
#pragma once #include <vector> #include <string> std::string get_first_uppercase_entry(std::vector<std::string> list); std::vector<std::string> get_all_uppercase_entries(std::vector<std::string> list);
if_for_each_element.cpp
#include "if_for_each_element.hpp" #include <algorithm> // for std::find_if and std::count_if #include <cctype> // for std::isupper std::string get_first_uppercase_entry(std::vector<std::string> list) { std::vector<std::string> 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<std::string> get_all_uppercase_entries(std::vector<std::string> list) { std::vector<std::string> 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
c/c_lists/search_lists_for_certain_elements.txt · Last modified: 2021/04/28 09:10 by peter