785 questions
7
votes
1
answer
225
views
Type-based std::get for std::pair can be compiled with MSVC but cannot be compiled in gcc
Consider the following example
#include <utility>
int main() {
int i = 0;
int& r = i;
std::pair<int&, int> p{r, i};
int& ret = std::get<int&>(...
1
vote
0
answers
130
views
How to iterate over a container of pairs of pairs in C++?
If I have a vector<std::pair<std::pair<int, int>, std::pair<int, int>>>, what would you suggest as the most concise method to iterate over them, having access to all four ...
0
votes
1
answer
89
views
How to avoid tempory variable create and dead when emplace_back a pair object?
I have a vector container, typename is std::pair, but i found there are many construct/deconstruct operation when add element.
Here is a demo code:
#include <iostream>
#include <vector>
...
5
votes
1
answer
292
views
Can I make one variable from a structured binding const and the other non-const?
I'd like to be able to decompose a structured binding, of lets say, std::pair<T, U> into a non-const variable and a const variable:
E.g.
std::pair<int, int> anExamplePair = std::make_pair(...
-1
votes
1
answer
56
views
Undefined Symbol To Static Const Member Variable in std::pair Construction [duplicate]
When populating an unordered map of int pairs, creating a std::pair object with a static const class member seems to cause an undefined symbol error, but only in the std::pair constructor call. If I ...
-1
votes
2
answers
67
views
Creating HashMap of point(x,y) and ArrayList<Integer> in java
I have to make a HashMap of point and Array-list. point is nothing but the pair of x and y in x-y plain.
then i want to store all student's roll no. which are there at that specific point.
as we don't ...
3
votes
2
answers
96
views
Implicit conversion from non-const to const pair template parameter and invocation of copy/move ctors on unoredered_map::insert in C++
Code
#include <iostream>
#include <unordered_map>
#include <utility>
using namespace std;
struct Foo {
Foo(const int value) : val(value) {
cout << "Foo(int), ...
3
votes
1
answer
63
views
How does std::pair constructor overload taking tuples tell if tuple contents are r-val references?
Apologies for the awkwardly worded question. My uncertainty is based on a situation that can roughly be reduced to the following scenaio.
I have a Foo class, and I'd like a std::pair<int, Foo> ...
2
votes
2
answers
154
views
unordered_map of class following pImpl idiom using unique_ptr
Here is a simplified code: https://godbolt.org/z/EnE76xMrP
The pImpl will contain a mutex member which make the pImpl neither copyable nor movable. But the Foo class has unique_ptr of pImpl as member ...
0
votes
1
answer
66
views
Vector<pair>: emplace without constructing pair-inner objects
Suppose we have two structs:
struct FIRST{
int a=0;
int b=0;
FIRST(int a, int b): a(a), b(b){}
};
struct SECOND{
int c=0;
int d=0;
SECOND(int c, int d): c(c), d(d){}
};
And we ...
0
votes
2
answers
113
views
Remove pairs in a vector sharing the same first element, but keep the pair containing the largest second element
Let's say the elements of the vector of pairs are
1, 0
2, 0
1, 2
2, 4
4, 0
5, 0
The output should be
1, 2
2, 4
4, 0
5, 0
In the above example, the pairs {1, 0}, and {1, 2} are replaced by
the pair {...
1
vote
1
answer
704
views
List-initialization of vector of pairs
Note: Please don't close the question just because the root cause was due to comma operator. The value of the question was to let the community understand the failure of:
std::vector<std::pair<...
0
votes
2
answers
129
views
Forcing evaluation order of `std::pair` constructors; a progress bar `struct` for a for-loop
I'm writing a program containing many long for-loops, and I want to add a progress bar indicator to each. To do this, I wrote struct ProgressBar to accomplish this. The interface is as follows:
struct ...
4
votes
2
answers
118
views
Why binding rvalue to a const type makes it an lvalue?
This question may not have the best title but here is the code that will explain what I am trying to ask.
This code runs and prints "lvalue" but If I remove const from MyPair's first type, ...
4
votes
1
answer
284
views
What is the order of destruction of the two entries of a std::pair?
Only 5 tags are allowed, but please, take it as c++20 and c++23 are in the list too, because I'd like to know about those standards as well, in case something has changed since c++17.
Is the order of ...