Vector Pair In C++ - C++ Programming Tutorial
C++ Course / STL Vector / Vector Pair In C++

Vector Pair In C++

BLUF: Mastering Vector Pair In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Vector Pair In C++

C++ is renowned for its efficiency. Learn how Vector Pair In C++ enables low-level control and high-performance computing in the tutorial below.

What is a Vector in C++?

In C++, a vector serves as a sequential container that holds elements of identical data type within a continuous memory block. Every element within a vector is allocated a numeric index, which is then utilized to retrieve the element. Vectors bear resemblance to arrays; however, they possess the capability to dynamically expand and contract in size. Consequently, it is feasible to append and delete elements from a vector without the need to predetermine the vector's size.

What is Vector Pair in C++?

A vector pair consists of two vectors utilized for storing interconnected data. For instance, one could employ a vector pair to hold a point's x and y values in a two-dimensional plane. The initial vector within the pair would store the x coordinates, while the subsequent vector would hold the associated 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

pair<std::vector<data_type>, std::vector<data_type>> Variable;

Below is a basic example showcasing the utilization of a vector in C++:

Example

#include <iostream>
#include <vector>
using namespace std;

int main() {
  // Create a vector of integers
  vector<int> v = {10, 20, 30, 40, 50};

  // Print the elements of the vector
  for (int i = 0; i < v.size(); i++) {
    cout << v[i] << " ";
  }
  cout << std::endl;

  // Add a new element to the vector
  v.push_back(60);

  // Print the elements of the vector again
  for (int i = 0; i < v.size(); i++) {
    cout << v[i] << " ";
  }
  cout << std::endl;

  return 0;
}

Output:

Explanation:

The provided code serves as an illustration of vectors in C++. Within this code snippet, a vector of integers is instantiated with certain initial values. Subsequently, the integer vector's elements are displayed, a new element is appended to the vector, and the elements are displayed once more to exhibit the vector's increased size.

Example-2: Presented below is a demonstration illustrating the utilization of a vector pair in the C++ programming language.

Example

#include <iostream>
#include <vector>
using namespace std;

int main() {
  // Create a vector pair
  spai<std::vector<int>, std::vector<int>> vp;

  // Add some x and y coordinates to the vector pair
  vp.first.push_back(10);
  vp.first.push_back(20);
  vp.first.push_back(30);
  vp.second.push_back(15);
  vp.second.push_back(25);
  vp.second.push_back(35);

  // Print the x and y coordinates
  for (int i = 0; i < vp.first.size(); i++) {
    cout << "(" << vp.first[i] << ", " << vp.second[i] << ")" << std::endl;
  }

  return 0;
}

Output:

Explanation:

The provided code showcases the usage of vector pairs in C++. Within this implementation, a vector pair is instantiated to contain x and y coordinates. Subsequently, the coordinates are displayed on the console. It is important to note that the x and y values are stored in distinct vectors, accessible via the first and second components of the vector pair. Following the creation of the vector pair, elements can be appended to the individual vectors using the push_back function, which is derived from the std::vector class. In this instance, three x coordinates were appended to the first vector, while three y coordinates were added to the second vector within the pair.

Vector pairs are valuable for managing and manipulating associated information in C++. They find application in various scenarios, ranging from basic geometry tasks to intricate data structures and algorithmic challenges.

Example-3

Example

#include <iostream>
#include <vector>
#include <utility>
#include <string>

int main() {
  // Create a vector pair to store the names and ages of a group of people
  std::pair<std::vector<std::string>, std::vector<int>> vp;

  // Add some names and ages to the vector pair
  vp.first.push_back("virat");
  vp.first.push_back("rohit");
  vp.first.push_back("rahul");
  vp.second.push_back(34);
  vp.second.push_back(35);
  vp.second.push_back(30);

  // Print the names and ages of the people
  for (int i = 0; i < vp.first.size(); i++) {
    std::cout << vp.first[i] << " is " << vp.second[i] << " years old." << std::endl;
  }

  return 0;
}

Output:

Explanation:

The provided code snippet showcases the implementation of a vector pair in C++. Within this code, a vector pair is utilized to store both names and ages of multiple individuals. By adding names and ages to the vector pair, the stored information can be displayed. Once the vector pair is established, elements can be appended to the separate vectors within the pair using the push_back function inherited from the std::vector class. In this instance, three x coordinates are included in the first vector, while three y coordinates are added to the second vector. Subsequently, a loop is employed to iterate through the vector pair's elements and exhibit the point's coordinates. Accessing elements within the vector pair is achieved by utilizing the first and second components of the std::pair class. These components correspond to the initial and secondary vectors within the pair, respectively. Consequently, conventional indexing notation can be applied to retrieve individual elements from the vectors.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience