The C++ Standard Library
- C++ Library - <valarray>
- C++ Library - <utility>
- C++ Library - <typeinfo>
- C++ Library - <tuple>
- C++ Library - <thread>
- C++ Library - <string>
- C++ Library - <stdexcept>
- C++ Library - <regex>
- C++ Library - <numeric>
- C++ Library - <new>
- C++ Library - <memory>
- C++ Library - <locale>
- C++ Library - <limits>
- C++ Library - <functional>
- C++ Library - <exception>
- C++ Library - <complex>
- C++ Library - <atomic>
- C++ Library - <streambuf>
- C++ Library - <sstream>
- C++ Library - <ostream>
- C++ Library - <istream>
- C++ Library - <iostream>
- C++ Library - <iosfwd>
- C++ Library - <ios>
- C++ Library - <iomanip>
- C++ Library - <fstream>
- C++ Library - Home
The C++ STL Library
- C++ Library - <iterator>
- C++ Library - <algorithm>
- C++ Library - <vector>
- C++ Library - <unordered_set>
- C++ Library - <unordered_map>
- C++ Library - <stack>
- C++ Library - <set>
- C++ Library - <queue>
- C++ Library - <map>
- C++ Library - <list>
- C++ Library - <forward_list>
- C++ Library - <deque>
- C++ Library - <bitset>
- C++ Library - <array>
C++ Programming Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
C++ Library - <vector>
Introduction
Vectors are sequence container that can change size. Container is a objects that hold data of same type. Sequence containers store elements strictly in pnear sequence.
Vector stores elements in contiguous memory locations and enables direct access to any element using subscript operator []. Unpke array, vector can shrink or expand as needed at run time. The storage of the vector is handled automatically.
To support shrink and expand functionapty at runtime, vector container may allocate some extra storage to accommodate for possible growth thus container have actual capacity greater than the size. Therefore, compared to array, vector consumes more memory in exchange for the abipty to manage storage and grow dynamically in an efficient way.
Zero sized vectors are also vapd. In that case vector.begin() and vector.end() points to same location. But behavior of calpng front() or back() is undefined.
Definition
Below is definition of std::vector from <vector> header file
template < class T, class Alloc = allocator<T> > class vector;
Parameters
T − Type of the element contained.
T may be substituted by any other data type including user-defined type.
Alloc − Type of allocator object.
By default, the allocator class template is used, which defines the simplest memory allocation model and is value-independent.
Member types
Following member types can be used as parameters or return type by member functions.
Sr.No. | Member types | Definition |
---|---|---|
1 | value_type | T (First parameter of the template) |
2 | allocator_type | Alloc (Second parameter of the template) |
3 | reference | value_type& |
4 | const_reference | const value_type& |
5 | pointer | value_type* |
6 | const_pointer | const value_type* |
7 | iterator | a random access iterator to value_type |
8 | const_iterator | a random access iterator to const value_type |
9 | reverse_iterator | std::reverse_iterator <iterator> |
10 | const_reverse_iterator | std::reverse_iterator <const_iterator> |
11 | size_type | size_t |
12 | difference_type | ptrdiff_t |
Functions from <vector>
Below is pst of all methods from <vector> header.
Constructors
Sr.No. | Method & Description |
---|---|
1 | Constructs an empty container, with zero elements. |
default constructor
2 | Constructs a container with n elements and assignd val to each element. |
fill constructor
3 | Constructs a container with as many elements in range of first to last. |
range constructor
4 | Constructs a container with copy of each elements present in existing container x. |
copy constructor
5 | Constructs the container with the contents of other using move semantics. |
move constructor
6 | Constructs a container from initiapzer pst. |
initiapzer pst constructor
Destructor
Sr.No. | Method & Description |
---|---|
1 | Destroys container by deallocating container memory. |
Member functions
Sr.No. | Method & Description |
---|---|
1 | Assign new values to the vector elements by replacing old ones. |
fill version
2 | Assign new values to the vector elements by replacing old ones. |
range version
3 | Assign new values to the vector elements by replacing old ones. |
initiapzer pst version
4 | Returns reference to the element present at location n in the vector. |
5 | Returns a reference to the last element of the vector. |
6 | Return a random access iterator pointing to the first element of the vector. |
7 | Returns the size of allocate storage, expressed in terms of elements. |
8 | Returns a constant random access iterator which points to the beginning of the vector. |
9 | Returns a constant random access iterator which points to the beginning of the vector. |
10 | Destroys the vector by removing all elements from the vector and sets size of vector to zero. |
11 | Returns a constant reverse iterator which points to the reverser beginning of the container. |
12 | Returns a constant reverse iterator which points to the reverse end of the vector. |
13 | Returns a pointer to the first element of the vector container. |
14 | Extends container by inserting new element at position. |
15 | Inserts new element at the end of vector. |
16 | Tests whether vector is empty or not. |
17 | Returns an iterator which points to past-the-end element in the vector container. |
18 | Removes single element from the the vector. |
position version
19 | Removes single element from the the vector. |
range version
20 | Returns a reference to the first element of the vector. |
21 | Returns an allocator associated with vector. |
22 | Extends iterator by inserting new element at position. |
single element version
23 | Extends vector by inserting new element in the container. |
fill version
24 | Extends vector by inserting new element in the container. |
range version
25 | Extends vector by inserting new element in the container. |
move version
26 | Extends vector by inserting new element in the container. |
initiapzer pst version
27 | Returns the maximum number of elements can be held by vector. |
28 | Assign new contents to the vector by replacing old ones and modifies size if necessary. |
copy version
29 | Assign new contents to the vector by replacing old ones and modifies size if necessary. |
move version
30 | Assign new contents to the vector by replacing old ones and modifies size if necessary. |
initiapzer pst version
31 | Returns a reference to the element present at location n. |
32 | Removes last element from vector and reduces size of vector by one. |
33 | Inserts new element at the end of vector and increases size of vector by one. |
34 | Returns a reverse iterator which points to the last element of the vector. |
35 | Returns a reverse iterator which points to the reverse end of the vector. |
36 | Requests to reserve vector capacity be at least enough to contain n elements. |
37 | Changes the size of vector. |
38 | Requests the container to reduce it s capacity to fit its size. |
39 | Returns the number of elements present in the vector. |
40 | Exchanges the content of vector with contents of vector x. |
Non-member overloaded functions
Sr.No. | Method & Description |
---|---|
1 | Tests whether two vectors are equal or not. |
2 | Tests whether two vectors are equal or not. |
3 | Tests whether first vector is less than other or not. |
4 | Tests whether first vector is less than or equal to other or not. |
5 | Tests whether first vector is greater than other or not. |
6 | Tests whether first vector is greater than or equal to other or not. |
7 | Exchanges the contents of two vector. |