Vector Pair in C++17 Mar 2025 | 4 min read What is a Vector in C++?In C++, a vector is a sequence container that stores elements of the same type in a contiguous block of memory. Each element in a vector is assigned a numerical index, which is used to access the element. Vectors are similar to arrays, but they have the advantage of being able to grow dynamically and shrink in size. This means that we can add and remove elements from a vector without specifying the vector's size in advance. What is Vector Pair in C++?A vector pair is a pair of two vectors that are used to store related data. For example, you might use a vector pair to store a point's x and y coordinates in a two-dimensional space. The first vector in the pair would contain the x coordinates, and the second vector would contain the corresponding y coordinates. To use a vector pair in C++, we must include the <vector> and <utility> headers in our code. The <vector> header provides the std::vector class, which is used to create vectors, and the <utility> header provides the std::pair class, which is used to create vector pairs. Vector Pair Syntax in C++Example-1: Here is a simple program that demonstrates how to use a vector in C++: Output: ![]() Explanation: The above code is used to demonstrate the vector in c++.In this code, we have created a vector of integers and added some initial values. We then printed the elements of the vector, added a new element to the vector, and printed the elements of the vector again to show that the vector has grown in size. Example-2: Here is an example of how to use a vector pair in C++: Output: ![]() Explanation: The above code is written to demonstrate the vector pair in c++. In this code, we have created a vector pair and added x and y coordinates. We then print the coordinates to the console. Note that the x and y coordinates are stored in separate vectors, accessed using the vector pair's first and second members. Once we have created the vector pair, we can add elements to the individual vectors in the pair using the push_back() method, which is inherited from the std::vector class. In this example, we added three x coordinates to the first vector in the pair and three y coordinates to the second vector in the pair. Vector pairs are useful for storing and manipulating related data in C++. They can be used in many applications, from simple geometry problems to more complex data structures and algorithms. Example-3 Output: ![]() Explanation: The above code is written to demonstrate the vector pair in c++. In this code, we have created a vector pair to store the names and ages of a group of people. We add names and ages to the vector pair and print them out. Once we have created the vector pair, we can add elements to the individual vectors in the pair using the push_back() method, which is inherited from the std::vector class. In this example, we add three x coordinates to the first vector in the pair and three y coordinates to the second vector in the pair. Finally, we iterate over the elements of the vector pair and print the coordinates of the point. To access the elements of the vector pair, we use the first and second members of the std::pair class. These members refer to the first and second vectors in the pair, respectively. We can then use the usual indexing syntax to access the individual elements of the vectors. Next TopicWhat is a Token in C++ |
Clone a Linked List with next and Random Pointer in C++
Clone a Linked List with and Random Pointer in C++ In this article, you will learn about how to clone a linked list with and Random Pointer in C++. Each node in a linked list of size N has two connections: one points to the node...
3 min read
Binary Operator Overloading in C++
This section will discuss the Binary Operator Overloading in the C++ programming language. An operator which contains two operands to perform a mathematical operation is called the Binary Operator Overloading. It is a polymorphic compile technique where a single operator can perform various functionalities by taking...
5 min read
Queries for Sum of Bitwise AND of all Subarrays in a Range in C++
Introduction: In this article, the task is to find the sum of bitwise AND operation results for all possible subarrays within a given range of indices in an array. Bitwise AND is an operation that takes two binary numbers and performs a logical AND operation on each...
11 min read
What is Top-Down Approach in C++
In C++, a top-down approach is a programming methodology that involves starting with an overview of the problem, breaking it down into smaller sub-problems, and then gradually building the solution by implementing each sub-problem in a hierarchical manner. This approach is also known as the "dividing...
3 min read
Add two Numbers using Class in C++
The below code is an implementation of adding two numbers in C++ using a class. A class is a blueprint or a structure that defines the data members and member functions of an object. In this code, the class Addition is used to add two numbers. The...
4 min read
Expected Unqualified Id Error in C++
When the C++ compiler comes across a statement or expression that it doesn't comprehend or that isn't correctly written by the language's rules, it usually produces the "Expected unqualified id" error. The "expected unqualified id" error in C++ generally indicates that the compiler encountered a situation...
4 min read
Wide Character in C++
Introduction: Wide char is comparable to the char data type, however, wide char takes up twice as much space and can therefore accommodate significantly larger values. The 256 possible values for char correspond to the ASCII table's entries. Contrarily, wide char can accept up to 65536 values,...
5 min read
Ostream in C++
To interface with external files or devices, log data, and display information to users, C++ output procedures are crucial. Through the usage of streams, C++ offers an effective method for processing output. Streams are collections of letters that represent a data source or destination abstractly and enable...
9 min read
std::string::crbegin() and std::string::crend() in C++
In C++, std::string::crbegin() and std::string::crend() are functions that are members of the std::string class, which was added in C++11. They provide access to a string's reversed iterators, allowing users to iterate by traversing the string's elements in the opposite direction. In this article, we will discuss...
2 min read
Call by address in C++
Call By Address is also referred to as Call By Pointers. In this Call By Address method, the developer passes the actual arguments addresses to the formal parameters. After that, the function utilizes these addresses to access the actual arguments in the system. In other words,...
4 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India


