std-pair

Why is std::pair faster than std::tuple

时光总嘲笑我的痴心妄想 提交于 2019-11-29 20:46:09
Here is the code for testing. Tuple test: using namespace std; int main(){ vector<tuple<int,int>> v; for (int var = 0; var < 100000000; ++var) { v.push_back(make_tuple(var, var)); } } Pair test: #include <vector> using namespace std; int main(){ vector<pair<int,int>> v; for (int var = 0; var < 100000000; ++var) { v.push_back(make_pair(var, var)); } } I did the time measurement via Linux time command. The results are: | | -O0 | -O2 | |:------|:-------:|:--------:| | Pair | 8.9 s | 1.60 s | | Tuple | 19.8 s | 1.96 s | I am wondering, why is such a big difference between those two data structures

C++ Erasing from list of pairs

这一生的挚爱 提交于 2019-11-29 18:13:34
Very simple: I have the following code and the method erase is not working. I do not see any problem there because if I go to http://www.cplusplus.com/reference/list/list/erase/ , syntax is: iterator erase (iterator position); list<pair<string,int>> l0 { { "name1", 20 }, { "name2", 30 }, { "name3", 40 } }; for( auto &it : l0 ) l0 . erase( it ); May there be a problem that there is a list of pair<string,int> and not a list of a basic data types? EDIT: The problem is that the code is not compilable. Christophe The range-for iterates through a container by giving you access to the elements in the

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

拈花ヽ惹草 提交于 2019-11-29 13:40:11
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. boost::bind is what you look for. boost::bind(&std::pair::second, _1); // returns the value of a pair Example: typedef std::map

Sorting a vector of pairs [duplicate]

寵の児 提交于 2019-11-29 11:38:00
问题 This question already has answers here : Sorting a std::vector<std::pair<std::string,bool>> by the string? (4 answers) Closed 6 years ago . I have a question about sorting a vector of pairs: std::vector<std::pair<double,Processor*>> baryProc; this vector is already filled up with the pairs. Now I wanted to sort the pairs inside the vector based on the double value inside the pair EXAMPLE: suppose I have 3 pairs inside the vector. pair1 is at front and pair 3 is at end. pair2 is in the middle:

Using pair<int, int> as key for map

≡放荡痞女 提交于 2019-11-29 05:37:43
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 key. Am I doing something wrong? you need a pair as a key cout << mymap[make_pair(1,2)] << endl; What

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

狂风中的少年 提交于 2019-11-28 19:12:00
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? std::pair<int, int>::pair() constructor initializes the fields with default values (zero in case of int ) and your struct Cell doesn't (since you only have an

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

旧时模样 提交于 2019-11-28 19:03:21
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...) Nicol Bolas 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 the tuple case, while the pair case is just a member field. But that's about it. This is a very

Why is std::pair faster than std::tuple

可紊 提交于 2019-11-28 16:26:07
问题 Here is the code for testing. Tuple test: using namespace std; int main(){ vector<tuple<int,int>> v; for (int var = 0; var < 100000000; ++var) { v.push_back(make_tuple(var, var)); } } Pair test: #include <vector> using namespace std; int main(){ vector<pair<int,int>> v; for (int var = 0; var < 100000000; ++var) { v.push_back(make_pair(var, var)); } } I did the time measurement via Linux time command. The results are: | | -O0 | -O2 | |:------|:-------:|:--------:| | Pair | 8.9 s | 1.60 s | |

How can I print out C++ map values?

半腔热情 提交于 2019-11-28 16:20:42
I have a map like this: map<string, pair<string,string> > myMap; And I've inserted some data into my map using: myMap.insert(make_pair(first_name, make_pair(middle_name, last_name))); How can I now print out all the data in my map? 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 <<

What is std::pair?

旧巷老猫 提交于 2019-11-28 16:12:08
What is std::pair for, why would I use it, and what benefits does boost::compressed_pair bring? jwfearn std::pair is a data type for grouping two values together as a single object. std::map uses it for key, value pairs. While you're learning pair , you might check out tuple . It's like pair but for grouping an arbitrary number of values. tuple is part of TR1 and many compilers already include it with their Standard Library implementations. Also, checkout Chapter 1, "Tuples," of the book The C++ Standard Library Extensions: A Tutorial and Reference by Pete Becker, ISBN-13: 9780321412997, for a