std-pair

Copy std::map into std::vector of pairs

五迷三道 提交于 2019-12-19 12:28:09
问题 I'm trying to copy a map into a vector of pair, so I can then sort the vector by the second data member of the pairs. I have resolved this doing like this: void mappedWordsListSorter(){ for (auto itr = mappedWordsList.begin(); itr != mappedWordsList.end(); ++itr){ vectorWordsList.push_back(*itr); } sort(vectorWordsList.begin(), vectorWordsList.end(), [=](pair<string, int>& a, pair<string, int>& b){return a.second > b.second;}); } I need to find a way to do this without using a raw loop, using

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

喜你入骨 提交于 2019-12-18 12:52:06
问题 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 {

Is there a standard C++ function object for taking apart a std::pair?

谁都会走 提交于 2019-12-18 07:47:06
问题 Does anyone know if there's a de-facto standard (i.e., TR1 or Boost) C++ function object for accessing the elements of a std::pair? Twice in the past 24 hours I've wished I had something like the keys function for Perl hashes. For example, it would be nice to run std::transform on a std::map object and dump all the keys (or values) to another container. I could certainly write such a function object but I'd prefer to reuse something that's had a lot of eyeballs on it. 回答1: boost::bind is what

Is there a standard C++ function object for taking apart a std::pair?

坚强是说给别人听的谎言 提交于 2019-12-18 07:47:03
问题 Does anyone know if there's a de-facto standard (i.e., TR1 or Boost) C++ function object for accessing the elements of a std::pair? Twice in the past 24 hours I've wished I had something like the keys function for Perl hashes. For example, it would be nice to run std::transform on a std::map object and dump all the keys (or values) to another container. I could certainly write such a function object but I'd prefer to reuse something that's had a lot of eyeballs on it. 回答1: boost::bind is what

std::pair<int, int> vs struct with two int's

狂风中的少年 提交于 2019-12-17 22:38:18
问题 In an ACM example, I had to build a big table for dynamic programming. I had to store two integers in each cell, so I decided to go for a std::pair<int, int> . However, allocating a huge array of them took 1.5 seconds: std::pair<int, int> table[1001][1001]; Afterwards, I have changed this code to struct Cell { int first; int second; } Cell table[1001][1001]; and the allocation took 0 seconds. What explains this huge difference in time? 回答1: std::pair<int, int>::pair() constructor initializes

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

丶灬走出姿态 提交于 2019-12-17 21:02:55
问题 This question already has answers here : Sorting a std::vector<std::pair<std::string,bool>> by the string? (4 answers) Closed 6 years ago . 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. 回答1: pair s by default compare by first element, then second. So, if you don't care about preserving the order when the

Meta-Programming with a Method Parameter

有些话、适合烂在心里 提交于 2019-12-13 01:23:56
问题 I'm writing a pair wrapper. For the purposes of this question it can be simplified down to: using namespace std; template <class T1, class T2> class myPair { pair<T1, T2> member; public: myPair() = default; myPair(T1 x, T2 y) : member(make_pair(x, y)) {} }; I'd like to be able to treat a myPair as an index-able container of size 2. To do this I'd obviously need to write an index operator for myPair . I'd like to do something like this, but my return type will depend upon a method parameter,

Pair of vectors instead of a vector<pair>? [closed]

青春壹個敷衍的年華 提交于 2019-12-12 16:26:42
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . I'm looking for a lib that would implement something similar to std::map and multimap, but without tree but vectors instead, like a pair<vector<key>,vector<value>> . The lib would have to keep the vectors synchronized and sorted. I'd like to compare the performance of maps, vector<pair<key,value>> and such a lib

Multimap containing pairs?

不想你离开。 提交于 2019-12-12 16:14:36
问题 Is it possible for a multimap to contain within it pairs? IE, rather then being defined as multimap<char,int> for instance, it would be defined as multimap<pair, pair >? How would this multimap then be sorted? Also, how would one access the individual contents of each pair? 回答1: Is it possible for a multimap to contain within it pairs? Yes its possible. How would this multimap then be sorted? By the key/first pair (ie, first by the first element of the first pair, then by the second element

Copy Constructor for pointers to objects

元气小坏坏 提交于 2019-12-12 05:52:42
问题 I am having problem in writing copy constructor for pointers to objects. This is my exact problem I have a class G1 that has an object s1 as its private data member. This is an object of a struct. The struct is composed of a vector<pair<int,pointer to another object of a different class>>. Now when I create a pointer for G1 everything is fine. When I try to copy this pointer to another new pointer of the same class it is making a shallow copy. So when I try to delete the first pointer the