std-pair

initialize vector of pairs

耗尽温柔 提交于 2019-12-11 09:06:22
问题 I wanted to initialize a vector of pairs with something like this std::vector< std::pair<bool, bool> > myvector(initSequence.size(), X ); what shall I substitute in place of X, if I want to initialize every pair with (false, false)? Thank you 回答1: std::pair<bool,bool>(false, false) or std::make_pair(false, false) 回答2: Nothing. std::pair<bool, bool> will be default constructed as make_pair(false, false) . 来源: https://stackoverflow.com/questions/4405630/initialize-vector-of-pairs

C++ can't initialize a pointer in a pair to NULL

痞子三分冷 提交于 2019-12-10 13:09:59
问题 I'm compiling with g++ 4.4.7 (and can't go any higher currently), and using the -std=gnu++0x compiler switch, which should allow the syntax of the third line. typedef std::vector<CI_RecordInfo_Pair> CI_RecordInfo_Vector; typedef std::vector<std::pair<std::string, CI_RecordInfo_Vector*> > MgrBlks; MgrBlks mgr_n_blks { {"T2M_NAME", NULL} }; // <--- line 59 However, the compiler complains as follows: /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_pair.h: In

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

最后都变了- 提交于 2019-12-10 03:37:04
问题 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

Why can't I return a unique_ptr from a pair?

淺唱寂寞╮ 提交于 2019-12-09 17:23:45
问题 Why can't I return a unique_ptr from a pair? #include <iostream> #include <memory> #include <utility> using namespace std; unique_ptr<int> get_value() { pair<unique_ptr<int>, int> p(unique_ptr<int>(new int(3)), 4); return p.first; } int main(void) { cout << *get_value() << endl; return 0; } When I try to compile this with g++ 4.6, I get: ../main.cpp: In function ‘std::unique_ptr<int> get_value()’: ../main.cpp:9:11: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const

C++ set search for pair element?

本秂侑毒 提交于 2019-12-07 03:11:55
问题 So I have a set of pairs<string ,string> And I want to use find() to search for a single string which would be in the "first" of the pair, then if I find that string in first I want to return second from that function. My current attempt is.. myList::iterator i; i = theList.find(make_pair(realName, "*")); return i->second; 回答1: Is C++11 acceptable? auto it = find_if(theList.begin(), theList.end(), [&](const pair<string, string>& val) -> bool { return val.first == realName; }); return it-

Will RVO happen when returning std::pair?

[亡魂溺海] 提交于 2019-12-07 00:09:20
问题 A function needs to return two values to the caller. What is the best way to implement? Option 1: pair<U,V> myfunc() { ... return make_pair(getU(),getV()); } pair<U,V> mypair = myfunc(); Option 1.1: // Same defn U u; V v; tie(u,v) = myfunc(); Option 2: void myfunc(U& u , V& v) { u = getU(); v= getV(); } U u; V v; myfunc(u,v); I know with Option2, there are no copies/moves but it looks ugly. Will there be any copies/moves occur in Option1, 1.1? Lets assume U and V are huge objects supporting

How do I create a set with std::pair thats sorted based on the ::second pair member using bind

不羁岁月 提交于 2019-12-05 13:54:50
I know I could use the following: template <typename Pair> struct ComparePairThroughSecond : public std::unary_function<Pair, bool> { bool operator ()(const Pair& p1, const Pair& p2) const { return p1.second < p2.second; } }; std::set<std::pair<int, long>, ComparePairThroughSecond> somevar; but wondered if it could be done with boost::bind How about the following one. I'm using boost::function to 'erase' the actual type of the comparator. The comparator is created using boost:bind itself. typedef std::pair<int, int> IntPair; typedef boost::function<bool (const IntPair &, const IntPair &)>

Any STL data structure like pair that gives three items(types) instead of two?

拜拜、爱过 提交于 2019-12-05 12:58:39
问题 Question 1: I'm using C++ 11, and I'm learning. I realize I can do this with two pairs: pair<pair<<#class _T1#>, <#class _T2#>>, <#class _T3#>> Is that the best way? Question 2: If I don't need different types, so same type for two items, is it a waste to use pair, what should I use then? For three items? (again same type) 回答1: Use a std::tuple: std::tuple<_T1, _T2, _T3> Note that std::tuple s support an arbitrary amount of types stored in them. Also, to access the elements, you can't do the

C++ set search for pair element?

假如想象 提交于 2019-12-05 07:06:27
So I have a set of pairs<string ,string> And I want to use find() to search for a single string which would be in the "first" of the pair, then if I find that string in first I want to return second from that function. My current attempt is.. myList::iterator i; i = theList.find(make_pair(realName, "*")); return i->second; Is C++11 acceptable? auto it = find_if(theList.begin(), theList.end(), [&](const pair<string, string>& val) -> bool { return val.first == realName; }); return it->second; Or in C++03, first define a functor: struct MatchFirst { MatchFirst(const string& realName) : realName

Will RVO happen when returning std::pair?

扶醉桌前 提交于 2019-12-05 04:52:01
A function needs to return two values to the caller. What is the best way to implement? Option 1: pair<U,V> myfunc() { ... return make_pair(getU(),getV()); } pair<U,V> mypair = myfunc(); Option 1.1: // Same defn U u; V v; tie(u,v) = myfunc(); Option 2: void myfunc(U& u , V& v) { u = getU(); v= getV(); } U u; V v; myfunc(u,v); I know with Option2, there are no copies/moves but it looks ugly. Will there be any copies/moves occur in Option1, 1.1? Lets assume U and V are huge objects supporting both copy/move operations. Q: Is it theoretically possible for any RVO/NRVO optimizations as per the