Sequence container (C++)
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:
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 History
Originally, only The The Properties
VectorThe elements of a 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 reallocationA typical vector implementation consists, internally, of a pointer to a dynamically allocated array,[1] 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.[1][5] 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.[6] 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.[7] 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.[8] 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 boolThe Standard Library defines a specialization of the ListThe 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 operation. No shifting is required like with vectors. Lists do not have random-access ability like vectors ( operation). Accessing a node in a list is an 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 Forward listThe Deque
Array
Overview of functionsThe containers are defined in headers named after the names of the containers, e.g. Member functions
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 Operations
Non-member functionsUsage exampleThe 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. #include <iostream>
#include <vector>
#include <array>
#include <algorithm> // sort, max_element, random_shuffle, remove_if, lower_bound
#include <functional> // greater
#include <iterator> // begin, end, cbegin, cend, distance
#include <random> // 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<int> 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
Notes
|
Portal di Ensiklopedia Dunia