Initialize Vector in C++
Initialize Vector in C++
The following comparison operators are defined for vector and those are given below.
==, <, <=, !=, >,>=
This allows you to access the element of a vector using standard array subscripting notation. Several of the member function definition of vector is given below. Some of the most common used member functions in vector are size(), begin(), end(), push_back(), insert(), and erase(). The size() function helps to return the current size of the vector.
This function is very much useful because it allows us to determine the size of the vector at the run time. But on the other size of the vector, it will increase the size of the vector as needed, so the size of a vector must be determined during execution, not during compilation.
The begin() function returns the iterators to the start of the vector.
The end() function returns an iterator to the end of the vector.
As explained, iterators are similar to pointers, and by the use of begin() and end() functions, you can bring the iterator to the beginning and the end of the vector.
The push_back() function puts the value into the end of a vector and if necessary, the length of the vector can be increased to accommodate the new element. We are also allowed to add element to the middle using insert(). A vector can also be initialized. At anytime, once a vector contains element, you can use array subscripting access or modify those element. We can also remove elements from the vector using the erase() function as this functions helps us in erasing the element of the vector.
Following member function in vector is as follows:-
|Void assign(inlter start, intlter end);||Assigns the vector num element of value val.|
|Template<class Inlter> Voidassign(Inlter start, Inlter end);||Assigns the vector the sequence defined by start and end.|
|Reference at(size_type i); Const_reference at(size_type i) const;||Returns a reference to the element specified by i.|
|Reference back(); Const_reference back() const;||Returns a reference to the last element in the vector.|
|Size_type capacity() const;||Returns the current capacity of the vector. This is the number of elements it can hold before it will need to allocate more memory.|
|Void clear();||Removes all elements from the vector.|
|Bool empty() const;||Returns true if the invoking vector is empty and false otherwise.|
|Iterator end(); Const_interator end() const;||Returns an iterator to the end of the vector.|
|Iterator erase(iterator i);||Removes the element pointed to by i. returns an iterator to the element after the one removed.|
|Iterator erase(iterator start, iterator end);||Removes the element in the range start to end. Returns an iterator to the element after the last element is removed.|
|Reference front(); Const_reference front() const;||Returns a reference to the first element in the vector.|
|Allocator_type gets_allocator() const();||This returns the vector allocator.|
|Void insert(iterator I, size_type num, const T & val;||Insert num copies of val immediately before the element specified by i.|
|Iterator insert(iterator i, const T & val);||Insert val immediately before the elements specified by i. An iterator to the element is returned.|
|Void pop_back();||Removes the last element in the vector.|
|Void push_back (const T &val);||Adds an element with the value specified by val to the end of the vector.|
The STL in C++ is also containing the specialization of the vector for the Boolean values. Which includes all the functionalities of the vector and adds these two members.
Those two members are as follows:-
- Void flip(); Reverses all bits in the vector.
- Static void swad(reference i, reference j); Exchanges the bits specified by i and j.