Questions tagged [std-pair]

A std::pair is an ordered, heterogeneous sequence of exactly two objects (it is a special case of std::tuple).

std::pair is a part of the C++ Standard Library, defined in the header <utility>.

Reference: https://en.cppreference.com/w/cpp/utility/pair

776 questions
17
votes
1 answer

std::pair return type

I was playing around with auto in std::pair. In the below code, function f is supposed to return a std::pair of types which depend on a template parameter. A working example: EXAMPLE 1 template auto f() { if constexpr (S == 1) …
mfnx
  • 2,894
  • 1
  • 12
  • 28
17
votes
4 answers

How do I initialize a const std::pair?

Let's say that I've got a : #include using namespace std; typedef pair my_pair; how do I initialize a const my_pair ?
Maciek
  • 19,435
  • 18
  • 63
  • 87
16
votes
3 answers

Difference between make_pair and curly brackets { } for assigning a pair in C++?

I didn't find anyone answering this, is there any difference between the following : v.push_back({x, y}); and : v.push_back(make_pair(x, y)); Assuming that v was declared this way : vector > v;
16
votes
5 answers

Sorting a std::vector> by the string?

How can I sort this vector by comparing the pair.first which is an std::string? (without providing a static compare function, nor use boost).
jmasterx
  • 52,639
  • 96
  • 311
  • 557
16
votes
2 answers

Sorting a vector of pairs

I have a question about sorting a vector of pairs: std::vector> baryProc; this vector is already filled up with the pairs. Now I wanted to sort the pairs inside the vector based on the double value inside the…
user2633791
  • 185
  • 1
  • 2
  • 5
16
votes
2 answers

lower_bound of vector of pairs with lambda

I want to find the std::lower_bound of the a std::vector of std::pair's according to the second element with lambda. std::vector < std::pair > vec; vec.resize(5); auto it = std::lower_bound(vec.begin(), vec.end(), lambda); // what is…
Shibli
  • 5,879
  • 13
  • 62
  • 126
15
votes
1 answer

Why do `initializer_list` and `initializer_list` behave differently?

The following code compiles and runs : #include #include #include #include void ext( std::initializer_list >> myList ) { //Do…
Gael Lorieul
  • 3,006
  • 4
  • 25
  • 50
15
votes
5 answers

Equivalent of C++ STL container "pair" in Objective-C?

I'm new to Objective-C, so please don't judge me too much. I was wondering: Is there an equivalent of the C++ STL pair container I can use in Objective-C? I want to build an array that contains an NSInteger associated to an NSBool. I know I could…
Michael Eilers Smith
  • 8,466
  • 20
  • 71
  • 106
15
votes
7 answers

Custom sort vector of pair based on their values

I have the following vector: std::vector< std::pair > vectorOfPairs with the following items: 0, 1 0, 2 1, 4 2, 3 3, 4 4, 5 5, 6 I would like to sort them in a way that the second component of every pair is equal to the first component…
Alex S. Diaz
  • 2,637
  • 2
  • 23
  • 35
14
votes
2 answers

How would one push back an empty vector of pairs to another vector?

std::vector > > offset_table; for (int i = 0; i < (offset.Width()*offset.Width()); ++i) { offset_table.push_back( std::vector< std::pair > ); } This is my code, but I am getting the…
pearbear
  • 1,025
  • 4
  • 18
  • 23
12
votes
3 answers

Different behavior when trying to swap two variables using {} and std::make_pair()

I was trying to swap two variables using std::tie() as per the following code (I am aware of std::swap, I was just trying this out of curiosity): #include #include using std::cin; using std::tie; using std::cout; using…
Shubham
  • 2,847
  • 4
  • 24
  • 37
12
votes
4 answers

Implementation of lower_bound on vector pairs

I know we need to include some compare function in order to achieve this. But not able to write for this one. For example: Elements of vector={(2,4),(4,2),(5,1),(5,3)} to find=5 lower_bound() should return 2 code-> #define pp pair bool…
user2826957
  • 303
  • 2
  • 3
  • 12
11
votes
1 answer

One liner tuple/pair unpack in c++ with reusing same variable multiple times

I have already seen Is there a one-liner to unpack tuple/pair into references? and know how the unpack values from tuple/pairs in a single line like following auto [validity, table] = isFieldPresentAndSet(r, "is_federated"); here…
Mayukh Sarkar
  • 2,289
  • 1
  • 14
  • 38
11
votes
3 answers

Forward declaration of objects with STL containers

Consider the following code snippet, where the first line serves only as forward declaration class A; followed by defining new class class B { vector Av; //line 1 map Am; //line 2 pair Ap; //line 3 }; line 1 and line 2…
11
votes
3 answers

Is there a NULL equivalent for pairs in C++?

What would I want to use instead of NULL if I have an unassigned pair in C++? As an example, suppose I have (pseudo)code like the following: pair bestPair; //Global variable updateBestPair(vector a, vector b) { bestPair =…
user1634426
  • 563
  • 2
  • 5
  • 12