std-pair

Difference between make_pair and curly brackets { } for assigning a pair in C++?

狂风中的少年 提交于 2019-12-01 12:56:30
I didn't find anyone answering this, is there any difference between the following : v.push_back({x, y}); and : v.push_back(make_pair(x, y)); Assuming that v was declared this way : vector<pair<int,int> > v; I tried this in an online compiler, and as far as I can see the optimized assembly for make_pair is identical to {} syntax. https://godbolt.org/z/P7Ugkt I think you might have accepted that answer a little too quickly. The commonly accepted way to do this is like this: vec.emplace_back (x, y); And if you look at Godbolt, you can see that this inlines everything (which may or may not be

Difference between make_pair and curly brackets { } for assigning a pair in C++?

此生再无相见时 提交于 2019-12-01 11:27:22
问题 I didn't find anyone answering this, is there any difference between the following : v.push_back({x, y}); and : v.push_back(make_pair(x, y)); Assuming that v was declared this way : vector<pair<int,int> > v; 回答1: I tried this in an online compiler, and as far as I can see the optimized assembly for make_pair is identical to {} syntax. https://godbolt.org/z/P7Ugkt 回答2: I think you might have accepted that answer a little too quickly. The commonly accepted way to do this is like this: vec

How to create a std::set with custom comparator in C++?

孤者浪人 提交于 2019-12-01 10:34:57
问题 How do I create a set of pairs, the elements of which (the pairs) are sorted with a custom bool function? I write set <pair<int,int>,compare> myset; and get error : Type/value mismatch at argument 2, expected a type, got "compare" I have defined "compare" as bool compare(pair <int,int> g1, pair <int,int> g2) { return (g1.second-g1.first > g2.second-g2.first); } and of course #include <vector> #include <set> 回答1: Method 1: use functor Write a class that overloads the operator() so it can be

How to convert a sorted std::list of std::pair to a std::map

旧时模样 提交于 2019-12-01 01:54:49
问题 I have got a std::list< std::pair<std::string,double> > , which I know is sorted according to the std::string element . Since I would like to do a lot of std::find_if based on the std::string element, I believe a std::map<string,double,MyOwnBinaryPredicate> with lower_bound and upper_bound would be more adequate. The fact is that I want to insert elements in the std::map in an efficient way. So I want to use an additional iterator to make the insert faster. I believe the easiest way would be

Is it possible to cast a pair<Key, Value> to a pair<const Key, Value>?

百般思念 提交于 2019-11-30 21:40:05
So I have a smart iterator that emulates a map const_iterator, and it needs to build the return type internally. Obviously, I'd like to store a pair<Key, Value> in my iterator class (since I need to modify it), but at the same time I'd like the dereference functions to present a pair<const Key, Value> (actually it would be a const pair<const Key, Value>& and const pair<const Key, Value>* respectively). The only solution I've come up with so far is to dynamically allocate a new pair every time change the value that my iterator class points to changes. Needless to say, this is not a good

Can I use std::pair, but rename .first and .second member names?

∥☆過路亽.° 提交于 2019-11-30 17:54:48
A common design problem I run into, is that I bundle two variables together and then lose the ability to reference them in a meaningful way. std::pair<int,int> cords; cord.first = 0; //is .first the x or y coordinate? cord.second = 0; //is .second the x or y coordinate? I've considered writing basic structs instead, but then I lose a lot of the benefits that come along with std::pair : make_pair non-member overloaded operators swap get etc. Is there a way to rename or provide an alternative identifier for the first and second data members? I was hoping to leverage all of the the functions that

Initialize a vector of pairs in one line

≡放荡痞女 提交于 2019-11-30 09:38:38
问题 I want to initialize a std::vector (of std::pair), with k objects, with the pair of values shown below. Here is my attempt: // int k std::vector <std::pair<Point::FT, int> > v(k, (std::numeric_limits<FT>::max(), -1)); The error: usr/include/c++/4.6/bits/stl_vector.h: In member function ‘void std::vector<T, Allocator>::_M_initialize_dispatch(_Integer, _Integer, std::__true_type) [with _Integer = int, _Tp = std::pair<float, int>, _Alloc = std::allocator<std::pair<float, int> >]’: /usr/include/c

C++ std::transform vector of pairs->first to new vector

若如初见. 提交于 2019-11-30 08:13:19
Sorry for a little bit beginner question. There are vector and vector of pairs typedef std::vector <int> TItems; typedef std::vector < std::pair <int, int> > TPairs; Is there any way to transform all first items in pair to another vector in one step int main () { TItems items; TPairs pairs; pairs.push_back (std::make_pair(1,3)); pairs.push_back (std::make_pair(5,7)); std::transform( items.begin(), items.end(), items.begin(), comp ( &pairs ) ); return 0; } How to design a functor? class comp { private: TPairs *pairs; public: comp ( TPairs *pairs_ ) : pairs ( pairs_) { } unsigned int operator ()

Is it possible to cast a pair<Key, Value> to a pair<const Key, Value>?

情到浓时终转凉″ 提交于 2019-11-30 05:28:40
问题 So I have a smart iterator that emulates a map const_iterator, and it needs to build the return type internally. Obviously, I'd like to store a pair<Key, Value> in my iterator class (since I need to modify it), but at the same time I'd like the dereference functions to present a pair<const Key, Value> (actually it would be a const pair<const Key, Value>& and const pair<const Key, Value>* respectively). The only solution I've come up with so far is to dynamically allocate a new pair every time

Can I use std::pair, but rename .first and .second member names?

吃可爱长大的小学妹 提交于 2019-11-30 01:13:46
问题 A common design problem I run into, is that I bundle two variables together and then lose the ability to reference them in a meaningful way. std::pair<int,int> cords; cord.first = 0; //is .first the x or y coordinate? cord.second = 0; //is .second the x or y coordinate? I've considered writing basic structs instead, but then I lose a lot of the benefits that come along with std::pair : make_pair non-member overloaded operators swap get etc. Is there a way to rename or provide an alternative