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
713
votes
37 answers

What is the equivalent of the C++ Pair in Java?

Is there a good reason why there is no Pair in Java? What would be the equivalent of this C++ construct? I would rather avoid reimplementing my own. It seems that 1.6 is providing something similar (AbstractMap.SimpleEntry), but this looks…
David Segonds
  • 83,345
  • 10
  • 45
  • 66
338
votes
14 answers

What is C# analog of C++ std::pair?

I'm interested: What is C#'s analog of std::pair in C++? I found System.Web.UI.Pair class, but I'd prefer something template-based. Thank you!
Alexander Prokofyev
  • 33,874
  • 33
  • 95
  • 118
238
votes
7 answers

What is the purpose of std::make_pair vs the constructor of std::pair?

What is the purpose of std::make_pair? Why not just do std::pair(0, 'a')? Is there any difference between the two methods?
user542687
171
votes
9 answers

What is the preferred/idiomatic way to insert into a map?

I have identified four different ways of inserting elements into a std::map: std::map function; function[0] = 42; function.insert(std::map::value_type(0, 42)); function.insert(std::pair(0,…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
131
votes
6 answers

Difference between std::pair and std::tuple with only two members?

Is there a difference between an std::pair and an std::tuple with only two members? (Besides the obvious that std::pair requires two and only two members and tuple may have more or less...)
Casey
  • 10,297
  • 11
  • 59
  • 88
86
votes
5 answers

How can I print out C++ map values?

I have a map like this: map> myMap; And I've inserted some data into my map using: myMap.insert(make_pair(first_name, make_pair(middle_name, last_name))); How can I now print out all the data in my map?
Zhi Rui
  • 1,023
  • 1
  • 11
  • 10
78
votes
10 answers

How can I make an unordered set of pairs of integers in C++?

The following program does not compile an unordered set of pairs of integers, but it does for integers. Can unordered_set and its member functions be used on user-defined types, and how can I define it? #include ... class…
Pippi
  • 2,451
  • 8
  • 39
  • 59
63
votes
11 answers

Use of for_each on map elements

I have a map where I'd like to perform a call on every data type object member function. I yet know how to do this on any sequence but, is it possible to do it on an associative container? The closest answer I could find was this: Boost.Bind to…
Antonio Pérez
  • 6,702
  • 4
  • 36
  • 61
61
votes
10 answers

Adding to a vector of pair

I have a vector of pair like such: vector> revenue; I want to add a string and a double from a map like this: revenue[i].first = "string"; revenue[i].second = map[i].second; But since revenue isn't initialized, it comes up with…
Richard
  • 5,840
  • 36
  • 123
  • 208
56
votes
3 answers

Why was pair range access removed from C++11?

I just discovered that at one point, the C++11 draft had std::begin/std::end overloads for std::pair that allowed treating a pair of iterators as a range suitable for use in a range-based for loop (N3126, section 20.3.5.5), but this has since been…
HighCommander4
  • 50,428
  • 24
  • 122
  • 194
55
votes
8 answers

converting a variable name to a string in C++

I'd like to output some data to a file. For example assume I have two vectors of doubles: vector data1(10); vector data2(10); is there an easy way to output this to a file so that the first row contains the headings 'data1' and…
Wawel100
  • 1,221
  • 4
  • 21
  • 26
53
votes
7 answers

What is the difference between std::list and std::map in C++ STL?

What is the difference between std::list and std::map? Is there a find method for the list, too?
Boolean
  • 14,266
  • 30
  • 88
  • 129
47
votes
2 answers

Why is std::pair faster than std::tuple

Here is the code for testing. Tuple test: using namespace std; int main(){ vector> v; for (int var = 0; var < 100000000; ++var) { v.push_back(make_tuple(var, var)); } } Pair test: #include using…
user4233758
45
votes
10 answers

What is std::pair?

What is std::pair for, why would I use it, and what benefits does boost::compressed_pair bring?
Anthony
  • 1,306
  • 4
  • 13
  • 23
44
votes
1 answer

Why is std::pair from anonymous object copying that object instead of moving?

I tried to make std::pair with this style: #include struct A { A() noexcept { std::cout << "Created\n"; } A(const A&) noexcept { std::cout << "Copy\n"; } A(A&&) noexcept { std::cout <<…
1
2 3
51 52