Namespaces
Variants
Views
Actions

Difference between revisions of "Template:cpp/container/assign range"

From cppreference.com
m (Use the {{cpp/container/note iterator invalidation}} hub; WIP.)
 
(2 intermediate revisions by one user not shown)
Line 1: Line 1:
{{cpp/container/{{{1|}}}/title|assign_range}}
+
{{#vardefine:cont|{{{1|inplace_vector}}}}}<!--
{{cpp/container/{{{1|}}}/navbar}}
+
-->{{cpp/container/{{#var:cont}}/title|assign_range}}
 
+
{{cpp/container/{{#var:cont}}/navbar}}
{{ddcl|since=c++23|
+
{{ddcl|since={{#switch:{{#var:cont}}|inplace_vector=c++26|c++23}}|
 
template< container-compatible-range<T> R >
 
template< container-compatible-range<T> R >
{{#ifeq:{{{1}}}|vector|constexpr void|void}} assign_range( R&& rg );
+
{{#switch:{{#var:cont}}|inplace_vector|vector=constexpr void|void}} assign_range( R&& rg );
 
}}
 
}}
  
 
Replaces elements in the container with a copy of each element in {{c|rg}}.
 
Replaces elements in the container with a copy of each element in {{c|rg}}.
  
{{cpp/container/note iterator invalidation|{{{1|}}}}}
+
{{cpp/container/note iterator invalidation|{{#var:cont}}|assign_range}}
  
 
Each iterator in the range {{c|rg}} is dereferenced exactly once.
 
Each iterator in the range {{c|rg}} is dereferenced exactly once.
Line 20: Line 20:
 
{{par hreq}}
 
{{par hreq}}
 
{{par req|{{c|std::assignable_from<T&, ranges::range_reference_t<R>>}} must be modeled. Otherwise, the program is ill-formed.}}
 
{{par req|{{c|std::assignable_from<T&, ranges::range_reference_t<R>>}} must be modeled. Otherwise, the program is ill-formed.}}
{{par req|{{tt|T}} must be {{named req|EmplaceConstructible}} into the container from {{c|*ranges::begin(rg)}}{{#ifeq:{{{1}}}|vector|. If {{tt|R}} models neither {{lconcept|sized_range}} nor {{lconcept|forward_range}}, {{tt|T}} must be also {{named req|MoveInsertable}} into the container}}. Otherwise, the behavior is undefined.}}
+
{{par req|{{tt|T}} must be {{named req|EmplaceConstructible}} into the container from {{c|*ranges::begin(rg)}}{{#switch:{{#var:cont}}|vector=. If {{tt|R}} models neither {{lconcept|sized_range}} nor {{lconcept|forward_range}}, {{tt|T}} must be also {{named req|MoveInsertable}} into the container}}. Otherwise, the behavior is undefined.}}
 
{{par end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
 
(none)
 
(none)
 
+
<!---->
 +
{{#switch:{{#var:cont}}|inplace_vector=
 +
===Exceptions===
 +
* {{lc|bad_alloc}}, if {{c|1=std::ranges::distance(rg) > capacity()}}.
 +
* Any exception thrown by initialization of inserted element.
 +
}}
 +
<!---->
 +
{{#switch:{{#var:cont}}|inplace_vector=|
 
===Notes===
 
===Notes===
 
{{feature test macro|__cpp_lib_containers_ranges|[[cpp/ranges/to#container compatible range|Ranges-aware]] construction and insertion|value=202202L|std=C++23}}
 
{{feature test macro|__cpp_lib_containers_ranges|[[cpp/ranges/to#container compatible range|Ranges-aware]] construction and insertion|value=202202L|std=C++23}}
 
+
}}
 +
<!---->
 
===Example===
 
===Example===
{{example|code=
+
{{#switch:{{#var:cont}}
 +
|inplace_vector=
 +
{{example
 +
|code=
 
#include <algorithm>
 
#include <algorithm>
 
#include <cassert>
 
#include <cassert>
#include <{{{1}}}>
+
#include <initializer_list>
#include <{{#ifeq:{{{1|}}}|list|vector|list}}>
+
#include <inplace_vector>
 +
#include <iostream>
 +
#include <new>
  
 
int main()
 
int main()
 
{
 
{
     const auto source = std::{{#ifeq:{{{1|}}}|list|vector|list}}{2, 7, 1};
+
     const auto source = {1, 2, 3};
     auto destination = std::{{{1}}}{3, 1, 4};
+
     std::inplace_vector<int, 4> destination{4, 5};
 
     destination.assign_range(source);
 
     destination.assign_range(source);
 +
    assert(std::ranges::equal(destination, source));
 +
 +
    try
 +
    {
 +
        const auto bad = {-1, -2, -3, -4, -5};
 +
        destination.assign_range(bad); // throws: bad.size() > destination.capacity()
 +
    }
 +
    catch(const std::bad_alloc& ex)
 +
    {
 +
        std::cout << ex.what() << '\n';
 +
    }
 +
}
 +
|p=true
 +
|output=
 +
std::bad_alloc
 +
}}
 +
|
 +
{{example
 +
|code=
 +
#include <algorithm>
 +
#include <cassert>
 +
#include <{{#var:cont}}>
 +
#include <{{#ifeq:{{#var:cont}}|list|vector|list}}>
 +
 +
int main()
 +
{
 +
    const auto source = std::{{#ifeq:{{#var:cont}}|list|vector|list}}{2, 7, 1};
 +
    auto destination = std::{{#var:cont}}{3, 1, 4};
 +
#ifdef __cpp_lib_containers_ranges
 +
    destination.assign_range(source);
 +
#else
 +
    destination.assign(source.cbegin(), source.cend());
 +
#endif
 
     assert(std::ranges::equal(source, destination));
 
     assert(std::ranges::equal(source, destination));
 
}
 
}
 +
}}
 
}}
 
}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{#switch:{{{1|}}}|forward_list=
+
{{#switch:{{#var:cont}}|forward_list=
{{cpp/container/dsc insert_range_after|{{{1|}}}}}
+
{{cpp/container/dsc insert_range_after|{{#var:cont}}}}
|{{cpp/container/dsc insert_range|{{{1|}}}}}
+
|{{cpp/container/dsc insert_range|{{#var:cont}}}}
 
}}
 
}}
{{#switch:{{{1|}}}|deque|forward_list|list=
+
{{#switch:{{#var:cont}}|deque|forward_list|list=
{{cpp/container/dsc prepend_range|{{{1|}}}}}
+
{{cpp/container/dsc prepend_range|{{#var:cont}}}}
 
}}
 
}}
{{#switch:{{{1|}}}|deque|list|vector=
+
{{#switch:{{#var:cont}}|deque|list|inplace_vector|vector=
{{cpp/container/dsc append_range|{{{1|}}}}}
+
{{cpp/container/dsc append_range|{{#var:cont}}}}
 
}}
 
}}
{{cpp/container/dsc assign|{{{1|}}}}}
+
{{cpp/container/dsc assign|{{#var:cont}}}}
 +
{{cpp/container/dsc operator{{=}}|{{#var:cont}}}}
 
{{dsc end}}
 
{{dsc end}}

Latest revision as of 12:56, 18 August 2024

 
 
 
 
template< container-compatible-range<T> R >
constexpr void assign_range( R&& rg );
(since C++26)

Replaces elements in the container with a copy of each element in rg.

Each iterator in the range rg is dereferenced exactly once.

The behavior is undefined if rg overlaps with the container.

Contents

[edit] Parameters

rg - an input_range with reference type convertible to the element type of the container
Type requirements
-
std::assignable_from<T&, ranges::range_reference_t<R>> must be modeled. Otherwise, the program is ill-formed.
-
T must be EmplaceConstructible into the container from *ranges::begin(rg). Otherwise, the behavior is undefined.

[edit] Return value

(none)

Exceptions

  • bad_alloc, if std::ranges::distance(rg) > capacity().
  • Any exception thrown by initialization of inserted element.

[edit] Example

#include <algorithm>
#include <cassert>
#include <initializer_list>
#include <inplace_vector>
#include <iostream>
#include <new>
 
int main()
{
    const auto source = {1, 2, 3};
    std::inplace_vector<int, 4> destination{4, 5};
    destination.assign_range(source);
    assert(std::ranges::equal(destination, source));
 
    try
    {
        const auto bad = {-1, -2, -3, -4, -5};
        destination.assign_range(bad); // throws: bad.size() > destination.capacity()
    }
    catch(const std::bad_alloc& ex)
    {
        std::cout << ex.what() << '\n';
    }
}

Possible output:

std::bad_alloc

[edit] See also

inserts a range of elements
(public member function of std::inplace_vector<T,N>)
adds a range of elements to the end
(public member function of std::inplace_vector<T,N>)
assigns values to the container
(public member function of std::inplace_vector<T,N>)
assigns values to the container
(public member function of std::inplace_vector<T,N>)