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

There is no standard way of computing a hash on a pair. Add this definition to your file: struct pair_hash { inline std::size_t operator()(const std::pair<int,int> & v) const { return v.first*31+v.second; } }; Now you can use it like this: std::unordered_set< std::pair<int, int>, pair_hash> u_edge_; This works, because pair<T1,T2> defines equality. For custom classes that … Read more

How can I print out C++ map values?

for(map<string, pair<string,string> >::const_iterator it = myMap.begin(); it != myMap.end(); ++it) { std::cout << it->first << ” ” << it->second.first << ” ” << it->second.second << “\n”; } In C++11, you don’t need to spell out map<string, pair<string,string> >::const_iterator. You can use auto for(auto it = myMap.cbegin(); it != myMap.cend(); ++it) { std::cout << it->first << … Read more

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

There are some differences: std::tuple is not required by the standard to ever be standard-layout. Every std::pair<T, Y> is standard-layout if both T and Y are standard-layout. It’s a bit easier to get the contents of a pair than a tuple. You have to use a function call in the tuple case, while the pair … Read more

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

As of C++11, you have two major additional options. First, you can use insert() with list initialization syntax: function.insert({0, 42}); This is functionally equivalent to function.insert(std::map<int, int>::value_type(0, 42)); but much more concise and readable. As other answers have noted, this has several advantages over the other forms: The operator[] approach requires the mapped type to … Read more

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

The difference is that with std::pair you need to specify the types of both elements, whereas std::make_pair will create a pair with the type of the elements that are passed to it, without you needing to tell it. That’s what I could gather from various docs anyways. See this example from http://www.cplusplus.com/reference/std/utility/make_pair/ pair <int,int> one; … Read more

What is 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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)