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

As far as I can tell, the issue isn’t caused by map::emplace, but by pair‘s constructors: #include <map> struct A { A(int) {} A(A&&) = delete; A(A const&) = delete; }; int main() { std::pair<int, A> x(1, 4); // error } This code example doesn’t compile, neither with coliru’s g++4.8.1 nor with clang++3.5, which are … Read more

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

Tuples are available since .NET4.0 and support generics: Tuple<string, int> t = new Tuple<string, int>(“Hello”, 4); In previous versions you can use System.Collections.Generic.KeyValuePair<K, V> or a solution like the following: public class Pair<T, U> { public Pair() { } public Pair(T first, U second) { this.First = first; this.Second = second; } public T First … Read more

Pair inside priority queue

This is what priority_queue looks like: template< class T, class Container = std::vector<T>, class Compare = std::less<typename Container::value_type> > class priority_queue; In other words, CompareDist should be the third argument and the second argument should be the container (which has value_type), like the following: priority_queue<pair<int,int>,vector<pair<int,int>>,CompareDist> pq; Notice also, that priority_queue is what is called a … Read more

std::pair vs struct with two int’s

std::pair<int, int>::pair() constructor initializes the fields with default values (zero in case of int) and your struct Cell doesn’t (since you only have an auto-generated default constructor that does nothing). Initializing requires writing to each field which requires a whole lot of memory accesses that are relatively time consuming. With struct Cell nothing is done … Read more

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

std::pair provides pre-written constructors and comparison operators. This also allows them to be stored in containers like std::map without you needing to write, for example, the copy constructor or strict weak ordering via operator < (such as required by std::map). If you don’t write them you can’t make a mistake (remember how strict weak ordering … Read more