std-pair

Sort a vector of pairs by first element then by second element of the pair in C++? [duplicate]

柔情痞子 提交于 2019-11-28 13:14:58
This question already has an answer here: Sorting a std::vector<std::pair<std::string,bool>> by the string? 4 answers If I have a vector<pair<int,int> > datatype, what is the accepted way to sort it by the first element of the pair and then by second if the firsts are equal? For instance maybe (1,10), (3,3), (7,13), (7,16), (8,1), (8,2), (15,2) etc. pair s by default compare by first element, then second. So, if you don't care about preserving the order when the first elements compare equal, then you can just use std::sort : std::sort(v.begin(), v.end()); std::pair s comparison operators

find_if and std::pair, but just one element

南楼画角 提交于 2019-11-28 09:26:31
Suppose i have the following code: std::vector< std::pair <int, char> > myVec; or std::list< std::pair <int, char> > myList; /* then ***************/ std::list< std::pair <int, char> >::iterator listIt; or std::vector< std::pair <int, char> >::iterator vectorIt; /* No difference between vector and list */ Now i need to search just for one int element in them, So: vectorIt = std::find_if(myVec.begin(),myVect.end(),make_pair(.....)); ^^^^^^^^^^^^^^^^^ how can i do it? Write a unary predicate that takes an std::pair , and returns true if the first element is equal to a given value. For example:

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

强颜欢笑 提交于 2019-11-28 06:12:56
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 <unordered_set> ... class A{ ... private: std::unordered_set< std::pair<int, int> > u_edge_; }; Compiler error: error: no matching function for call to 'std::unordered_set >::unordered_set()' Your code compiles on VS2010 SP1 (VC10), but it fails to compile with GCC g++ 4.7.2. However, you may want to consider boost::hash from Boost.Functional to hash a std::pair (with this addition,

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

不想你离开。 提交于 2019-11-28 02:56:04
What is the purpose of std::make_pair ? Why not just do std::pair<int, char>(0, 'a') ? Is there any difference between the two methods? 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; pair <int,int> two; one = make_pair (10,20); two = make_pair (10.5,'A'); // ok: implicit

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

匆匆过客 提交于 2019-11-27 21:05:14
What is the difference regarding memory allocation and efficiency between using a struct with two fields and a pair? AshleysBrain 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 works?) so it's more reliable just to use std::pair . Matthieu M. std::pair comes with a number of constructors and

converting a variable name to a string in C++

对着背影说爱祢 提交于 2019-11-27 19:24:01
I'd like to output some data to a file. For example assume I have two vectors of doubles: vector<double> data1(10); vector<double> data2(10); is there an easy way to output this to a file so that the first row contains the headings 'data1' and 'data2' followed by the actual contents. The function which outputs the data will be passed various different arrays so hardcoding the name of the heading is not possible - ideally I'd like to convert the variable name to some string and then output that string followed by the contents of the vector array. However, I'm not sure how to convert the

Using pair<int, int> as key for map

╄→гoц情女王★ 提交于 2019-11-27 17:30:42
问题 Based on a previous question, I am trying to create a map using a pair of integers as a key i.e. map<pair<int, int>, int> and I've found information on how to insert: #include <iostream> #include <map> using namespace std; int main () { map<pair<int, int>, int> mymap; mymap.insert(make_pair(make_pair(1,2), 3)); //edited } but I can't seem to access the element! I've tried cout << mymap[(1,2)] << endl; but it shows an error, and I can't find information on how to access the element using the

pair<int,int> pair as key of unordered_map issue

限于喜欢 提交于 2019-11-27 13:37:56
My code: typedef pair<int,int> Pair tr1::unordered_map<Pair,bool> h; h.insert(make_pair(Pair(0,0),true)); Erorr undefined reference to `std::tr1::hash<std::pair<int, int> >::operator()(std::pair<int, int>) const' Something I need to fix? thanks This happens because there is no specialization for std::tr1::hash<Key> with Key = std::pair<int, int> . You must to specialize std::tr1::hash<Key> with Key = std::pair<int, int> before declaring tr1::unordered_map<Pair,bool> h; . This happens because std don't know how to hash a pair<int, int> . Following there is a example of how to specialize std:

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

淺唱寂寞╮ 提交于 2019-11-27 11:37:23
问题 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...) 回答1: There are some differences: std::tuple can never be by standard-layout (at least, it's not required to be by the standard). 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

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

岁酱吖の 提交于 2019-11-27 11:18:19
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 removed. Does anyone know why it was removed? I find the removal very unfortunate, because it seems there is no other way to treat a pair of iterators as a range. Indeed: The lookup rules for begin/end in a range-based for loop say that begin/end are looked for in 1) as member functions of the range object 2) as free functions in "associated