std-pair

Do std::tuple and std::pair support aggregate initialization?

百般思念 提交于 2019-12-05 04:07:21
Aggregate initialization requires among other things no user-provided constructors . But std::tuple and std::pair pair have a large set of overloaded constructors . From the point of the core language, are these constructors user-provided or even user-declared ? With C++17 it will be possible to write (update/clarification: where nocopy is a class that can not be copied or moved, such as std::mutex ) auto get_ensured_rvo_str(){ return std::pair(std::string(),nocopy()); } edit: no, it's not possible as explained in the linked to answers and the answer below. which requires aggregate

Equivalent of C++ STL container “pair<T1, T2>” in Objective-C?

◇◆丶佛笑我妖孽 提交于 2019-12-04 16:33:27
问题 I'm new to Objective-C, so please don't judge me too much. I was wondering: Is there an equivalent of the C++ STL pair container I can use in Objective-C? I want to build an array that contains an NSInteger associated to an NSBool. I know I could use an array with each entry being a NSDictionary with a single key-value but I find it to be a little overkill. Any ideas? Thanks. 回答1: You can use the STL in Objective-C++. All you need to do is change the extension of your .m file to .mm and I

use of emplace(args&& …) in associative containers

天涯浪子 提交于 2019-12-04 12:16:10
I am trying to forward some arguments to do inplace construction of objects . I don't quite get the rationale behind the usage of emplace in associative containers or may be I am just using/thinking in a wrong way. It would be great if someone can share code snippets for usage. Associative container like map always store an object of kind pair() , and the emplace function says that it will call the constructor of the object stored (which for is always pair in case of maps) by forwarding the arguments. So are we just restricted to provide two arguments (key , value) even if the the function has

Use of for_each on map elements

↘锁芯ラ 提交于 2019-12-04 07:43:08
问题 I have a map where I'd like to perform a call on every data type object member function. I yet know how to do this on any sequence but, is it possible to do it on an associative container? The closest answer I could find was this: Boost.Bind to access std::map elements in std::for_each. But I cannot use boost in my project so, is there an STL alternative that I'm missing to boost::bind? If not possible, I thought on creating a temporary sequence for pointers to the data objects and then, call

How to std::hash an unordered std::pair

心已入冬 提交于 2019-12-04 05:06:53
I want to be able to use a std::pair as a key in an unordered_container. I know that I could do this the following way: template<typename T> void hash_combine(std::size_t &seed, T const &key) { std::hash<T> hasher; seed ^= hasher(key) + 0x9e3779b9 + (seed << 6) + (seed >> 2); } namespace std { template<typename T1, typename T2> struct hash<std::pair<T1, T2>> { std::size_t operator()(std::pair<T1, T2> const &p) const { std::size_t seed(0); ::hash_combine(seed, p.first); ::hash_combine(seed, p.second); return seed; } }; } However, I want the hashing to ignore the order of the elements in the std

C++ Erasing from list of pairs

老子叫甜甜 提交于 2019-12-03 18:17:55
问题 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

Equivalent of C++ STL container “pair<T1, T2>” in Objective-C?

a 夏天 提交于 2019-12-03 11:26:11
I'm new to Objective-C, so please don't judge me too much. I was wondering: Is there an equivalent of the C++ STL pair container I can use in Objective-C? I want to build an array that contains an NSInteger associated to an NSBool. I know I could use an array with each entry being a NSDictionary with a single key-value but I find it to be a little overkill. Any ideas? Thanks. You can use the STL in Objective-C++. All you need to do is change the extension of your .m file to .mm and I would also advise you use #import instead of #include . That way you can use your pair STL container. You can

STL map insertion efficiency: [] vs. insert

♀尐吖头ヾ 提交于 2019-12-03 11:20:16
There are two ways of map insertion: m[key] = val; Or m.insert(make_pair(key, val)); My question is, which operation is faster? People usually say the first one is slower, because the STL Standard at first 'insert' a default element if 'key' is not existing in map and then assign 'val' to the default element. But I don't see the second way is better because of 'make_pair'. make_pair actually is a convenient way to make 'pair' compared to pair<T1, T2>(key, val) . Anyway, both of them do two assignments, one is assigning 'key' to 'pair.first' and two is assigning 'val' to 'pair.second'. After

Using pair as key in a map (C++ / STL)

南楼画角 提交于 2019-12-03 08:14:15
问题 I want to use a pair from STL as a key of a map. #include <iostream> #include <map> using namespace std; int main() { typedef pair<char*, int> Key; typedef map< Key , char*> Mapa; Key p1 ("Apple", 45); Key p2 ("Berry", 20); Mapa mapa; mapa.insert(p1, "Manzana"); mapa.insert(p2, "Arandano"); return 0; } But the compiler throw a bunch of unreadable information and I'm very new to C and C++. How can I use a pair as a key in a map? And in general How can I use any kind of structure (objects,

struct with 2 cells vs std::pair? [duplicate]

孤街醉人 提交于 2019-12-03 04:35:11
Possible Duplicate: What is the difference between using a struct with two fields and a pair? Dear all, I have a little question about pairs and struct. Is there any advantage to use a std::pair instead of a struct with two cells ? I have used pairs for a while but the main problem is readability : If you want to represent for example a duple (int "label", double "value") you can use either a : typedef std::pair<int,double> myElem; or a typedef struct { int label; double value; } myElem; The code becomes more readable if your statements have a "semantic" sense (you will always know what x