A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://en.cppreference.com/w/cpp/language/../ranges/../iterator/move_iterator.html below:

std::move_iterator - cppreference.com

template< class Iter >
class move_iterator;

(since C++11)

std::move_iterator is an iterator adaptor which behaves exactly like the underlying iterator (which must be at least a LegacyInputIterator or model input_iterator(since C++20), or stronger iterator concept(since C++23)), except that dereferencing converts the value returned by the underlying iterator into an rvalue. If this iterator is used as an input iterator, the effect is that the values are moved from, rather than copied from.

[edit] Nested types (until C++20) (since C++20) [edit] Data members Member Description Iter current the underlying iterator
(exposition-only member object*) [edit] Member functions [edit] Non-member functions [edit] Helper templates template< class Iterator1, class Iterator2 >

    requires (!std::sized_sentinel_for<Iterator1, Iterator2>)
constexpr bool disable_sized_sentinel_for

    <std::move_iterator<Iterator1>, std::move_iterator<Iterator2>> = true;
(since C++20)

This partial specialization of std::disable_sized_sentinel_for prevents specializations of move_iterator from satisfying sized_sentinel_for if their underlying iterators do not satisfy the concept.

[edit] Notes [edit] Example
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <ranges>
#include <string>
#include <string_view>
#include <vector>
 
void print(const std::string_view rem, const auto& v)
{
    std::cout << rem;
    for (const auto& s : v)
        std::cout << std::quoted(s) << ' ';
    std::cout << '\n';
};
 
int main()
{
    std::vector<std::string> v{"this", "_", "is", "_", "an", "_", "example"};
    print("Old contents of the vector: ", v);
    std::string concat;
    for (auto begin = std::make_move_iterator(v.begin()),
              end = std::make_move_iterator(v.end());
         begin != end; ++begin)
    {
        std::string temp{*begin}; // moves the contents of *begin to temp
        concat += temp;
    }
 
    // Starting from C++17, which introduced class template argument deduction,
    // the constructor of std::move_iterator can be used directly:
    // std::string concat = std::accumulate(std::move_iterator(v.begin()),
    //                                      std::move_iterator(v.end()),
    //                                      std::string());
 
    print("New contents of the vector: ", v);
    print("Concatenated as string: ", std::ranges::single_view(concat));
}

Possible output:

Old contents of the vector: "this" "_" "is" "_" "an" "_" "example"
New contents of the vector: "" "" "" "" "" "" ""
Concatenated as string: "this_is_an_example"
[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior LWG 2106 C++11 dereferencing a move_iterator could return a dangling reference
if the dereferencing the underlying iterator returns a prvalue returns the
object instead LWG 3736 C++20 move_iterator was missing disable_sized_sentinel_for specialization added P2259R1 C++20 member iterator_category was defined even if
std::iterator_traits<Iter>::iterator_category is not defined iterator_category is
not defined in this case [edit] See also

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4