Namespaces
Variants
Views
Actions

Difference between revisions of "Template:cpp/container/back"

From cppreference.com
(Be more precisely about undefined behavior.)
 
(9 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{cpp/container/{{{1|}}}/title | back}}
+
{{#vardefine:cont|{{{1|vector}}}}}<!--
{{cpp/container/{{{1|}}}/navbar}}
+
-->{{cpp/container/{{#var:cont}}/title|back}}
{{#switch:{{{1|}}}|array=
+
{{cpp/container/{{#var:cont}}/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl rev begin}}
+
{{#switch:{{#var:cont}}
{{dcl | until=c++17|
+
|array=
 +
{{dcl|num=1|since=c++11|notes={{mark constexpr since c++17}}|
 
reference back();
 
reference back();
 
}}
 
}}
{{dcl | since=c++17|
+
{{dcl|num=2|since=c++11|notes={{mark constexpr since c++14}}|
constexpr reference back();
+
const_reference back() const;
 +
}}
 +
|vector=
 +
{{dcl|num=1|notes={{mark constexpr since c++20}}|
 +
reference back();
 
}}
 
}}
{{dcl rev end}}
+
{{dcl|num=2|notes={{mark constexpr since c++20}}|
{{dcl rev begin}}
+
{{dcl | until=c++14 |
+
 
const_reference back() const;
 
const_reference back() const;
 
}}
 
}}
{{dcl | since=c++14 |
+
|inplace_vector=
 +
{{dcl|num=1|since=c++26|
 +
constexpr reference back();
 +
}}
 +
{{dcl|num=2|since=c++26|
 
constexpr const_reference back() const;
 
constexpr const_reference back() const;
 
}}
 
}}
{{dcl rev end}}
+
|
{{dcl end}}
+
{{dcl|num=1|since={{cpp/std|{{#var:cont}}}}|
|{{dcl begin}}
+
{{dcl | since={{cpp/std|{{{1|}}}}} |
+
 
reference back();
 
reference back();
 
}}
 
}}
{{dcl | since={{cpp/std|{{{1|}}}}}|
+
{{dcl|num=2|since={{cpp/std|{{#var:cont}}}}|
 
const_reference back() const;
 
const_reference back() const;
 
}}
 
}}
{{dcl end}}}}
+
}}
 +
{{dcl end}}
 +
 
 +
Returns a reference to the last element in the container.
  
Returns reference to the last element in the container.  
+
Calling {{tt|back}} on an empty container causes [[cpp/language/ub|undefined behavior]].
  
Calling {{tt|back}} on an empty container causes undefined behavior [[cpp/language/ub|undefined behavior]].
 
 
===Parameters===
 
===Parameters===
 
(none)
 
(none)
Line 42: Line 49:
  
 
===Notes===
 
===Notes===
For a container {{tt|c}}, the expression {{c|return c.back();}} is equivalent to {{c|{ auto tmp {{=}} c.end(); --tmp; return *tmp; } }}
+
For a non-empty container {{tt|c}}, the expression {{c|c.back()}} is equivalent to {{c|*std::prev(c.end())}}.
  
 
===Example===
 
===Example===
 
{{example
 
{{example
| The following code uses {{tt|back}} to display the last element of a {{c|std::{{{1}}}<char>}}:
+
|code=
| code=
+
#include <cassert>
#include <{{{1}}}>
+
#include <{{#var:cont}}>
#include <iostream>
+
 
+
 
int main()
 
int main()
 
{
 
{
     {{#ifeq:{{{1|}}}|array
+
     std::{{#var:cont}}<char{{#switch:{{#var:cont}}|inplace_vector|array=, 4}}> letters{'a', 'b', 'c', 'd'};
|std::{{{1}}}<char, 6> letters {'a', 'b', 'c', 'd', 'e', 'f'};
+
     assert(letters.back() == 'd');
|std::{{{1}}}<char> letters {'a', 'b', 'c', 'd', 'e', 'f'};
+
}}
+
     if (!letters.empty()) {
+
        std::cout << "The last character is: " << letters.back() << '\n';
+
    } 
+
 
}
 
}
| output=
 
The last character is f
 
 
}}
 
}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/container/dsc front |{{{1|}}}}}
+
{{dsc inc|cpp/container/dsc front|{{#var:cont}}}}
 +
{{dsc inc|cpp/container/dsc rbegin|{{#var:cont}}}}
 +
{{dsc inc|cpp/container/dsc end|{{#var:cont}}}}
 
{{dsc end}}
 
{{dsc end}}

Latest revision as of 15:30, 15 August 2024

 
 
 
 
reference back();
(1) (constexpr since C++20)
const_reference back() const;
(2) (constexpr since C++20)

Returns a reference to the last element in the container.

Calling back on an empty container causes undefined behavior.

Contents

[edit] Parameters

(none)

[edit] Return value

Reference to the last element.

[edit] Complexity

Constant.

[edit] Notes

For a non-empty container c, the expression c.back() is equivalent to *std::prev(c.end()).

[edit] Example

#include <cassert>
#include <vector>
 
int main()
{
    std::vector<char> letters{'a', 'b', 'c', 'd'};
    assert(letters.back() == 'd');
}

[edit] See also

access the first element
(public member function of std::vector<T,Allocator>) [edit]
returns a reverse iterator to the beginning
(public member function of std::vector<T,Allocator>) [edit]
(C++11)
returns an iterator to the end
(public member function of std::vector<T,Allocator>) [edit]