Using std::pair

std::pair vs Custom Struct

When should I use std::pair instead of defining my own struct or class?

Abstract art representing computer programming

The choice between using std::pair and defining your own struct or class depends on the specific requirements and semantics of your code. Here are some guidelines:

Use std::pair when:

  • You need to group two values together temporarily or pass them as a single unit.
  • The two values do not have a strong logical connection or a specific meaning.
  • You don't need to define any custom behavior or member functions for the grouped values.

Use a custom struct or class when:

  • The grouped values have a strong logical connection and represent a meaningful entity in your program.
  • You need to define custom behavior, member functions, or operators for the grouped values.
  • You want to provide a more expressive and self-explanatory name for the type.

Example of using std::pair:

#include <iostream>
#include <utility>

std::pair<int, int> getMinMax(int a, int b) {
  return (a < b) ?
    std::make_pair(a, b) : std::make_pair(b, a);  
}

int main() {
  auto [min, max] = getMinMax(5, 3);
  std::cout << "Min: " << min
    << ", Max: " << max;
}
Min: 3, Max: 5

In this example, std::pair is used to group and return the minimum and maximum values from the getMinMax function. The pair doesn't have a specific meaning beyond holding the min and max values temporarily.

Example of using a custom struct:

#include <iostream>

struct Point {
  int x;
  int y;

  void print() const {
    std::cout << "(" << x << ", " << y << ")";
  }
};

int main() {
  Point p{3, 5};
  p.print();
}
(3, 5)

In this case, we define a custom Point struct to represent a 2D point. The x and y values have a strong logical connection, and we provide a custom print member function to display the point in a specific format.

Consider the semantics and requirements of your code when deciding between std::pair and a custom struct or class.

This Question is from the Lesson:

Using std::pair

Master the use of std::pair with this comprehensive guide, encompassing everything from simple pair manipulation to template-based applications

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Using std::pair

Master the use of std::pair with this comprehensive guide, encompassing everything from simple pair manipulation to template-based applications

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 125 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved