Javatpoint Logo
Javatpoint Logo

Vector Pair in C++

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:

Vector Pair in C++

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:

Vector Pair in C++

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:

Vector Pair in C++

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.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA