std::ranges:: move_backward, std::ranges:: move_backward_result
std::ranges
| Non-modifying sequence operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Modifying sequence operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Partitioning operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Sorting operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Binary search operations (on sorted ranges) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Set operations (on sorted ranges) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Heap operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Minimum/maximum operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Permutation operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Fold operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Operations on uninitialized storage | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Return types | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
헤더에 정의됨
<algorithm>
|
||
|
호출 서명
|
||
|
template
<
std::
bidirectional_iterator
I1,
std::
sentinel_for
<
I1
>
S1,
std::
bidirectional_iterator
I2
>
|
(1) | (C++20 이후) |
|
template
<
ranges::
bidirectional_range
R,
std::
bidirectional_iterator
I
>
requires
std::
indirectly_movable
<
ranges::
iterator_t
<
R
>
, I
>
|
(2) | (C++20 이후) |
|
도우미 타입
|
||
|
template
<
class
I,
class
O
>
using move_backward_result = ranges:: in_out_result < I, O > ; |
(3) | (C++20 이후) |
[first, last)
범위에 정의된 요소들을 다른 범위
[d_last - N, d_last)
로 이동합니다. 여기서
N
=
ranges::
distance
(
first, last
)
입니다. 요소들은 역순으로(마지막 요소가 먼저 이동됨) 이동되지만, 상대적 순서는 유지됩니다.
d_last
가
(
first, last
]
범위 내에 있는 경우의 동작은 정의되지 않습니다. 이러한 경우에는
ranges::move
를 대신 사용할 수 있습니다.
이동된 범위의 요소들은 여전히 적절한 타입의 유효한 값을 포함하지만, 이동 전과 반드시 같은 값일 필요는 없습니다.
각 정수
n
에 대해
0
≤ n
<
N
일 때,
*
(
d_last
-
n
)
=
ranges::
iter_move
(
last
-
n
)
를 사용한 것과 같습니다.
이 페이지에서 설명하는 함수형 개체들은 algorithm function objects (일반적으로 niebloids 로 알려진)입니다. 즉:
- 명시적 템플릿 인수 목록은 이들 중 어느 것을 호출할 때도 지정할 수 없습니다.
- 이들 중 어느 것도 인수 의존 이름 검색 에 보이지 않습니다.
- 이들 중 어느 것이 함수 호출 연산자 왼쪽의 이름으로 일반 비한정 이름 검색 에 의해 발견될 때, 인수 의존 이름 검색 이 억제됩니다.
목차 |
매개변수
| first, last | - | 이동할 요소들의 범위 를 정의하는 반복자-감시자 쌍 |
| r | - | 이동할 요소들의 범위 |
| d_last | - | 대상 범위의 끝 |
반환값
{ last, d_last - N } .
복잡도
참고 사항
겹치는 범위를 이동할 때, ranges::move 는 왼쪽으로 이동할 때(대상 범위의 시작이 소스 범위 밖에 있을 때) 적합한 반면, ranges::move_backward 는 오른쪽으로 이동할 때(대상 범위의 끝이 소스 범위 밖에 있을 때) 적합합니다.
가능한 구현
struct move_backward_fn { template<std::bidirectional_iterator I1, std::sentinel_for<I1> S1, std::bidirectional_iterator I2> requires std::indirectly_movable<I1, I2> constexpr ranges::move_backward_result<I1, I2> operator()(I1 first, S1 last, I2 d_last) const { auto i {last}; for (; i != first; *--d_last = ranges::iter_move(--i)) {} return {std::move(last), std::move(d_last)}; } template<ranges::bidirectional_range R, std::bidirectional_iterator I> requires std::indirectly_movable<ranges::iterator_t<R>, I> constexpr ranges::move_backward_result<ranges::borrowed_iterator_t<R>, I> operator()(R&& r, I d_last) const { return (*this)(ranges::begin(r), ranges::end(r), std::move(d_last)); } }; inline constexpr move_backward_fn move_backward {}; |
예제
#include <algorithm> #include <iostream> #include <string> #include <string_view> #include <vector> using Vec = std::vector<std::string>; void print(std::string_view rem, Vec const& vec) { std::cout << rem << "[" << vec.size() << "]: "; for (const std::string& s : vec) std::cout << (s.size() ? s : std::string{"·"}) << ' '; std::cout << '\n'; } int main() { Vec a{"▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"}; Vec b(a.size()); print("Before move:\n" "a", a); print("b", b); std::ranges::move_backward(a, b.end()); print("\n" "Move a >> b:\n" "a", a); print("b", b); std::ranges::move_backward(b.begin(), b.end(), a.end()); print("\n" "Move b >> a:\n" "a", a); print("b", b); std::ranges::move_backward(a.begin(), a.begin()+3, a.end()); print("\n" "Overlapping move a[0, 3) >> a[5, 8):\n" "a", a); }
가능한 출력:
Before move: a[8]: ▁ ▂ ▃ ▄ ▅ ▆ ▇ █ b[8]: · · · · · · · · Move a >> b: a[8]: · · · · · · · · b[8]: ▁ ▂ ▃ ▄ ▅ ▆ ▇ █ Move b >> a: a[8]: ▁ ▂ ▃ ▄ ▅ ▆ ▇ █ b[8]: · · · · · · · · Overlapping move a[0, 3) >> a[5, 8): a[8]: · · · ▄ ▅ ▁ ▂ ▃
참고 항목
|
(C++20)
|
요소 범위를 새로운 위치로 이동
(알고리즘 함수 객체) |
|
(C++20)
(C++20)
|
요소 범위를 새로운 위치로 복사
(알고리즘 함수 객체) |
|
(C++20)
|
요소 범위를 역순으로 복사
(알고리즘 함수 객체) |
|
(C++11)
|
요소 범위를 새로운 위치로 이동
(함수 템플릿) |
|
(C++11)
|
인수를 xvalue로 변환
(함수 템플릿) |