C++基础::便捷函数

来源:互联网 发布:淘宝店铺代销货源 编辑:程序博客网 时间:2024/06/05 10:12

便捷函数是对原始类模板的一次封装,通过函数模板的类型推导机制,实现模板参数列表的传递。

template<typename T1, typename T2>std::pair<T1, T2> make_pair(const T1& x, const T2& y){       return pair<T1, T2>(x, y);}
stuff 便捷函数 说明 pair<> make_pair() <utility> tuple<> make_tuple() <tuple> shared_ptr<> make_shared<>() <memory> reference_wrapper<> ref()
cref() <functional>

make_shared

std::shared_ptr<std::string> pNico = std::make_shared<std::string>("nico");class A {public:    A(int){}};shared_ptr<A> pA = make_shared<A>(5);

这种建立方式比较快,也比较安全,它使用一次而非两次分配

std::distance()

头文件:

#include <utility>

对于 set 容器类的迭代器:
s.end()-s.begin()是不支持的,而 std::distance() 可以达到通用性:

std::distance(s.begin(), s.end());
0 0
原创粉丝点击