std::copy_if
Актуально для C++23.
#include <algorithm>
Актуально на 2024-03-08.
Define overload #1
template<class InputIter, class OutputIter, class UnaryPredicate>
constexpr OutputIter
copy_if(InputIter first, InputIter last, OutputIter out, UnaryPredicate pred);
template<class InputIter, class OutputIter, class UnaryPredicate>
constexpr OutputIter
copy_if(InputIter first, InputIter last, OutputIter out, UnaryPredicate pred);
template<class InputIter, class OutputIter, class UnaryPredicate> constexpr OutputIter copy_if(InputIter first, InputIter last, OutputIter out, UnaryPredicate pred);
Скопирует из диапазона [first, last] в "out" элементы для которых унарный предикат "pred" вернёт true.
Вернёт OutputIter итератор за последним скопированным элементом.
Example, possible implementation
Define overload #2
template<class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class UnaryPredicate>
ForwardIt2 copy_if(ExecutionPolicy&& policy, ForwardIt1 first,
ForwardIt1 last, ForwardIt2 d_first,
UnaryPredicate pred);
template<class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class UnaryPredicate>
ForwardIt2 copy_if(ExecutionPolicy&& policy, ForwardIt1 first,
ForwardIt1 last, ForwardIt2 d_first,
UnaryPredicate pred);
template<class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class UnaryPredicate> ForwardIt2 copy_if(ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first, UnaryPredicate pred);
TODO
Example, possible implementation
Examples
Example 1:
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
int main()
{
auto data = {1, 2, 3, 5, 7};
std::vector<int> v(4);ф
// скопирует только нечетные элементы
auto it = std::copy_if(std::begin(data), std::end(data), std::begin(v), [](auto value) {
return value % 2 != 0;
});
for (auto x : v)
{
std::cout << x << std::endl;
}
return 0;
}
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
int main()
{
auto data = {1, 2, 3, 5, 7};
std::vector<int> v(4);ф
// скопирует только нечетные элементы
auto it = std::copy_if(std::begin(data), std::end(data), std::begin(v), [](auto value) {
return value % 2 != 0;
});
for (auto x : v)
{
std::cout << x << std::endl;
}
return 0;
}
#include <iostream> #include <algorithm> #include <string> #include <vector> int main() { auto data = {1, 2, 3, 5, 7}; std::vector<int> v(4);ф // скопирует только нечетные элементы auto it = std::copy_if(std::begin(data), std::end(data), std::begin(v), [](auto value) { return value % 2 != 0; }); for (auto x : v) { std::cout << x << std::endl; } return 0; }
1
3
5
7
1
3
5
7
1 3 5 7
Changelog
C++26
TODOC++23
TODOC++20
TODOC++17
TODOC++14
TODOC++11
TODOSee also
TODO
This page was last modified on 2024-03-08