Ежедневен бит(д) на C++ #11, C++11 алгоритъм вариант std::partition_copy
std::partition_copy
е C++11 вариант на std::partition
, който извежда всеки дял през двата предоставени итератора, вместо да работи вградено.
В C++20 алгоритъмът получи вариант на диапазони.
#include <algorithm> #include <ranges> #include <vector> #include <string> #include <iostream> std::vector<std::string> vowels, consonants; std::ranges::partition_copy( std::views::istream<std::string>(std::cin), std::back_inserter(vowels), // iterator for condition == true std::back_inserter(consonants), // iterator for condition == false [](const std::string& s){ // Check if first character is a vowel: char c = std::tolower(s.front()); // guaranteed non-empty return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'); }); // For input "Hello, World! This is going to be a blast.": // vowels == {"is", "a"} // consonants == {"Hello,", "World!", "This", "going", // "to", "be", "blast."}