×

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:-

MemberDescription
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:-

  1. Void flip();                                      Reverses all bits in the vector.
  2. Static void swad(reference i, reference j);   Exchanges the bits  specified by i and j.                  

Related Topics

Default arguments in C++

Arguments in a function are defined as the values supplied when the function is called. The source is the values supplied, and the destination is the receiving function. Let us...

3 minutes read.

How to create a table in C++

C++ is a programming language that has evolved as an improvement of c language. It includes an object-oriented archetype. C++ programming language is a compiled language that complies with the...

3 minutes read.

How to find the length of the vector in C++

Like dynamic arrays, vectors can automatically adjust their size when an element is added or removed, and the container manages its storage. Because vector items are stored in contiguous storage, iterators...

3 minutes read.

Single dimension array

C++ Array An array is a collection of data (elements) of the same data types. The elements of an array are allocated in contiguous memory allocation. Elements of the array are accessed through...

1 minute read.

Decimal to Binary in C++

What is the meaning of Decimal Numbers? Decimal numbers range from 0 to 9, there are a total of ten digits between 0 and 9. Any number with more than two...

3 minutes read.

Const Keyword in C++

The const programming language keyword will be covered in this section. The constant value that cannot change during program execution is defined using the const keywords. It implies that once...

9 minutes read.

C++ Static

What is the Static keyword? In C++, the keyword static is used to give an element some particular properties. Static elements are only given storage in the static storage region once...

4 minutes read.

Binary Operator Overloading in C++

The Binary Operator Overloading in the C++ programming language will be covered in this part. An operator which comprises two operands to execute a mathematical operation is termed the Binary...

6 minutes read.

System() function in C++

As a part of the c/c+ standard library, the system() function passes commands to be executed by the operating system’s command processor or terminal and returns the completed command. We...

2 minutes read.

Default Constructor

C++ Default Constructor A default constructor is such constructor which does not take any parameter. A constructor initializes the data member when a class object created. If a programmer does not create any...

2 minutes read.

Program to arrange an array in alternate positive and negative numbers

Let’s say, there is given an array arr, arrange the array in such a way that every positive number is followed by a negative number. If there are extra positive...

4 minutes read.

Hello World Program in C++

The steps for “Hello World” C++ program are as follows: Write a C++ code given below in an editor. Save the file with .cpp Compile the code using C++ compiler or using online...

3 minutes read.

How to call a void function in C++

Generally, any function has two types: 1. Void function: It doesn't return any value. 2. Non-void function: It returns some value. Program to call a void function in C++ #include <iostream> using namespace std;  void check() {  ...

2 minutes read.

Constructor Overloading in C++

A Constructor is a class member function that is used to initialize the class's objects. Constructors have no return type and are called automatically when an object is formed. Constructor Characteristics Constructors...

3 minutes read.

Array of Vectors in C++ STL

Prerequisites: C++ STL Arrays and C++ STL Vector. A group of items kept in consecutive memory region is known as an array. It is to group similar objects of the same...

4 minutes read.

How to enter a name in C++

A name is a string or array of characters or letters. The string is one of the most helpful data types offered by the C++ library. A string helps the...

4 minutes read.

C++ Goto

In this article, we will discuss the C++ goto statement with its syntax, use, key features, key points, pseudo code, and examples. What is the goto statement in C++? In C++, the...

4 minutes read.

Skyline Problem in C++

We have given n rectangular buildings in a 2-dimensional city. Here, to compute the Skyline of the given n rectangle structures in a two-dimensional metropolis while removing hidden lines, the...

3 minutes read.

C++ References

C++ References An alias is a reference variable that is another name for a variable already in existence. If a relation is initialized with a variable, it is possible to use...

3 minutes read.

C++ Overloading

C++ Overloading is a condition when two or more members have the same name with different parameter type or a different number of parameter. C++ overloading is two types: Function...

1 minute read.