C++ Pair Container: A Comprehensive Guide (2024)

In C++, a set is an associative container that holds unique objects. Once you apply an aspect to a specific, you cannot change it. To modify them, one can only delete and add components.

C++ pair is a type that is specified under the utility> header and is used to connect two pair values. The pair's values can be of separate or identical types. To view the values in a pair independently, the class has the member functions first() and second().

You set the order in which pair elements appear (first, second). To combine two heterogeneous values of different kinds, you use pairs.

What is C++ pair?

The C++ pair container is a basic header container that holds two data elements or objects. The order is set, and it refers to the first element as "first." It refers to the second element as "second" (first, second).

A pair is a term that is used to combine two values that may or may not be of the same kind. The C++ pair allows you to store two disparate items as a single entity.

It is possible to select, save, and compare pairs. By default, the list of objects assigned in a map or hashmap is of type ‘pair,' with all ‘first' components being special keys for their ‘second' value objects.

To get to the components, you use the vector name, the dot operator, and the keyword first or second.

pair (data_type1, data_type2) Pair_name

Example

// CPP program to illustrate pair STL

#include <iostream>

#include <utility>

using namespace std;

int main()

{

pair<int, char> PAIR1;

PAIR1.first = 100;

PAIR1.second = 'G';

cout << PAIR1.first << " ";

cout << PAIR1.second << endl;

return 0;

}

Output

C++ Pair Container: A Comprehensive Guide (1)

How Can You Initialize a C++ Pair?

pair (data_type1, data_type2) Pair_name (value1, value2) ;

Different Ways to Initialise a Pair

pair g1; //default

pair g2(1, 'a'); //initialized, different data type

pair g3(1, 10); //initialized, same data type

pair g4(g3); //copy of g3

Example:

// CPP program to illustrate

// Initializing of pair STL

#include <iostream>

#include <utility>

using namespace std;

int main()

{

pair<string, double> PAIR2("GeeksForGeeks", 1.23);

cout << PAIR2.first << " ";

cout << PAIR2.second << endl;

return 0;

}

Output

C++ Pair Container: A Comprehensive Guide (3)

What Are the Different Types of Member Functions?

Make_pair

This template feature allows you to construct a value pair without having to directly write the forms.

Pair_name = make_pair (value1,value2);

Example

#include <iostream>

#include <utility>

using namespace std;

int main()

{

pair <int, char> PAIR1 ;

pair <string, double> PAIR2 ("GeeksForGeeks", 1.23) ;

pair <string, double> PAIR3 ;

PAIR1.first = 100;

PAIR1.second = 'G' ;

PAIR3 = make_pair ("GeeksForGeeks is Best",4.56);

cout << PAIR1.first << " " ;

cout << PAIR1.second << endl ;

cout << PAIR2.first << " " ;

cout << PAIR2.second << endl ;

cout << PAIR3.first << " " ;

cout << PAIR3.second << endl ;

return 0;

}

Output:

C++ Pair Container: A Comprehensive Guide (4)

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program

C++ Pair Container: A Comprehensive Guide (5)

What Are the Different Types of Operators in C++?

You can also use operators for sets.

equal(=) adds a new object to a pair of objects.

Syntax: This assigns pr to the pair object's new text. The first value receives the first value of pr, while the second receives the second value of pr.

  • With C++ pair, use the comparison (==) operator: The comparison operator measures the first and second values of two sets, say pair1 and pair2, to see whether pair1.first is equal to pair2.first or not, and whether pair1.second is equal to pair2.second or not.
  • For pair, use the not equal (!=) operator: The!= operator compares the first values of two sets, say pair1 and pair2, i.e. if pair1 and pair2 are given, the!= operator compares the first values of those two pairs.
  • Pair of logical( >=, = )operators: The =, > operators can also be used for pairs, such as pair1 and pair2. It just compares the first value of the pair and returns 0 or 1.
  • P2p1 should return 0 for pairs like p1=(1,20) and p2=(1,10) (because it just compares the first variable and they are equal, so it can't be less), but this isn't the case. When the first element becomes equal when using the relational operators > or only, the pair compares the second element and returns 1 if it satisfies

Example

#include <iostream>

#include<utility>

using namespace std;

int main()

{

pair<char, int>pair1 = make_pair('A', 1);

pair<char, int>pair2 = make_pair('B', 2);

cout << "Before swapping:\n " ;

cout << "Contents of pair1 = "

<< pair1.first << " " << pair1.second ;

cout << "Contents of pair2 = "

<< pair2.first << " " << pair2.second ;

pair1.swap(pair2);

cout << "\nAfter swapping:\n ";

cout << "Contents of pair1 = "

<< pair1.first << " " << pair1.second ;

cout << "Contents of pair2 = "

<< pair2.first << " " << pair2.second ;

return 0;

}

Output

C++ Pair Container: A Comprehensive Guide (6)

Conclusion

This article pretty much sums up the C++ pair. For a full-fledged course in the subject, you must enroll in Simplilearn’s Post Graduate Program in Full Stack Development. Offered in collaboration with Caltech CTME. This comprehensive, globally-recognized applied learning program gives you everything you need to begin enjoying consisten and accelerated growth in your career graph as a full stack development professional anywhere in the world. For better understanding and getting hired by the best companies, you can also look for a course.

Do you have any questions for us regarding this C++ pair tutorial? If yes, do mention them in the comments section of this article. Our experts will get back to you on the same ASAP!

C++ Pair Container: A Comprehensive Guide (2024)

FAQs

Is pair a container in C++? ›

The C++ pair container is a basic header container that holds two data elements or objects. The order is set, and it refers to the first element as "first." It refers to the second element as "second" (first, second). A pair is a term that is used to combine two values that may or may not be of the same kind.

What is the difference between pair and Make_pair in C++? ›

The difference is that with std::pair you need to specify the types of both elements, whereas std::make_pair will create a pair with the type of the elements that are passed to it, without you needing to tell it.

What is pair <> in C++? ›

Pairs in C++ are containers that allow us to store two values as a single element. The values may or may not be of the same data type. Pairs store values as {first, second}, the order of values is fixed. The values can be accessed using the dot operator followed by keywords.

How do you access the first element of a pair? ›

Pair in C++ Standard Template Library (STL)
  1. The first element is referenced as 'first' and the second element as 'second' and the order is fixed (first, second).
  2. Pair can be assigned, copied, and compared. ...
  3. To access the elements, we use variable name followed by dot operator followed by the keyword first or second.
Jul 12, 2023

Are C++ pairs immutable? ›

Pairs in C++ are generally immutable, but sometimes we may need to modify or update the values stored in a pair. In this response, we will discuss how to modify and update pairs in C++ editor . To modify a pair in C++, you can create a new pair with the modified values and assign it to the original pair.

Is pair and map same in C++? ›

A pair stores a first and second item, which can be of different types. The pair template is parameterized by the types of these items. We can use a pair and a BST to implement a map: template <typename Key_type, typename Value_type> class Map { public: // EFFECTS: Returns whether this map is empty.

Can we compare two pairs in C++? ›

Compare pairs in C++

We can simply use the Equal to(==)comparison operator to check if two pairs are equal or not. For The two given pairs say pair1 and pair2, the Equal to(==) operator compares the “first value and second value of those two pairs i.e. if pair1. first is equal to pair2. first or not” and “if pair1.

Can a C++ function return a pair? ›

You can't do it directly (because a return value is singular). But, you could put a few values in a structure, and return that (like a pair<>). You cannot return two values from one function, but you could return a pointer to an array or some other structure which contains the two doubles.

Can you make an array of pairs in C++? ›

Pair can be assigned, copied and compared. The array of objects allocated in a map or hash_map are of type 'pair' by default in which all the 'first' elements are unique keys associated with their 'second' value objects.

Why does pair programming work? ›

Pair programming naturally promotes communication between developers as they work together on tasks, encouraging them to verbalize their thoughts as they tackle challenges as a team. This communication, in turn, helps the engineers improve their understanding of the problems at hand.

What happens if a pair is not initialized in C++? ›

Explanation: Both pair <type,type> p2 = p1; and pair <type,type> p2(p1); can be used to copy the data of one pair into other pair. 12. What happens if a pair is not initialized? Explanation: If a pair is not initialized then by default both parts of the pair is initialized to zero.

What elements exist in pair? ›

Diatomics on the Periodic Table. The 7 diatomic elements are hydrogen (H), nitrogen (N), oxygen (O), fluorine (F), chlorine (Cl), bromine (Br), and iodine (I). We call them diatomic elements because the atoms appear in pairs.

How do you sort elements in a pair? ›

This type of sorting can be achieved using simple “ sort() ” function. By default the sort function sorts the vector elements on basis of first element of pairs.

What is a container in C++? ›

In C++, containers are data structures that allow you to store and organize multiple elements of the same or different types. These containers offer various functionalities, such as dynamic memory management, automatic resizing, efficient insertion, deletion, and retrieval of elements.

How many types of containers are there in C++? ›

The Containers library is a generic collection of class templates and algorithms that allow programmers to easily implement common data structures like queues, lists and stacks. There are two(until C++11)three(since C++11) classes of containers: sequence containers, associative containers, and.

What are container classes in C++? ›

A container class is a data type that is capable of holding a collection of items. In C++, container classes can be implemented as a class, along with member functions to add, remove, and examine items.

Top Articles
Latest Posts
Article information

Author: Jonah Leffler

Last Updated:

Views: 6267

Rating: 4.4 / 5 (45 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Jonah Leffler

Birthday: 1997-10-27

Address: 8987 Kieth Ports, Luettgenland, CT 54657-9808

Phone: +2611128251586

Job: Mining Supervisor

Hobby: Worldbuilding, Electronics, Amateur radio, Skiing, Cycling, Jogging, Taxidermy

Introduction: My name is Jonah Leffler, I am a determined, faithful, outstanding, inexpensive, cheerful, determined, smiling person who loves writing and wants to share my knowledge and understanding with you.