C++ - Standard library - Using the pair container

The pair container cannot be iterated.

So we can use 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.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Refresh Type the characters you see in this picture. Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.