std-pair

Comparator for vector<pair<int,int>> [duplicate]

我只是一个虾纸丫 提交于 2021-02-08 12:13:41
问题 This question already has answers here : How do I sort a vector of pairs based on the second element of the pair? (7 answers) Closed 6 years ago . vector<pair<int,int> > v; for(i=0;i<5;i++){ cin>>b>>c; v.push_back(make_pair(b,c)); } sort(v.begin(),v.end()); Is it possible to write a comparator for the sort function such that v[i].first is sorted in increasing order and for similar values of v[i].first , v[i].second is sorted in decreasing order? like:- i/p: 13 10 44 15 13 15 13 99 6 45 o/p: 6

Comparator for vector<pair<int,int>> [duplicate]

泄露秘密 提交于 2021-02-08 12:12:33
问题 This question already has answers here : How do I sort a vector of pairs based on the second element of the pair? (7 answers) Closed 6 years ago . vector<pair<int,int> > v; for(i=0;i<5;i++){ cin>>b>>c; v.push_back(make_pair(b,c)); } sort(v.begin(),v.end()); Is it possible to write a comparator for the sort function such that v[i].first is sorted in increasing order and for similar values of v[i].first , v[i].second is sorted in decreasing order? like:- i/p: 13 10 44 15 13 15 13 99 6 45 o/p: 6

Equivalent of std::pair in C

情到浓时终转凉″ 提交于 2021-01-04 02:39:50
问题 What is the C equivalent of std::pair from C++? I'm trying to find the equivalent on the web and can't find anything but explanations of what it does. 回答1: There isn't one. std::pair is a template, and C doesn't have anything similar to templates. For a given specialisation of the template, you can define a structure that's more or less equivalent; so that std::pair<int, double> is similar to struct pair_int_double { int first; double second; }; but you've chosen the wrong language if you

Equivalent of std::pair in C

北战南征 提交于 2021-01-04 02:39:11
问题 What is the C equivalent of std::pair from C++? I'm trying to find the equivalent on the web and can't find anything but explanations of what it does. 回答1: There isn't one. std::pair is a template, and C doesn't have anything similar to templates. For a given specialisation of the template, you can define a structure that's more or less equivalent; so that std::pair<int, double> is similar to struct pair_int_double { int first; double second; }; but you've chosen the wrong language if you

Equivalent of std::pair in C

生来就可爱ヽ(ⅴ<●) 提交于 2021-01-04 02:39:08
问题 What is the C equivalent of std::pair from C++? I'm trying to find the equivalent on the web and can't find anything but explanations of what it does. 回答1: There isn't one. std::pair is a template, and C doesn't have anything similar to templates. For a given specialisation of the template, you can define a structure that's more or less equivalent; so that std::pair<int, double> is similar to struct pair_int_double { int first; double second; }; but you've chosen the wrong language if you

Check if pair is empty or uninitialized

蹲街弑〆低调 提交于 2020-12-12 05:28:40
问题 int main() { pair<int, int> res; // check here pair is empty ? // res = make_pair(1 , 2); return 0; } In the above snippet what is the ideal way to check if a pair has been initilaized or not? EDIT: As pointed out in some answers below, using the word "uninitialized" is wrong here, to be more specific how do I check if the value has been explicitly set (other than the default constructor) 回答1: There's no such thing as unitialized std::pair . Default constructor for std::pair will value

Check if pair is empty or uninitialized

会有一股神秘感。 提交于 2020-12-12 05:28:35
问题 int main() { pair<int, int> res; // check here pair is empty ? // res = make_pair(1 , 2); return 0; } In the above snippet what is the ideal way to check if a pair has been initilaized or not? EDIT: As pointed out in some answers below, using the word "uninitialized" is wrong here, to be more specific how do I check if the value has been explicitly set (other than the default constructor) 回答1: There's no such thing as unitialized std::pair . Default constructor for std::pair will value

C++ vector of pairs initialization

心已入冬 提交于 2020-05-25 03:23:53
问题 I have vector< pair<int, int>> myVec (N); I want to have all pairs initialized to -1,-1. 回答1: Here you go: #include <utility> vector<pair<int, int>> myVec (N, std::make_pair(-1, -1)); The second argument to that constructor is the initial value that the N pairs will take. 回答2: Just to add some additional info (not quite what the Asker wanted, but asked for in the comments of the accepted answer): Individual initialization can be done with (C++11): std::vector<std::pair<int, int> > vec1 = { {1

stable_sort on a vector of pairs by first element in pair in increasing order without comparator function in C++

假如想象 提交于 2020-05-09 05:57:46
问题 #include <bits/stdc++.h> using namespace std; int main() { vector<pair<int,int>>v; v.push_back(make_pair(1,3)); v.push_back(make_pair(1,1)); v.push_back(make_pair(2,19)); v.push_back(make_pair(2,4)); int n = 4; stable_sort(v.begin(),v.end()); for (int i = 0; i < n; i++) cout << "[" << v[i].first << ", " << v[i].second << "] "; return 0; } Output : [1, 1] [1, 3] [2, 4] [2, 19] Expected Output : [1, 3] [1, 1] [2, 19] [2, 4] Why does the vector of pairs not maintain the relative ordering even