C++ - Standard library - Using the pair container

The pair container cannot be iterated.

So we can use it for example to return a double value at the end of a function.

In the snippet below we are going to use it to change the first and the second value.
This, respectively with the pair.first and pair.second methods:

 

#include <iostream>

#include <string>


int main()

{

std::pair<int, std::string> myPair(1, "John");


std::cout << "Id: " << myPair.first << " - Name: " << myPair.second << std::endl;

myPair.first = 38;

myPair.second = "William";

std::cout << "Id: " << myPair.first << " - Name: " << myPair.second << std::endl;

return 0;

}
 
Result:
 
Id: 1 - Name: John

Id: 38 - Name: William
 

 

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.