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

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

Example-1: Here is a simple program that demonstrates how to use 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 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++:

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

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

Input Required

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