草庐IT

make_pair

全部标签

c++ - 如何结合 std::make_shared 和 new(std::nothrow)

C++的new有一个选项可以在分配失败时返回空指针而不是抛出bad_alloc异常。Foo*pf=new(std::nothrow)Foo(1,2,3);(是的,我知道这只会阻止new抛出bad_alloc;它不会阻止Foo的构造函数抛出异常。)如果您想使用共享指针而不是原始指针,您通常应该使用make_shared,因为它可以巧妙地分配控制block。autopf=std::make_shared(1,2,3);make_shared封装了新版本,这使得(?)无法选择nothrow版本。因此,您似乎必须放弃make_shared并明确调用new。std::shared_ptrpf(n

c++ - 如何结合 std::make_shared 和 new(std::nothrow)

C++的new有一个选项可以在分配失败时返回空指针而不是抛出bad_alloc异常。Foo*pf=new(std::nothrow)Foo(1,2,3);(是的,我知道这只会阻止new抛出bad_alloc;它不会阻止Foo的构造函数抛出异常。)如果您想使用共享指针而不是原始指针,您通常应该使用make_shared,因为它可以巧妙地分配控制block。autopf=std::make_shared(1,2,3);make_shared封装了新版本,这使得(?)无法选择nothrow版本。因此,您似乎必须放弃make_shared并明确调用new。std::shared_ptrpf(n

iphone - Objective-C 中 C++ STL 容器 "pair<T1, T2>"的等价物?

我是Objective-C的新手,所以请不要过多评价我。我想知道:有没有可以在Objective-C中使用的等效C++STL对容器?我想构建一个包含与NSBool关联的NSInteger的数组。我知道我可以使用一个数组,每个条目都是一个具有单个键值的NSDictionary,但我发现它有点矫枉过正。有什么想法吗?谢谢。 最佳答案 您可以编写自己的数据结构对象-对于这样一个简单的情况,这将非常容易:@interfacePair:NSObject{NSIntegerinteger;BOOLboolean;}@property(nonat

iphone - Objective-C 中 C++ STL 容器 "pair<T1, T2>"的等价物?

我是Objective-C的新手,所以请不要过多评价我。我想知道:有没有可以在Objective-C中使用的等效C++STL对容器?我想构建一个包含与NSBool关联的NSInteger的数组。我知道我可以使用一个数组,每个条目都是一个具有单个键值的NSDictionary,但我发现它有点矫枉过正。有什么想法吗?谢谢。 最佳答案 您可以编写自己的数据结构对象-对于这样一个简单的情况,这将非常容易:@interfacePair:NSObject{NSIntegerinteger;BOOLboolean;}@property(nonat

c++ - 为什么没有 std::make_function()?

std::function是几乎所有可调用事物的有用包装器,包括自由函数、lambda、仿函数、成员函数,结果来自std::bind.但是,当创建std::function,必须明确指定函数签名,如(取自here)structFoo{Foo(intnum):num_(num){}voidprint_add(inti)const{std::coutf_display=print_num;//storealambdastd::functionf_display_42=[](){print_num(42);};//storetheresultofacalltostd::bindstd::fu

c++ - 为什么没有 std::make_function()?

std::function是几乎所有可调用事物的有用包装器,包括自由函数、lambda、仿函数、成员函数,结果来自std::bind.但是,当创建std::function,必须明确指定函数签名,如(取自here)structFoo{Foo(intnum):num_(num){}voidprint_add(inti)const{std::coutf_display=print_num;//storealambdastd::functionf_display_42=[](){print_num(42);};//storetheresultofacalltostd::bindstd::fu

c++ - 如何使用具有 pair<int,int> vector 元素的 unordered_set

我想拥有类似的东西unordered_set>>us;但即使没有配对:#include#includeusingnamespacestd;intmain(){unordered_set>um;}失败了:Infileincludedfrom/usr/include/c++/4.8/bits/hashtable.h:35:0,from/usr/include/c++/4.8/unordered_set:47,fromprog.cpp:2:/usr/include/c++/4.8/bits/hashtable_policy.h:Ininstantiationof‘structstd::__d

c++ - 如何使用具有 pair<int,int> vector 元素的 unordered_set

我想拥有类似的东西unordered_set>>us;但即使没有配对:#include#includeusingnamespacestd;intmain(){unordered_set>um;}失败了:Infileincludedfrom/usr/include/c++/4.8/bits/hashtable.h:35:0,from/usr/include/c++/4.8/unordered_set:47,fromprog.cpp:2:/usr/include/c++/4.8/bits/hashtable_policy.h:Ininstantiationof‘structstd::__d

c++ - 在 std::make_shared 中使用 c++ 聚合初始化

根据我的理解,以下代码构造了一个Foo类型的对象,然后将该对象移动到std::make_shared分配的内存中structFoo{std::strings;inti;charc;};intmain(intargc,char*argv[]){autofoo=std::make_shared(Foo{"hello",5,'c'});}有没有可能aggregateinitializeFoo直接放入std::make_shared分配的内存? 最佳答案 您可以使用可变参数构造函数模板创建一个适配器来转发参数,例如:templatestru

c++ - 在 std::make_shared 中使用 c++ 聚合初始化

根据我的理解,以下代码构造了一个Foo类型的对象,然后将该对象移动到std::make_shared分配的内存中structFoo{std::strings;inti;charc;};intmain(intargc,char*argv[]){autofoo=std::make_shared(Foo{"hello",5,'c'});}有没有可能aggregateinitializeFoo直接放入std::make_shared分配的内存? 最佳答案 您可以使用可变参数构造函数模板创建一个适配器来转发参数,例如:templatestru