Sequence container (C++)
In C++, sequence containers or sequence collections refer to a group of container class templates in the standard library 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: * std::array implements a compile-time non-resizable array. * std::bitset implements a compile-time bit set. * std::vector implements an array with fast random access and an ability to automatically resize when appending elements. * std::inplace_vector implements a resizable array with contiguous in-place storage. * std::deque implements a double-ended queue with comparatively fast random access. * std::list implements a doubly linked list. * std::forward_list implements a singly linked list. * std::hive implements an object pool. Each of these containers implements different algorithms for data storage, which means that they have different speed guarantees for different operations. There are also versions of these collections in namespace std::pmr (for polymorphic memory resources).…
In C++, sequence containers or sequence collections refer to a group of container class templates in the standard library 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: std::array implements a compile-time non-resizable array. std::bitset implements a compile-time bit set. std::vector implements an array with fast random access and an ability to automatically resize when appending elements. std::inplace_vector implements a resizable array with contiguous in-place storage. std::deque implements a double-ended queue with comparatively fast random access. std::list implements a doubly linked list. std::forward_list implements a singly linked list. std::hive implements an object pool. Each of these containers implements different algorithms for data storage, which means that they have different speed guarantees for different operations. There are also versions of these collections in namespace std::pmr (for polymorphic memory resources). These versions specify the optional template parameter Allocator as std::pmr::polymorphic_allocator . C++ further defines container adapters that wrap a sequence container: std::stack implements a stack with deque as the default underlying container. std::queue implements a queue with deque as the default underlying container. std::priority_queue implements a priority queue (by default max heap) with vector as the default underlying container. std::flat_set implements a "flat set" (stores a sorted set of unique keys) with vector as the default underlying container. std::flat_map implements a "flat map" (stores a sorted set of unique key-value pairs) with vector as the default underlying containers. std::flat_multiset implements a "flat multi-set" (stores a sorted set of keys) with vector as the default underlying container. std::flat_multimap implements a "flat multi-map" (stores a sorted set of key-value pairs) with vector as the default underlying containers. The "flat" collections ( std::flat_set , std::flat_map , std::flat_multiset , and std::flat_multimap ) use std::vector as the underlying container, unlike their "non-flat" equivalents ( std::set , std::map , std::multiset , and std::multimap ), which store data as red–black trees (hence being "flat"). View container types represent views over non-owning arrays of elements: std::span implements a non-owning view over a contiguous sequence of objects. std::mdspan implements a non-owning view over a multi-dimensional array. 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. 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. import std; using std::array; array a = {1, 2, 3}; // OK array a = {1, 2, 3}; // not OK: length not specified in declaration array a{1, 2, 3}; // OK: deduces type as array using CTAD // compare with C-style arrays: int a[3] = {1, 2, 3}; // OK: length specified as 3 int a[] = {1, 2, 3}; // OK: length inferred by compiler std::span is a close relative of array , for non-owning array views. std::mdspan is a multi-dimensional version of span , however these are view containers. === Bit set=== std::bitset implements a non-resizable bit set. The size N , an unsigned integer, must be determined at compile time as a template parameter, much like std::array . It is much more compact than std::vector , storing just N bits and a small amount of padding. It further supports bitwise operations, as well as accessing and modifying specific bits. It supports conversion to unsigned long as well as to strings, and testing/querying the number of active bits. However, unlike other containers, it does not support iterators. It is declared in header . It is essentially equivalent to java.util.BitSet in Java, System.Collections.BitArray in .NET, or the former std::collections::BitSet in Rust. For a dynamic bit set, third party libraries such as Boost provide classes such as boost::dynamic_bitset . While vector does store packed bits and can be dynamically sized, vector ::operator[] returns a proxy type vector ::reference (rather than bool& ), and lacks the bitwise operations provided by bitset . import std; using std::array; using std::mt19937; using std::random_device; using std::vector; int main(int argc, char* argv[]) { array arr{1, 2, 3, 4}; // initialize a vector from an array vector numbers(arr.cbegin(), arr.cend()); // 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 std::ranges::shuffle(numbers, g); // locate the largest element, O(n) int largest = std::ranges::max_element(numbers); int indexOfLargest = std::ranges::distance(numbers.cbegin(), largest); std::println("The largest number is {}, located at index {}", largest, indexOfLargest); // sort the elements std::ranges::sort(numbers); // find the position of the number 5 in the vector int five = std::ranges::lower_bound(numbers, 5); int indexOfFive = std::ranges::distance(numbers.cbegin(), five); std::println("The number 5 is located at index {}", indexOfFive); // erase all the elements greater than 4 numbers.erase( std::ranges::remove_if( numbers, [](int n) constexpr -> bool { return n > 4; } ), numbers.end() ); // print all the remaining numbers for (int element: numbers) { std::print("{}", 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
No reference rows published for this article.
Cite this article
MLA
"Sequence container (C++)." encyclopegia.org, 2026, https://encyclopegia.org/wiki/sequence-container-c. Accessed 24 November 2026.
Chicago
"Sequence container (C++)." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/sequence-container-c.
APA
encyclopegia.org. (2026). Sequence container (C++). Retrieved November 24, 2026, from https://encyclopegia.org/wiki/sequence-container-c
Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON