×

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

C++ Friend Functions

In C++, friends are special functions that are not a part of a class yet have access to its private and protected members. The friend keyword is used to declare...

4 minutes read.

Armstrong Number using Do-While Loop in C++

What is Do-While Loop? An iterative loop that checks the condition at the end.The Do-While loop can be used whenever a test condition is specific, as the control enters the loop...

4 minutes read.

Copy elision in C++

The Copy Omission is another name for the Copy Elision. One of the several compiler optimization techniques is copy elision. It prevents items from being copied inadvertently. This Copy Elision approach...

3 minutes read.

Pure Virtual Function in C++ With Example Program

What is a Virtual Function? A virtual function is created inside a class with the keyword virtual. A virtual function does not have any value to be returned. Once a virtual...

3 minutes read.

C++ String Concatenation

C++ String Concatenation In this section, we will learn about C ++ String Concatenation, what it does, how it works, and will also see its programs. What is the String Concatenation? The + operator...

3 minutes read.

Reverse a Number in C++

In this tutorial, you'll learn how to utilize a C++ program to reverse a number entered by a user at runtime. Because there are various ways to write a C++...

3 minutes read.

Timsort Implementation Using C++

Timsort Implementation Using C++ The Timsort is a stable sorting algorithm that uses the idea of merge sort and insertion sort. It can also be called a hybrid algorithm of insertion...

3 minutes read.

Reverse function in C++

The function std::reverse() is included in the standard template library of C++. It takes in a beginning and ending iterator, reversing the order. To use the reverse statement, we need...

2 minutes read.

Pointer to Object in C++

What is a pointer? A pointer in C++ is used to point the variable by storing the address of the variable. In C++, to print the address of the variable, we...

4 minutes read.

C++ Memory Management

Memory management is a method of controlling computer memory and allocating memory space to applications to increase overall system performance. What is the purpose of memory management? Because the array contains homogeneous...

4 minutes read.

Program to convert infix to postfix expression in C++

Parentheses are frequently employed in mathematical formulas to make their interpretation easier to understand. However, with computers, parenthesis in an expression might lengthen the time it takes to find a...

7 minutes read.

Palindrome using For loop in C++

A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++ also allows...

6 minutes read.

C++ Pipe Tutorial

A pipe is a mechanism for inter-process communication (IPC) in a Unix-like operating system. It allows two or more processes to communicate with each other by sending and receiving data...

3 minutes read.

C++ Writing to file

In file handling, write() function is used to write data into the file. The write() uses ofstream or fstream library to write into the file. Syntax file-stream-class   file-stream-object;   file-stream-object.write((char *)&var , sizeof (var)); The write() takes two arguments. The first argument is the address of variable var...

2 minutes read.

Array program in C++

What is an Array? An array is a set of identically typed elements that are organized into contiguous memory locations and each element can be independently accessed using an index. We can...

16 minutes read.

Memset in C++

Memset () is a function in the C++ programming language that fills memory blocks. The value of "ch" is first converted to an unsigned character. In this case, "ch" denotes the...

3 minutes read.

fread() Function in C++ Programming

C++ language is used to make high-performance applications that can work efficiently, and it is one of the world's most popular languages. It is an object-oriented and high-level programming language;...

3 minutes read.

Abstract class in C++

In this article, you will get exposure to an abstract class in C++. We will discuss this topic using some practical examples too. To understand the abstract classes, you should...

6 minutes read.

C++ 11 vs C++ 14 vs C++ 17

C++ is a language that is used to create a high-performance application. C++ 11, C++ 14, and C++ 17 are the different version of C++. There are some differences between...

1 minute read.

Structure Sorting (By Multiple Rules) in C++

To understand the concept of Structure Sorting (By Multiple Rules) in C++, it is recommended to know the Structures concept in the C++ programming language. Here the scenario is pretty...

3 minutes read.