Difference between revisions of "cpp/container/inplace vector/try push back"
From cppreference.com
< cpp | container | inplace vector
(+page, +example.) |
m (fmt.) |
||
(One intermediate revision by one user not shown) | |||
Line 3: | Line 3: | ||
{{dcl begin}} | {{dcl begin}} | ||
{{dcl|num=1|since=c++26| | {{dcl|num=1|since=c++26| | ||
− | constexpr | + | constexpr pointer try_push_back( const T& value ); |
}} | }} | ||
{{dcl|num=2|since=c++26| | {{dcl|num=2|since=c++26| | ||
− | constexpr | + | constexpr pointer try_push_back( T&& value ); |
}} | }} | ||
{{dcl end}} | {{dcl end}} | ||
Line 15: | Line 15: | ||
@1@ The new element is initialized as a copy of {{c|value}}. | @1@ The new element is initialized as a copy of {{c|value}}. | ||
@2@ {{c|value}} is moved into the new element. | @2@ {{c|value}} is moved into the new element. | ||
− | + | ||
{{cpp/container/note iterator invalidation|inplace_vector|try_push_back}} | {{cpp/container/note iterator invalidation|inplace_vector|try_push_back}} | ||
− | + | ||
===Parameters=== | ===Parameters=== | ||
{{par begin}} | {{par begin}} |
Latest revision as of 14:54, 23 September 2024
constexpr pointer try_push_back( const T& value ); |
(1) | (since C++26) |
constexpr pointer try_push_back( T&& value ); |
(2) | (since C++26) |
Conditionally appends the given element value to the end of the container.
If size() == capacity() is true, there are no effects. Otherwise, appends an object of type T
:
1) The new element is initialized as a copy of value.
2) value is moved into the new element.
No iterators or references are invalidated, except end()
, which is invalidated if the insertion occurs.
Contents |
[edit] Parameters
value | - | the value of the element to append |
Type requirements | ||
-T must meet the requirements of EmplaceConstructible.
|
[edit] Return value
std::addressof(back()) if size() < capacity(), nullptr otherwise.
[edit] Complexity
Constant.
[edit] Exceptions
Any exception thrown by initialization of inserted element.
If an exception is thrown for any reason, these functions have no effect (strong exception safety guarantee).
[edit] Notes
This section is incomplete Reason: Explain the purpose of this API. |
[edit] Example
Run this code
#include <cassert> #include <inplace_vector> #include <string> int main() { std::inplace_vector<std::string, 2> pets; std::string dog{"dog"}; std::string* p1 = pets.try_push_back("cat"); // overload (1) assert(*p1 == "cat" and pets.size() == 1); std::string* p2 = pets.try_push_back(std::move(dog)); // overload (2) assert(*p2 == "dog" and pets.size() == 2); assert(pets[0] == "cat" and pets[1] == "dog"); assert(pets.size() == pets.capacity()); std::string* p3 = pets.try_push_back("bug"); assert(p3 == nullptr and pets.size() == 2); }
[edit] See also
adds an element to the end (public member function) | |
constructs an element in-place at the end (public member function) | |
adds a range of elements to the end (public member function) | |
tries to construct an element in-place at the end (public member function) | |
tries to add a range of elements to the end (public member function) | |
unconditionally constructs an element in-place at the end (public member function) | |
unconditionally adds an element to the end (public member function) | |
removes the last element (public member function) | |
creates a std::back_insert_iterator of type inferred from the argument (function template) |