Ежедневный бит(е) C++ #11, вариант алгоритма C++11 std::partition_copy
std::partition_copy
— это вариант std::partition
C++11, который выводит каждый раздел через два предоставленных итератора, а не встроенный.
В 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."}