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
44
votes
6 answers

Using pair as key in a map (C++ / STL)

I want to use a pair from STL as a key of a map. #include #include using namespace std; int main() { typedef pair Key; typedef map< Key , char*> Mapa; Key p1 ("Apple", 45); Key p2 ("Berry", 20); Mapa…
ccarpenterg
  • 779
  • 2
  • 10
  • 11
40
votes
3 answers

C++ vector of pairs initialization

I have vector< pair> myVec (N); I want to have all pairs initialized to -1,-1.
MyNameIsKhan
  • 2,594
  • 8
  • 36
  • 56
36
votes
2 answers

How to initialize C++17 vector of pairs with optional element

In C++17, how do you declare and initialize a vector of pairs(or tuples) with an optional element? std::vector > > vec1 = { {1, true}, {2, false}, …
Eugene
  • 10,957
  • 20
  • 69
  • 97
36
votes
6 answers

What is the difference between using a struct with two fields and a pair?

What is the difference regarding memory allocation and efficiency between using a struct with two fields and a pair?
Tamara Wijsman
  • 12,198
  • 8
  • 53
  • 82
35
votes
5 answers

struct with 2 cells vs std::pair?

Possible Duplicate: What is the difference between using a struct with two fields and a pair? Dear all, I have a little question about pairs and struct. Is there any advantage to use a std::pair instead of a struct with two cells ? I have used…
ThR37
  • 3,965
  • 6
  • 35
  • 42
35
votes
2 answers

How should I brace-initialize an std::array of std::pairs?

std::array, 2> ids = { { 0, 1 }, { 1, 2 } }; VS2013 error: error C2440: 'initializing' : cannot convert from 'int' to 'std::pair' No constructor could take the source type, or constructor overload resolution was…
Neil Kirk
  • 21,327
  • 9
  • 53
  • 91
33
votes
6 answers

Is there a way to easily handle functions returning std::pairs?

C++11 has function std::minmax_element which returns a pair of values. This, however, is quite confusing to handle and read, and produces an extra, later useless variable to pollute the scope. auto lhsMinmax = std::minmax_element(lhs.begin(),…
Adam Hunyadi
  • 1,890
  • 16
  • 32
33
votes
4 answers

std::pair vs struct with two int's

In an ACM example, I had to build a big table for dynamic programming. I had to store two integers in each cell, so I decided to go for a std::pair. However, allocating a huge array of them took 1.5 seconds: std::pair
Etan
  • 17,014
  • 17
  • 89
  • 148
32
votes
1 answer

Does there exist something like std::tie for std::pair?

Eg with tuples: #include // std::tuple, std::make_tuple, std::tie int num; char letter; std::tuple num_letter; num_letter = std::make_tuple(10, 'a'); std::tie(num, letter) = num_letter; // unpack num_letter into num and…
wrhall
  • 1,288
  • 1
  • 12
  • 26
32
votes
2 answers

Pair inside priority queue

I am trying to store pairs in priority queue and I am using a compare function that compares second value of each pair. #include #include #include using namespace std; class CompareDist { public: bool…
g4ur4v
  • 3,210
  • 5
  • 32
  • 57
31
votes
1 answer

why do i need to use piecewise_construct in map::emplace for single arg constructors of noncopyable objects?

The following code will not compile on gcc 4.8.2. The problem is that this code will attempt to copy construct an std::pair which can't happen due to struct A missing copy and move constructors. Is gcc failing here or am I missing…
oli_obk
  • 28,729
  • 6
  • 82
  • 98
28
votes
4 answers

Does C++ provide a "triple" template, comparable to pair?

Does C++ have anything like std::pair but with 3 elements? For example: #include triple array[10]; array[1].first = 1; array[1].second = 2; array[1].third = 3;
user3432317
  • 309
  • 1
  • 3
  • 3
28
votes
2 answers

Using std::tie as a range for loop target

I want to do something like the following: //std::vector> someInitializingFunction(); { TypeA a; TypeB b; for (std::tie(a, b) : someInitializingFunction()) { // do stuff; } } However, this is not valid code…
OmnipotentEntity
  • 16,531
  • 6
  • 62
  • 96
28
votes
3 answers

Using pair as key for map

Based on a previous question, I am trying to create a map using a pair of integers as a key i.e. map, int> and I've found information on how to insert: #include #include using namespace std; int main…
sccs
  • 1,123
  • 3
  • 14
  • 27
26
votes
2 answers

Initializer list inside std::pair

This code: #include #include std::pair, int> groups{ { "A", "B" }, 0 }; int main() { for (const auto& i : groups.first) { std::cout << i << '\n'; } return…
rin
  • 295
  • 2
  • 6
1
2
3
51 52