AppDividend
Latest Code Tutorials

Pair In C++ Example | C++ Pair Program

0

Pair In C++ Example is today’s topic. PAIR is a container that is present in the STL library of C++, which consists of two data elements or objects. The pair container is a simple container defined in <utility> header consisting of two data elements or objects.

Pair In C++

The first element in the pair is referenced as ‘first,’ and the second element is referenced as ‘second’.

Pair is heterogeneous i.e. it allows to store a variable of different data types.

Pair in C++ can be assigned, copied, and compared.

For accessing the elements, the user can use a variable name followed by a dot operator followed by the keyword first or second.

An array of objects allocated in the map are in pair by default in which all the ‘first’ elements are unique keys associated with their ‘second’ value objects.

Syntax

pair(datatype1, datatype2) Pair_name;

Parameters

  1. datatype1, datatype2: It denotes what types of values will be inserted in the pair.
  2. Pair_name: It is the name of the pair which will be used for accessing the elements followed by ‘first’ and ‘second.’

Let’s look at the example to make you clear about the syntax.

C++ Pair Program

#include <iostream>
#include <utility>

using namespace std;

int main()
{
  pair<int, char> PAIR1;
  PAIR1.first = 65;
  PAIR1.second = 'A';

  cout << PAIR1.first << " ";
  cout << PAIR1.second << endl;

  return 0;
}

See the output.

 

C++ Pair Program

Let’s look at another way to initialize values.

SYNTAX

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

Parameters

  1. datatype1, datatype2: It denotes what types of values will be inserted in the pair.
  2. Pair_name: It is the name of the pair which will be used for accessing the elements followed by ‘first’ and ‘second.’
  3. value1, value2: The values which have to be inserted in the pair.

Let’s look at the example to make you clear about the syntax.

C++ Pair Example

#include <iostream>
#include <utility>

using namespace std;

int main()
{
  pair<string, int> PAIR2("AppDividend", 100);

  cout << PAIR2.first << " ";
  cout << PAIR2.second << endl;

  return 0;
}

See the output.

 

C++ Pair Example

See the following program 3.

#include <iostream>
#include <utility>

using namespace std;

int main()
{
  pair<int, double> PAIR1;
  pair<string, char> PAIR2;

  cout << PAIR1.first;  //it is initialised to 0
  cout << PAIR1.second; //it is initialised to 0

  cout << " ";

  cout << PAIR2.first;  //it prints nothing i.e NULL
  cout << PAIR2.second; //it prints nothing i.e NULL

  return 0;
}

See the output.

 

Pair In C++

If the pair is not initialized with any value, then its first value of the pair will get automatically initialized.

Different ways to initialize Pair

pair p1;

It is a default way of expressing a pair.

pair p2(1, ‘a’);  

It is initialized with different data types.

pair p3(1, 10);

It is initialized with the same data type.

pair p4(p3); 

It is used for making a copy of p3.

Another way of creating pair is by using the member function make_pair.

Member Function

make_pair

SYNTAX:

Pair_name = make_pair (value1, value2);

The advantage of using make_par() function is that it allows making a pair of values without writing the types explicitly.

See the following program.

#include <iostream>
#include <utility>

using namespace std;

int main()
{
  pair<int, char> PAIR1;
  pair<string, double> PAIR2("AppDividend", 100);
  pair<string, double> PAIR3;

  PAIR1.first = 100;
  PAIR1.second = 'G';

  PAIR3 = make_pair("AppDivided is Best for C++", 4.0);

  cout << PAIR1.first << " ";
  cout << PAIR1.second << endl;

  cout << PAIR2.first << " ";
  cout << PAIR2.second << endl;

  cout << PAIR3.first << " ";
  cout << PAIR3.second << endl;

  return 0;
}

See the output.

 

Member Function in C++

Unlike other STL container operators can also be used with PAIR container.

Operators

Equal Operator (=)

It is used for assigning a new object for a pair object.

SYNTAX:

pair& operator= (const pair&pr);

Here, pr is assigned as the new content for the pair object.

Comparison operator (==):

It is used for comparing two pairs, i.e. ‘first’ value of first pair (Pair1.first) will be compared with ‘first’ value of second pair (Pair2.first) and ‘second’ value of first pair (Pair1.second) will be compared with ‘second’ value of second pair (Pair2.second).

Not Equal Operator (! =):

Not equal operator is used for comparing the ‘first’ value of two pairs. If both the values get equal, then it will check for the ‘second’ value of both the pairs.

Logical Operator (>=, <=):

It is used for checking the values of both the pairs and return 0 if it is false and 1 if it is true.

Let’s look at an example to make you clear about all these operators.

Now, let’s see the next program.

#include <iostream>
#include <utility>

using namespace std;

int main()
{
  pair<int, int> pair1 = make_pair(2, 10);
  pair<int, int> pair2 = make_pair(9, 10);

  cout << (pair1 == pair2) << endl; //comparision operator
  cout << (pair1 != pair2) << endl; //not equal operator
  cout << (pair1 >= pair2) << endl; //Logical operator
  cout << (pair1 <= pair2) << endl; //logical operator
  cout << (pair1 > pair2) << endl;  //logical operator
  cout << (pair1 < pair2) << endl;  //logical operator

  return 0;
}

See the output.

 

Logical Operator in C++

Swap

The swap() function is used for swapping the contents of one pair with another pair, i.e. pair1.first will be swapped with pair2.first and pair1.second will be swapped with pair2.second.

See the following code example.

#include <iostream>
#include <utility>

using namespace std;

int main()
{
  pair<char, int> pair1 = make_pair('A', 10);
  pair<char, int> pair2 = make_pair('B', 20);

  cout << "Before swapping:\n ";
  cout << "Contents of pair1 = " << pair1.first << " " << pair1.second;
  cout << "Contents of pair2 = " << pair2.first << " " << pair2.second;
  pair1.swap(pair2);

  cout << "After swapping:\n ";
  cout << "Contents of pair1 = " << pair1.first << " " << pair1.second;
  cout << "Contents of pair2 = " << pair2.first << " " << pair2.second;

  return 0;
}

See the output.

 

Swap in C++

Conclusion

We have discussed Pair as they are very commonly required in programming competitions, and they make sure things very easy to implement.

Finally, Pair In C++ Example | C++ Pair Program Tutorial is over.

Recommended Posts

Multiset in C++ Example

Stack in C++ Example

Priority Queue in C++ Example

Deque in C++ Example

Queues in C++ Example

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.