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
26
votes
3 answers

How can I store a pair of numbers in C++?

I'm trying to learn C++ and right now I'm writing a program that needs to output a list of pairs of integers. What is the best way to handle this? I don't have the boost library available on our linux computers at school, so I don't believe I can…
Mithrax
  • 7,603
  • 18
  • 55
  • 60
25
votes
5 answers

Can I use std::pair, but rename .first and .second member names?

A common design problem I run into, is that I bundle two variables together and then lose the ability to reference them in a meaningful way. std::pair cords; cord.first = 0; //is .first the x or y coordinate? cord.second = 0; //is .second…
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
24
votes
5 answers

Create a vector of pairs from a single vector in C++

I have a single even-sized vector that I want to transform into a vector of pairs where each pair contains always two elements. I know that I can do this using simple loops but I was wondering if there is a nice standard-library tool for this? It…
Schottky
  • 1,549
  • 1
  • 4
  • 19
24
votes
4 answers

Weird compiler error: Cannot convert parameter from 'int' to 'int &&'

What on earth is going on here? I'm trying to create a pair of an int and a string and I can create the pair if I use "magic values" but can't seem to pass in variables. std::vector > num_text; std::string text =…
user1277997
  • 285
  • 1
  • 2
  • 6
23
votes
6 answers

C++ std::transform vector of pairs->first to new vector

Sorry for a little bit beginner question. There are vector and vector of pairs typedef std::vector TItems; typedef std::vector < std::pair > TPairs; Is there any way to transform all first items in pair to another vector in one…
justik
  • 4,145
  • 6
  • 32
  • 53
23
votes
2 answers

Using move semantics with std::pair or std::tuple

Suppose you want to take advantage of move semantics, but one of your movable classes needs to be part of an std::pair. The purpose would be to create a function that returns an std::pair that can be treated as an rvalue, and forwarded along. But…
Channel72
  • 24,139
  • 32
  • 108
  • 180
21
votes
1 answer

why would std::equal_to cause dynamic allocation?

Consider the following simple example, where I am using std::equal_to to compare two std::pair. The operator new is overloaded so that it prints a message when allocations take place (live code here): #include…
linuxfever
  • 3,763
  • 2
  • 19
  • 43
21
votes
3 answers

pair pair as key of unordered_map issue

My code: typedef pair Pair tr1::unordered_map h; h.insert(make_pair(Pair(0,0),true)); Erorr undefined reference to `std::tr1::hash >::operator()(std::pair) const' Something I need to…
icn
  • 17,126
  • 39
  • 105
  • 141
21
votes
1 answer

Any STL data structure like pair that gives three items(types) instead of two?

Question 1: I'm using C++ 11, and I'm learning. I realize I can do this with two pairs: pair, <#class _T2#>>, <#class _T3#>> Is that the best way? Question 2: If I don't need different types, so same type for two items, is it a…
Arch1tect
  • 4,128
  • 10
  • 48
  • 69
20
votes
3 answers

Copy std::map into std::vector of pairs

I'm trying to copy a map into a vector of pair, so I can then sort the vector by the second data member of the pairs. I have resolved this doing like this: void mappedWordsListSorter(){ for (auto itr = mappedWordsList.begin(); itr !=…
Victor
  • 201
  • 2
  • 3
20
votes
1 answer

Why does std::make_pair not return a pair? Or does it?

My internal sanity check failed so I'm rerunning it on Stackoverflow. The following code: #include #include #include int main() { constexpr auto pair_of_ints = std::make_pair(1, 2); std::cerr <<…
rubenvb
  • 74,642
  • 33
  • 187
  • 332
20
votes
4 answers

Why doesn't std::pair have iterators?

Why doesn't std::pair have iterators? std::pair should provide iterator and const_iterator as well as begin() and end() -- just for their two members. I think it would be useful because then we could pass them into templated functions that expect…
Frank
  • 64,140
  • 93
  • 237
  • 324
19
votes
6 answers

STL map insertion efficiency: [] vs. insert

There are two ways of map insertion: m[key] = val; Or m.insert(make_pair(key, val)); My question is, which operation is faster? People usually say the first one is slower, because the STL Standard at first 'insert' a default element if 'key' is…
DerekFOol
  • 193
  • 1
  • 1
  • 5
19
votes
3 answers

Comparing two map::iterators: why does it need the copy constructor of std::pair?

The very simple code below compiles and links without a warning in C++98 but gives an incomprehensible compile error in C++11 mode. #include struct A { A(A& ); // <-- const missing }; int main() { std::map m; return…
Ali
  • 56,466
  • 29
  • 168
  • 265
18
votes
1 answer

When is it good to use std::pair?

My impression is that it is always better to define my own struct, such that I can use meaningful field names instead of first and second. One place where the standard uses std::pair is for accessing elements of std::map. first is the key, and…
Rémi
  • 3,705
  • 1
  • 28
  • 39
1 2
3
51 52