STL (a) of the new language features C ++ standard library (a) of the new language features

C ++ standard library (a) of the new language features

New language features

  • nullptr is interpreted as a void*different from NULL is interpreted as aint
  • You can use auto complete automatic derivation at compile time, it will not affect the speed of execution
  • New for cyclic manner:
for(decl : coll)
{
    statement
}
  • rvalue reference is a Reference , that is to say, the following code is invalid:
X&& foo()
{
    X x;
    return std::move(x); }
  • Keywords: expllicit means that during object construction can not have an explicit type conversion
  • Keywords: noexcept used to indicate a function is not going to throw an exception. An exception occurred at compile time rather than run, but the runtime exception will cause the compiler to generate additional instruction code. This statement can be used in the destructor, swap function, move constructors and move assignment operator. In short, functions, and resources related to the management should not be thrown.
void foo() noexcept
  • Keywords: constexpr can be used to make an expression approved in the compiler.
  • Keywords: the mutable modified variables can be changed under any circumstances, even if the variable is const modified
  • Lambda expressions:
int x,y;
auto lambda = [x,&y]()->double{return 42+x*y;};
  • Keywords: decltype used to describe the type of expression, two basic uses:
int x,y;

decltype(x+y) add(int x,int y); auto add(int x,int y) -> decltype(x+y);

General Tools

  • For tuple and pair of elements is:
std::tuple<int,std::string,std::vector<int>> tupple; auto one = std::get<0>(tupple); auto two = std::get<1>(tupple); auto three = std::get<2>(tupple);

Can be obtained pair or tuple elements.
pair default implementation < comparison operator, and at first value as the comparison.

  • shared_ptr and unique_ptr usage:
shared_ptr<int> ptr(new int[10],[](int* array){delete [] array;}); unique_ptr<int,void(*)(int*)> uptr(new int[10],[](int* array){delete [] array;}); ptr.get(); //获得指向资源的裸指针,若为空,返回nullptr ptr.use_count(); //返回共享计数 ptr.unique(); //返回是否唯一
  • weak_ptr Detailed: shared_ptr advanced usage: the this pointer into shared_ptr handed over to the callback function type of treatment
    • Solution: two shared_ptr circular references
    • Solution: Want to share but do not want to have a situation
    std::shared_ptr<std::string> strPtr;
    std::weak_ptr<std::string> strWPtr = strPtr; auto newPtr = strWPtr.lock(); //将WPtr升级为Ptr strWPtr.expired(); //如果Wptr有共享对象,返回True,速度快于use_count strWPtr.use_count(); //返回Ptr的共享个数
class Find : public std::enable_shared_from_this<Find> { ... ... auto thisPtr = shared_from_this(); }
  • Function Object to use as the Smart Pointer deletion is to reduce the function of runtime overhead
  • <cstring>Common functions:
memchr(const void* ptr,int c,size_t len); memcmp(const void* ptr1,const void* ptr2,size_t len); memcpy(void* toPtr,const void* fromPtr,size_t len); memmove(void* toPtr,const void* fromPtr,size_t len); memset(void* ptr,int c,size_t len);

STL Overview

  • Insert Iterator iterator type placement streaming Stream Iterator iterator
    • Back_Insert: placement in a vessel endmost
    • Front_Insert: Security at the forefront of container
    • General_Insert: placement position of the container at the specified
      routine of all the elements in the list placed at the far end of the vector:
    list<int> list{1,2,3,4,5,6,7,8,9}; vector<int> vector; std::copy(list.begin(),list.end(),back_inserter(vector));
    back_inserter(container);//尾安插
    front_inserter(container);//头安插
    inserter(container,pos);//pos安插
std::vector<std::string> vecStream; std::copy(std::istream_iterator<std::string>(std::cin),std::istream_iterator<std::string>(),std::back_inserter(vecStream)); std::sort(vecStream.begin(),vecStream.end()); std::unique_copy(vecStream.cbegin(),vecStream.cend(),std::ostream_iterator<std::string>(std::cout,"\n"));
  • Any generic algorithms "to access the container iterators", they can not by calling any class member functions provided by the container iterators.
  • In any case, the preferred container member functions province, followed by the generic algorithms
  • Move iterator
std::list<std::string> s; ... std::vector<std::string> vec(s.begin(),s.end()); //copy std::vector<std::string> vec(make_move_iterator(s.begin(),s.end())); //move
  • Analyzing the single formula the Predicate : bool return type of data as the sort criterion or criteria to find.
  • 双参判断式:Binary Predicate

参考博客园大佬,以后可能会用到,有的暂时还没遇见过,大佬就是牛逼

新语言特性

  • nullptr被解释为一个void*,不同于NULL被解释为一个int
  • 可以用auto在编译期完成自动推导,不会影响执行期的速度
  • 新的for循环方式:
for(decl : coll)
{
    statement
}
  • rvalue reference也是一个reference,也就是说,下面这段代码是无效的:
X&& foo()
{
    X x;
    return std::move(x); }
  • 关键字:expllicit意味着在对象构造期间不能有显式类型转换
  • 关键字:noexcept用来表示某个函数不打算抛出异常。异常出现在运行期而非编译期,但是运行期的异常会使得编译器产生额外的指令代码。可在析构函数,swap函数,move构造函数和move assignment操作符使用此声明。总之,和资源管理相关的函数都不应该抛出异常.
void foo() noexcept
  • 关键字:constexpr可用来让表达式核定于编译器。
  • 关键字:mutable修饰任何情况下都可变的变量,即使该变量被const修饰
  • Lambda表达式:
int x,y;
auto lambda = [x,&y]()->double{return 42+x*y;};
  • 关键字:decltype用于描述表达式类型,两种基本用法:
int x,y;

decltype(x+y) add(int x,int y); auto add(int x,int y) -> decltype(x+y);

通用工具

  • 对于tuplepair元素来说:
std::tuple<int,std::string,std::vector<int>> tupple; auto one = std::get<0>(tupple); auto two = std::get<1>(tupple); auto three = std::get<2>(tupple);

可以获得pair或者tuple中的元素。
pair默认实现<比较运算符,且以first的值作为比较的对象。

  • shared_ptrunique_ptr的用法:
shared_ptr<int> ptr(new int[10],[](int* array){delete [] array;}); unique_ptr<int,void(*)(int*)> uptr(new int[10],[](int* array){delete [] array;}); ptr.get(); //获得指向资源的裸指针,若为空,返回nullptr ptr.use_count(); //返回共享计数 ptr.unique(); //返回是否唯一
  • weak_ptr详解:shared_ptr的高级用法:将this指针转化为shared_ptr类型交由回调函数处理
    • 解决:两个shared_ptr循环引用的问题
    • 解决:想要共享但是不想拥有的状况
    std::shared_ptr<std::string> strPtr;
    std::weak_ptr<std::string> strWPtr = strPtr; auto newPtr = strWPtr.lock(); //将WPtr升级为Ptr strWPtr.expired(); //如果Wptr有共享对象,返回True,速度快于use_count strWPtr.use_count(); //返回Ptr的共享个数
class Find : public std::enable_shared_from_this<Find> { ... ... auto thisPtr = shared_from_this(); }
  • 使用Function Object作为上述Smart Pointer的删除器,以减少函数运行期的开销
  • <cstring>常用函数:
memchr(const void* ptr,int c,size_t len); memcmp(const void* ptr1,const void* ptr2,size_t len); memcpy(void* toPtr,const void* fromPtr,size_t len); memmove(void* toPtr,const void* fromPtr,size_t len); memset(void* ptr,int c,size_t len);

STL概述

  • 安插型迭代器 Insert Iterator串流迭代器 Stream Iterator
    • Back_Insert:安插于容器的最末端
    • Front_Insert:安处于容器的最前端
    • General_Insert:安插于容器的指定位置
      下述例程将list中的所有元素安插到vector的最末端:
    list<int> list{1,2,3,4,5,6,7,8,9}; vector<int> vector; std::copy(list.begin(),list.end(),back_inserter(vector));
    back_inserter(container);//尾安插
    front_inserter(container);//头安插
    inserter(container,pos);//pos安插
std::vector<std::string> vecStream; std::copy(std::istream_iterator<std::string>(std::cin),std::istream_iterator<std::string>(),std::back_inserter(vecStream)); std::sort(vecStream.begin(),vecStream.end()); std::unique_copy(vecStream.cbegin(),vecStream.cend(),std::ostream_iterator<std::string>(std::cout,"\n"));
  • 任何“以迭代器访问容器“的泛型算法,都无法通过通过迭代器调用容器类所提供的任何成员函数。
  • 在任何情况下,优先选用容器本省的成员函数,其次才是泛型算法
  • Move迭代器
std::list<std::string> s; ... std::vector<std::string> vec(s.begin(),s.end()); //copy std::vector<std::string> vec(make_move_iterator(s.begin(),s.end())); //move
  • 单个判断式Predicate:会返回bool类型的数据作为排序准则或者查找准则。
  • 双参判断式:Binary Predicate

参考博客园大佬,以后可能会用到,有的暂时还没遇见过,大佬就是牛逼

Guess you like

Origin www.cnblogs.com/xcb-1024day/p/11332451.html