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
11
votes
1 answer

c++ - How to get first and second element of pair if used as key in map?

I was trying to get first and second element of pair when i am using pair as key in map.For better clarification please see code below.This is what i have tried #include using namespace std; int main() { // your code goes…
chota bheem
  • 371
  • 3
  • 8
  • 20
11
votes
4 answers

Accessing a pair after moving it into a map

If I move a pair into a map, but the insert failed because the key already exists, can I safely use the pair afterwards? //objects available: map, pair auto insert_pair = map.insert(std::move(pair)); if (!insert_pair.second) { //can I safely…
roger.james
  • 1,478
  • 1
  • 11
  • 23
10
votes
6 answers

Is there a standard C++ function object for taking apart a std::pair?

Does anyone know if there's a de-facto standard (i.e., TR1 or Boost) C++ function object for accessing the elements of a std::pair? Twice in the past 24 hours I've wished I had something like the keys function for Perl hashes. For example, it…
Michael Kristofik
  • 34,290
  • 15
  • 75
  • 125
10
votes
4 answers

C++ Error Handling - downside of using std::pair or std::tuple for returning error codes and function returns

Without getting into the general exceptions vs error codes discussion, what do you think are the downsides of using std::pair or std:tuple for returning multiple values, namely the function's return value AND the error/success code, similar to how…
CodeSalad
  • 1,375
  • 2
  • 14
  • 22
9
votes
5 answers

Is it possible to cast a pair to a pair?

So I have a smart iterator that emulates a map const_iterator, and it needs to build the return type internally. Obviously, I'd like to store a pair in my iterator class (since I need to modify it), but at the same time I'd like the…
masaers
  • 697
  • 9
  • 21
9
votes
3 answers

Correct friend definition to give std::map access to private default constructor

I am working on a library which uses a struct which should not have the default constructor accessible by users of the lib. struct Example { Example(int x); private: Example(); }; Inside the library the the default constructor is…
Beginner
  • 5,277
  • 6
  • 34
  • 71
9
votes
1 answer

Do std::tuple and std::pair support aggregate initialization?

Aggregate initialization requires among other things no user-provided constructors. But std::tuple and std::pair pair have a large set of overloaded constructors. From the point of the core language, are these constructors user-provided or even…
Johan Lundberg
  • 26,184
  • 12
  • 71
  • 97
9
votes
2 answers

Changing value in a pair from foreach doesn't show change out of loop

I have an object outside of the for-each loops that contains a static vector of pair. I want to increment the second parameter of the pair, as the code below shows. However, when I print the second parameter from within another for-each loop, it…
user3812584
  • 103
  • 1
  • 5
9
votes
1 answer

How to std::hash an unordered std::pair

I want to be able to use a std::pair as a key in an unordered_container. I know that I could do this the following way: template void hash_combine(std::size_t &seed, T const &key) { std::hash hasher; seed ^= hasher(key) +…
101010
  • 41,839
  • 11
  • 94
  • 168
9
votes
3 answers

Problem with std::map and std::pair

I have a small program I want to execute to test something #include #include using namespace std; struct _pos{ float xi; float xf; bool operator<(_pos& other){ return this->xi < other.xi; …
Tom
  • 43,810
  • 29
  • 138
  • 169
9
votes
1 answer

Why can't you assign a pair from a tuple, but tuple can be assigned from a pair?

I'm not clear why it is legal to assign tuple=pair But it is illegal to assign pair=tuple std::pair x { 1 , 5.5}; std::tuple y { 1 , 5.5}; int a; double b; std::tie(a,b) = x; …
Glenn Teitelbaum
  • 10,108
  • 3
  • 36
  • 80
9
votes
4 answers

Will RVO happen when returning std::pair?

A function needs to return two values to the caller. What is the best way to implement? Option 1: pair myfunc() { ... return make_pair(getU(),getV()); } pair mypair = myfunc(); Option 1.1: // Same defn U u; V v; tie(u,v) =…
balki
  • 26,394
  • 30
  • 105
  • 151
9
votes
4 answers

ISO C++ forbids declaration of ‘tuple’ with no type

When trying to compile a simple class (g++ myclass.cpp), I get the following error: ISO C++ forbids declaration of ‘tuple’ with no type I searched for this problem, and in most cases people seemed to forget std:: or including in the…
Jawap
  • 2,463
  • 3
  • 28
  • 46
9
votes
1 answer

c2664 in Visual Studio 2012 when using make_pair

I dig up an old project and wanted to compile it, but received several errors, a few of those being a c2664: error C2664: 'std::make_pair' : cannot convert parameter 1 from 'CUser *' to 'CUser *&&' error C2664: 'std::make_pair' : cannot convert…
Juarrow
  • 2,232
  • 5
  • 42
  • 61
8
votes
6 answers

How to declare a static lookup table using C++11

I have C++11 program which needs to pick a value from a range of values. Each of the ranges have an assigned value to pick a value from. Following is my range of values with assigned values for each. 1-10 = 5 11-14 = 13 15-20 = 17 21-28 = 24 29-36 =…
AdeleGoldberg
  • 1,289
  • 3
  • 12
  • 28