Sequence container (C++)#Vector

{{Short description|Group of standard library class templates}}

{{Use dmy dates|date=January 2021}}

{{C++ Standard library}}

In computing, sequence containers refer to a group of container class templates in the standard library of the C++ programming language that implement storage of data elements. Being templates, they can be used to store arbitrary elements, such as integers or custom classes. One common property of all sequential containers is that the elements can be accessed sequentially. Like all other standard library components, they reside in namespace std.

The following containers are defined in the current revision of the C++ standard: array, vector, list, forward_list, deque. Each of these containers implements different algorithms for data storage, which means that they have different speed guarantees for different operations:{{cite book | first = Nicolai | last = Josuttis | author-link = Nicolai Josuttis | year = 1999 | title = C++ Standard Library - A Tutorial and Reference | publisher = Addison-Wesley}}

Since each of the containers needs to be able to copy its elements in order to function properly, the type of the elements must fulfill CopyConstructible and Assignable requirements.ISO/IEC (2003). ISO/IEC 14882:2003(E): Programming Languages - C++ §23.1 Container requirements [lib.container.requirements] para. 4 For a given container, all elements must belong to the same type. For instance, one cannot store data in the form of both char and int within the same container instance.

History

{{expand section|date=December 2011}}

Originally, only vector, list and deque were defined. Until the standardization of the C++ language in 1998, they were part of the Standard Template Library (STL), published by SGI. Alexander Stepanov, the primary designer of the STL, bemoans the choice of the name vector, saying that it comes from the older programming languages Scheme and Lisp but is inconsistent with the mathematical meaning of the term.{{Cite book |last=Stepanov |first=Alexander A. |url=https://www.worldcat.org/oclc/898036481 |title=From mathematics to generic programming |date=2015 |others=Daniel E. Rose |isbn=978-0-13-349179-1 |location=Upper Saddle River, NJ |oclc=898036481}}

The array container at first appeared in several books under various names. Later it was incorporated into a Boost library, and was proposed for inclusion in the standard C++ library. The motivation for inclusion of array was that it solves two problems of the C-style array: the lack of an STL-like interface, and an inability to be copied like any other object. It firstly appeared in C++ TR1 and later was incorporated into C++11.

The forward_list container was added to C++11 as a space-efficient alternative to list when reverse iteration is not needed.

Properties

array, vector and deque all support fast random access to the elements. list supports bidirectional iteration, whereas forward_list supports only unidirectional iteration.

array does not support element insertion or removal. vector supports fast element insertion or removal at the end. Any insertion or removal of an element not at the end of the vector needs elements between the insertion position and the end of the vector to be copied. The iterators to the affected elements are thus invalidated. In fact, any insertion can potentially invalidate all iterators. Also, if the allocated storage in the vector is too small to insert elements, a new array is allocated, all elements are copied or moved to the new array, and the old array is freed. deque, list and forward_list all support fast insertion or removal of elements anywhere in the container. list and forward_list preserves validity of iterators on such operation, whereas deque invalidates all of them.

= Vector =

The elements of a vector are stored contiguously.{{cite tech report| publisher=ISO/IEC | date=2003 |title= ISO/IEC 14882 ISO/IEC 14882:2003(E): Programming Languages - C++ §23.2.4 Class template vector (lib.vector) paragraph.1|url=http://staff.ustc.edu.cn/~zhuang/cpp/specs/ISO_IEC%2014882%202003.pdf| page=489 | archive-url=https://web.archive.org/web/20220225224452/http://staff.ustc.edu.cn/~zhuang/cpp/specs/ISO_IEC%2014882%202003.pdf | quote=elements of a vector are stored contiguously, meaning that if v is a vector where T is some type other than bool, then it obeys the identity &v[n] == &v[0] + n for all 0 <= n < v.size(). | archive-date=2022-02-25}} Like all dynamic array implementations, vectors have low memory usage and good locality of reference and data cache utilization. Unlike other STL containers, such as deques and lists, vectors allow the user to denote an initial capacity for the container.

Vectors allow random access; that is, an element of a vector may be referenced in the same manner as elements of arrays (by array indices). Linked-lists and sets, on the other hand, do not support random access or pointer arithmetic.

The vector data structure is able to quickly and easily allocate the necessary memory needed for specific data storage, and it is able to do so in amortized constant time. This is particularly useful for storing data in lists whose length may not be known prior to setting up the list but where removal (other than, perhaps, at the end) is rare. Erasing elements from a vector or even clearing the vector entirely does not necessarily free any of the memory associated with that element.

== Capacity and reallocation ==

A typical vector implementation consists, internally, of a pointer to a dynamically allocated array, and possibly data members holding the capacity and size of the vector. The size of the vector refers to the actual number of elements, while the capacity refers to the size of the internal array.

When new elements are inserted, if the new size of the vector becomes larger than its capacity, reallocation occurs.ISO/IEC (2003). ISO/IEC 14882:2003(E): Programming Languages - C++ §23.2.4.3 vector modifiers [lib.vector.modifiers] para. 1 This typically causes the vector to allocate a new region of storage, move the previously held elements to the new region of storage, and free the old region.

Because the addresses of the elements change during this process, any references or iterators to elements in the vector become invalidated.ISO/IEC (2003). ISO/IEC 14882:2003(E): Programming Languages - C++ §23.2.4.2 vector capacity [lib.vector.capacity] para. 5 Using an invalidated reference causes undefined behaviour.

The reserve() operation may be used to prevent unnecessary reallocations. After a call to reserve(n), the vector's capacity is guaranteed to be at least n.ISO/IEC (2003). ISO/IEC 14882:2003(E): Programming Languages - C++ §23.2.4.2 vector capacity [lib.vector.capacity] para. 2

The vector maintains a certain order of its elements, so that when a new element is inserted at the beginning or in the middle of the vector, subsequent elements are moved backwards in terms of their assignment operator or copy constructor. Consequently, references and iterators to elements after the insertion point become invalidated.ISO/IEC (2003). ISO/IEC 14882:2003(E): Programming Languages - C++ §23.2.4.3 vector modifiers [lib.vector.modifiers] para. 3

C++ vectors do not support in-place reallocation of memory, by design; i.e., upon reallocation of a vector, the memory it held will always be copied to a new block of memory using its elements' copy constructor, and then released. This is inefficient for cases where the vector holds plain old data and additional contiguous space beyond the held block of memory is available for allocation.

== Specialization for bool ==

The Standard Library defines a specialization of the vector template for bool. The description of this specialization indicates that the implementation should pack the elements so that every bool only uses one bit of memory.ISO/IEC (2003). ISO/IEC 14882:2003(E): Programming Languages - C++ §23.2.5 Class vector [lib.vector.bool] para. 1 This is widely considered a mistake.{{cite web| title=vector: More Problems, Better Solutions | url=http://www.gotw.ca/publications/N1211.pdf|accessdate= 28 November 2017 |date=August 1999 }}{{cite web| title=A Specification to deprecate vector| url=http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2204.html |accessdate= 28 November 2017 |date=March 2007 }} vector does not meet the requirements for a C++ Standard Library container. For instance, a container::reference must be a true lvalue of type T. This is not the case with vector::reference, which is a proxy class convertible to bool.ISO/IEC (2003). ISO/IEC 14882:2003(E): Programming Languages - C++ §23.2.5 Class vector [lib.vector.bool] para. 2 Similarly, the vector::iterator does not yield a bool& when dereferenced. There is a general consensus among the C++ Standard Committee and the Library Working Group that vector should be deprecated and subsequently removed from the standard library, while the functionality will be reintroduced under a different name.{{cite web| title=96. Vector is not a container | url=http://www.open-std.org/Jtc1/sc22/wg21/docs/lwg-closed.html#96 |accessdate= 28 June 2018}}

= List =

The list data structure implements a doubly linked list. Data is stored non-contiguously in memory which allows the list data structure to avoid the reallocation of memory that can be necessary with vectors when new elements are inserted into the list.

The list data structure allocates and deallocates memory as needed; therefore, it does not allocate memory that it is not currently using. Memory is freed when an element is removed from the list.

Lists are efficient when inserting new elements in the list; this is an {{tmath|O(1)}} operation. No shifting is required like with vectors.

Lists do not have random-access ability like vectors ({{tmath|O(1)}} operation). Accessing a node in a list is an {{tmath|O(n)}} operation that requires a list traversal to find the node that needs to be accessed.

With small data types (such as ints) the memory overhead is much more significant than that of a vector. Each node takes up sizeof(type) + 2 * sizeof(type*). Pointers are typically one word (usually four bytes under 32-bit operating systems), which means that a list of four byte integers takes up approximately three times as much memory as a vector of integers.

= Forward list =

The forward_list data structure implements a singly linked list.

= Deque =

deque is a container class template that implements a double-ended queue. It provides similar computational complexity to vector for most operations, with the notable exception that it provides amortized constant-time insertion and removal from both ends of the element sequence. Unlike vector, deque uses discontiguous blocks of memory, and provides no means to control the capacity of the container and the moment of reallocation of memory. Like vector, deque offers support for random-access iterators, and insertion and removal of elements invalidates all iterators to the deque.

= Array =

array implements a non-resizable array. The size is determined at compile-time by a template parameter. By design, the container does not support allocators because it's basically a C-style array wrapper.

Overview of functions

The containers are defined in headers named after the names of the containers, e.g. vector is defined in header . All containers satisfy the requirements of the [http://www.sgi.com/tech/stl/Container.html Container] concept, which means they have begin(), end(), size(), max_size(), empty(), and swap() methods.

= Member functions =

class="wikitable" style="font-size:en"
Functions

! [http://en.cppreference.com/w/cpp/container/array array]
(C++11)

! [http://en.cppreference.com/w/cpp/container/vector vector]

! [http://en.cppreference.com/w/cpp/container/deque deque]

! [http://en.cppreference.com/w/cpp/container/list list]

! [http://en.cppreference.com/w/cpp/container/forward_list forward_list]
(C++11)

! Description

rowspan=4 | Basics

| rowspan=3 {{n/a|(implicit)}}

| [http://en.cppreference.com/w/cpp/container/vector/vector (constructor)]

| [http://en.cppreference.com/w/cpp/container/deque/deque (constructor)]

| [http://en.cppreference.com/w/cpp/container/list/list (constructor)]

| [http://en.cppreference.com/w/cpp/container/forward_list/forward_list (constructor)]

| Constructs the container from variety of sources

[http://en.cppreference.com/w/cpp/container/vector/~vector (destructor)]

| [http://en.cppreference.com/w/cpp/container/deque/~deque (destructor)]

| [http://en.cppreference.com/w/cpp/container/list/~list (destructor)]

| [http://en.cppreference.com/w/cpp/container/forward_list/~forward_list (destructor)]

| Destructs the container and the contained elements

[http://en.cppreference.com/w/cpp/container/vector/operator= operator=]

| [http://en.cppreference.com/w/cpp/container/deque/operator= operator=]

| [http://en.cppreference.com/w/cpp/container/list/operator= operator=]

| [http://en.cppreference.com/w/cpp/container/forward_list/operator= operator=]

| Assigns values to the container

rowspan=2 {{n/a}}

| [http://en.cppreference.com/w/cpp/container/vector/assign assign]

| [http://en.cppreference.com/w/cpp/container/deque/assign assign]

| [http://en.cppreference.com/w/cpp/container/list/assign assign]

| [http://en.cppreference.com/w/cpp/container/forward_list/assign assign]

| Assigns values to the container

rowspan=1| Allocators

| [http://en.cppreference.com/w/cpp/container/vector/get_allocator get_allocator]

| [http://en.cppreference.com/w/cpp/container/deque/get_allocator get_allocator]

| [http://en.cppreference.com/w/cpp/container/list/get_allocator get_allocator]

| [http://en.cppreference.com/w/cpp/container/forward_list/get_allocator get_allocator]

| Returns the allocator used to allocate memory for the elements

rowspan=5 | Element
access

| [http://en.cppreference.com/w/cpp/container/array/at at]

| [http://en.cppreference.com/w/cpp/container/vector/at at]

| [http://en.cppreference.com/w/cpp/container/deque/at at]

| rowspan=2 {{n/a}} || rowspan=2 {{n/a}} || Accesses specified element with bounds checking.

[http://en.cppreference.com/w/cpp/container/array/operator_at operator[]]

| [http://en.cppreference.com/w/cpp/container/vector/operator_at operator[]]

| [http://en.cppreference.com/w/cpp/container/deque/operator_at operator[]]

| Accesses specified element without bounds checking.

[http://en.cppreference.com/w/cpp/container/array/front front]

| [http://en.cppreference.com/w/cpp/container/vector/front front]

| [http://en.cppreference.com/w/cpp/container/deque/front front]

| [http://en.cppreference.com/w/cpp/container/list/front front]

| [http://en.cppreference.com/w/cpp/container/forward_list/front front]

| Accesses the first element

[http://en.cppreference.com/w/cpp/container/array/back back]

| [http://en.cppreference.com/w/cpp/container/vector/back back]

| [http://en.cppreference.com/w/cpp/container/deque/back back]

| [http://en.cppreference.com/w/cpp/container/list/back back]

| rowspan=2 {{n/a}}

Accesses the last element
[http://en.cppreference.com/w/cpp/container/array/data data]

| [http://en.cppreference.com/w/cpp/container/vector/data data]

| {{n/a}}

{{n/a}}Accesses the underlying array
rowspan=4 | Iterators

| [http://en.cppreference.com/w/cpp/container/array/begin begin
cbegin]

| [http://en.cppreference.com/w/cpp/container/vector/begin begin
cbegin]

| [http://en.cppreference.com/w/cpp/container/deque/begin begin
cbegin]

| [http://en.cppreference.com/w/cpp/container/list/begin begin
cbegin]

| [http://en.cppreference.com/w/cpp/container/forward_list/begin begin
cbegin]

| Returns an iterator to the beginning of the container

[http://en.cppreference.com/w/cpp/container/array/end end
cend]

| [http://en.cppreference.com/w/cpp/container/vector/end end
cend]

| [http://en.cppreference.com/w/cpp/container/deque/end end
cend]

| [http://en.cppreference.com/w/cpp/container/list/end end
cend]

| [http://en.cppreference.com/w/cpp/container/forward_list/end end
cend]

| Returns an iterator to the end of the container

[http://en.cppreference.com/w/cpp/container/array/rbegin rbegin
crbegin]

| [http://en.cppreference.com/w/cpp/container/vector/rbegin rbegin
crbegin]

| [http://en.cppreference.com/w/cpp/container/deque/rbegin rbegin
crbegin]

| [http://en.cppreference.com/w/cpp/container/list/rbegin rbegin
crbegin]

| rowspan=2 {{n/a}}

Returns a reverse iterator to the reverse beginning of the container
[http://en.cppreference.com/w/cpp/container/array/rend rend
crend]

| [http://en.cppreference.com/w/cpp/container/vector/rend rend
crend]

| [http://en.cppreference.com/w/cpp/container/deque/rend rend
crend]

| [http://en.cppreference.com/w/cpp/container/list/rend rend
crend]

| Returns a reverse iterator to the reverse end of the container

rowspan=6 | Capacity

| [http://en.cppreference.com/w/cpp/container/array/empty empty]

| [http://en.cppreference.com/w/cpp/container/vector/empty empty]

| [http://en.cppreference.com/w/cpp/container/deque/empty empty]

| [http://en.cppreference.com/w/cpp/container/list/empty empty]

| [http://en.cppreference.com/w/cpp/container/forward_list/empty empty]

| Checks whether the container is empty

[http://en.cppreference.com/w/cpp/container/array/size size]

| [http://en.cppreference.com/w/cpp/container/vector/size size]

| [http://en.cppreference.com/w/cpp/container/deque/size size]

| [http://en.cppreference.com/w/cpp/container/list/size size]

| {{n/a}}

Returns the number of elements in the container.
[http://en.cppreference.com/w/cpp/container/array/max_size max_size]

| [http://en.cppreference.com/w/cpp/container/vector/max_size max_size]

| [http://en.cppreference.com/w/cpp/container/deque/max_size max_size]

| [http://en.cppreference.com/w/cpp/container/list/max_size max_size]

| [http://en.cppreference.com/w/cpp/container/forward_list/max_size max_size]

| Returns the maximum possible number of elements in the container.

rowspan=17 {{n/a}}

| [http://en.cppreference.com/w/cpp/container/vector/reserve reserve]

| rowspan=2 {{n/a}}

rowspan=3 {{n/a}}rowspan=3 {{n/a}}Reserves storage in the container
[http://en.cppreference.com/w/cpp/container/vector/capacity capacity]

| Returns the number of elements that can be held in currently allocated storage

[http://en.cppreference.com/w/cpp/container/vector/shrink_to_fit shrink_to_fit]

| [http://en.cppreference.com/w/cpp/container/deque/shrink_to_fit shrink_to_fit]

| Reduces memory usage by freeing unused memory (C++11)

rowspan=16 | Modifiers

| [http://en.cppreference.com/w/cpp/container/vector/clear clear]

| [http://en.cppreference.com/w/cpp/container/deque/clear clear]

| [http://en.cppreference.com/w/cpp/container/list/clear clear]

| [http://en.cppreference.com/w/cpp/container/forward_list/clear clear]

| Clears the contents

[http://en.cppreference.com/w/cpp/container/vector/insert insert]

| [http://en.cppreference.com/w/cpp/container/deque/insert insert]

| [http://en.cppreference.com/w/cpp/container/list/insert insert]

| rowspan=3 {{n/a}}

Inserts elements
[http://en.cppreference.com/w/cpp/container/vector/emplace emplace]

| [http://en.cppreference.com/w/cpp/container/deque/emplace emplace]

| [http://en.cppreference.com/w/cpp/container/list/emplace emplace]

| Constructs elements in-place (C++11)

[http://en.cppreference.com/w/cpp/container/vector/erase erase]

| [http://en.cppreference.com/w/cpp/container/deque/erase erase]

| [http://en.cppreference.com/w/cpp/container/list/erase erase]

| Erases elements

rowspan=3 {{n/a}}[http://en.cppreference.com/w/cpp/container/deque/push_front push_front]

| [http://en.cppreference.com/w/cpp/container/list/push_front push_front]

| [http://en.cppreference.com/w/cpp/container/forward_list/push_front push_front]

| Inserts elements to the beginning

[http://en.cppreference.com/w/cpp/container/deque/emplace_front emplace_front]

| [http://en.cppreference.com/w/cpp/container/list/emplace_front emplace_front]

| [http://en.cppreference.com/w/cpp/container/forward_list/emplace_front emplace_front]

| Constructs elements in-place at the beginning (C++11)

[http://en.cppreference.com/w/cpp/container/deque/pop_front pop_front]

| [http://en.cppreference.com/w/cpp/container/list/pop_front pop_front]

| [http://en.cppreference.com/w/cpp/container/forward_list/pop_front pop_front]

| Removes the first element

[http://en.cppreference.com/w/cpp/container/vector/push_back push_back]

| [http://en.cppreference.com/w/cpp/container/deque/push_back push_back]

| [http://en.cppreference.com/w/cpp/container/list/push_back push_back]

| rowspan=3 {{n/a}}

Inserts elements to the end
[http://en.cppreference.com/w/cpp/container/vector/emplace_back emplace_back]

| [http://en.cppreference.com/w/cpp/container/deque/emplace_back emplace_back]

| [http://en.cppreference.com/w/cpp/container/list/emplace_back emplace_back]

| Constructs elements in-place at the end (C++11)

[http://en.cppreference.com/w/cpp/container/vector/pop_back pop_back]

| [http://en.cppreference.com/w/cpp/container/deque/pop_back pop_back]

| [http://en.cppreference.com/w/cpp/container/list/pop_back pop_back]

| Removes the last element

rowspan=3 {{n/a}}rowspan=3 {{n/a}}rowspan=3 {{n/a}}

| [http://en.cppreference.com/w/cpp/container/forward_list/insert_after insert_after]

| Inserts elements after specified position (C++11)

[http://en.cppreference.com/w/cpp/container/forward_list/emplace_after emplace_after]

| Constructs elements in-place after specified position (C++11)

[http://en.cppreference.com/w/cpp/container/forward_list/erase_after erase_after]

| Erases elements in-place after specified position (C++11)

[http://en.cppreference.com/w/cpp/container/vector/resize resize]

| [http://en.cppreference.com/w/cpp/container/deque/resize resize]

| [http://en.cppreference.com/w/cpp/container/list/resize resize]

| [http://en.cppreference.com/w/cpp/container/forward_list/resize resize]

| Changes the number of stored elements

[http://en.cppreference.com/w/cpp/container/array/swap swap]

| [http://en.cppreference.com/w/cpp/container/vector/swap swap]

| [http://en.cppreference.com/w/cpp/container/deque/swap swap]

| [http://en.cppreference.com/w/cpp/container/list/swap swap]

| [http://en.cppreference.com/w/cpp/container/forward_list/swap swap]

| Swaps the contents with another container of the same type

[http://en.cppreference.com/w/cpp/container/array/fill fill]

| {{n/a}}

{{n/a}}{{n/a}}{{n/a}}Fills the array with the given value

There are other operations that are available as a part of the list class and there are algorithms that are part of the C++ STL (Algorithm (C++)) that can be used with the list and forward_list class:

== Operations ==

  • [http://en.cppreference.com/w/cpp/container/list/merge list::merge] and [http://en.cppreference.com/w/cpp/container/forward_list/merge forward_list::merge] - Merges two sorted lists
  • [http://en.cppreference.com/w/cpp/container/list/splice list::splice] and [http://en.cppreference.com/w/cpp/container/forward_list/merge forward_list::splice_after] - Moves elements from another list
  • [http://en.cppreference.com/w/cpp/container/list/remove list::remove] and [http://en.cppreference.com/w/cpp/container/forward_list/remove forward_list::remove] - Removes elements equal to the given value
  • [http://en.cppreference.com/w/cpp/container/list/remove list::remove_if] and [http://en.cppreference.com/w/cpp/container/forward_list/remove forward_list::remove_if] - Removes elements satisfying specific criteria
  • [http://en.cppreference.com/w/cpp/container/list/reverse list::reverse] and [http://en.cppreference.com/w/cpp/container/forward_list/reverse forward_list::reverse] - Reverses the order of the elements
  • [http://en.cppreference.com/w/cpp/container/list/unique list::unique] and [http://en.cppreference.com/w/cpp/container/forward_list/unique forward_list::unique] - Removes consecutive duplicate elements
  • [http://en.cppreference.com/w/cpp/container/list/sort list::sort] and [http://en.cppreference.com/w/cpp/container/forward_list/sort forward_list::sort] - Sorts the elements

= Non-member functions =

Usage example

The following example demonstrates various techniques involving a vector and C++ Standard Library algorithms, notably shuffling, sorting, finding the largest element, and erasing from a vector using the erase-remove idiom.

  1. include
  2. include
  3. include
  4. include // sort, max_element, random_shuffle, remove_if, lower_bound
  5. include // greater
  6. include // begin, end, cbegin, cend, distance
  7. include // per std::shuffle

/*

Explanation of Corrections (Giorgio Ruffa 02-23-2025):

Shuffle Error: The std::shuffle function requires a third argument that provides a random number generator. I added a random number generator based on std::random_device and std::mt19937.

Character Error: The quotes used around '\n' were non-standard characters (common in text editors or document formatting). I replaced them with the standard quotes for C++.

Lambda and erase Error: The closure of the call to remove_if was incorrect; a second argument must be provided to erase, which is the end iterator. I added end(numbers).

  • /

using namespace std;

int main()

{

array arr{ 1, 2, 3, 4 };

// initialize a vector from an array

vector numbers(cbegin(arr), cend(arr));

// insert more numbers into the vector

numbers.push_back(5);

numbers.push_back(6);

numbers.push_back(7);

numbers.push_back(8);

// the vector currently holds { 1, 2, 3, 4, 5, 6, 7, 8 }

// randomly shuffle the elements

random_device rd; // Seed for the random number generator

mt19937 g(rd()); // Mersenne Twister random number engine

shuffle(begin(numbers), end(numbers), g); // Aggiunto il generatore come terzo argomento

// locate the largest element, O(n)

auto largest = max_element(cbegin(numbers), cend(numbers));

cout << "The largest number is " << *largest << "\n";

cout << "It is located at index " << distance(largest, cbegin(numbers)) << "\n";

// sort the elements

sort(begin(numbers), end(numbers));

// find the position of the number 5 in the vector

auto five = lower_bound(cbegin(numbers), cend(numbers), 5);

// Corretto l'uso delle virgolette

cout << "The number 5 is located at index " << distance(five, cbegin(numbers)) << '\n';

// erase all the elements greater than 4

numbers.erase(

remove_if(begin(numbers),

end(numbers),

[](auto n) constexpr { return n > 4; }), // Aggiunto il ';' corretto qui

end(numbers)); // Aggiunto end(numbers) come secondo argomento per 'erase'

// print all the remaining numbers

for (const auto& element : numbers)

cout << element << " ";

}

The output will be the following:

The largest number is 8

It is located at index 6 (implementation-dependent)

The number 5 is located at index 4

1 2 3 4

References

  • William Ford, William Topp. Data Structures with C++ and STL, Second Edition. Prentice Hall, 2002. {{ISBN|0-13-085850-1}}. Chapter 4: The Vector Class, pp. 195–203.
  • {{Cite book |first=Nicolai M. |last=Josuttis |title=The C++ Standard Library |year=1999 |publisher=Addison-Wesley |isbn=0-201-37926-0 |url-access=registration |url=https://archive.org/details/cstandardlibrary00josu }}

Notes