资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

C++11/14的新特性有哪些

这篇文章将为大家详细讲解有关C++11/14的新特性有哪些,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

我们提供的服务有:成都网站设计、网站建设、外贸网站建设、微信公众号开发、网站优化、网站认证、平阳ssl等。为数千家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的平阳网站制作公司

新的字符串表示方式——原生字符串(Raw String Literals)

C/C++中提供了字符串,字符串的转义序列,给输出带来了很多不变,如果需要原生义的时候,需要反转义,比较麻烦。

C++提供了,原生字符串,即字符串中无转义,亦无需再反义。详细规则见带码:

#include 
using namespace std;

string path = "C:\Program Files (x86)\alipay\aliedit\5.1.0.3754";
string path3 = "C:\\Program Files (x86)\\alipay\\aliedit\\5.1.0.3754";
//更简洁的表示
string path4 = R"(C:\Program Files (x86)\alipay\aliedit\5.1.0.3754)";
string path5 = R"(C:\Program "Files" (x86)\\alipay\aliedit\5.1.0.3754)";


int main(int argc, char *argv[])
{
  cout<

新的for循环——for(x:range)

C++为 for 提供 for range 的用法。

#include 
#include 
#include 
using namespace std;
int main(int argc, char *argv[])
{
  string str = "china";
   //!字符数组
  for(auto ch: str)
  {
    cout< vs = {"abc","xyz","mnq"};
  vector::iterator itr = vs.begin();
  for(; itr != vs.end(); itr++)
  {
    cout<<*itr< mis={{1,"c++"},{2,"java"},{3,"python"}};
  map::iterator itr = mis.begin();
  for(; itr != mis.end(); ++itr)
  {
    cout<<(*itr).first<<"\t"<second<

新的初始化的方式——Initializer List

1)常规方法——normal init

#include 
#include 

#include 
#include 
using namespace std;
int main(int argc, char *argv[])
{ 
#if 0
  vector vi(5);
  cout< vi2(5,10);
  for(auto i: vi2){
    cout< vi3;
  for(int i=0; i<10; i++){
    vi3.push_back(i);
  } 
  for(auto i: vi3){
    cout< li(5);
  cout< li2(5,10);
  cout< li3;
  for(int i=0; i<10; i++)
  {
    li3.push_back(i);
  } 
  cout< mis;

  mis.insert(pair(1,"c++"));
  mis.insert(pair(2,"java"));
  mis.insert(pair(3,"python"));
  mis.insert(map::value_type(4,"c"));
  mis.insert(map::value_type(5,"php"));
  for(auto is: mis)
  {
  cout<

2)初始化列表——Initializer List

#include 
#include 
#include 
#include 

using namespace std;

int main(int argc, char *argv[])
{
  vector iv = {1,2,3,4,5};
  list li = {1,2,3,4,5};
  map mis = {{1,"c"},{2,"c++"},
                    {3,"java"},{4,"scala"},
                    {5,"python"}};
  mis.insert({6,"ruby"});
  // map::iterator itr = mis.begin();
  // for(; itr != mis.end(); ++itr)
  // {
  // cout<first<< itr->second<

3)initializer_list(作入参)

#include 
#include 
using namespace std;
template 
class MyArray
{ 
private:
  vector m_Array;
public:
  MyArray() { }
  MyArray(const initializer_list& il)
  {
    for (auto x : il)
    m_Array.push_back(x);
  }
};

int main()
{
  MyArray foo = { 3, 4, 6, 9 };
  return 0;
}

统一的初始化风格(Uniform initialization)

C++中的初始化风格,大体有如下形式:

int a = 2; //"赋值风格"的初始化
int aa [] = { 2, 3 }; //用初始化列表进行的赋值风格的初始化
complex z(1, 2); //"函数风格"的初始化

C++ 11 中,允许通过以花括号的形式来调用构造函数。这样多种对象构造方式便可以统一起来了:

int a = { 2 };
int aa [] = { 2, 3 };
complex z = { 1, 2 };
#include 
using namespace std;

class complex
{ 
public:
  complex(int x, int y)
    :_x(x),_y(y){}
  private:
  int _x;
  int _y;
};

complex func(const complex & com)
{
  return {1,2};
} 

int main(int argc, char *argv[])
{
  int a = 10;
  int aa[] = {1,2,3};
  complex com(1,2);

//---------------------------
  int a_ = {1};
  int aa_[] = {1,2,3};
  complex com_ = {1,2};
  func({1,2});
  return 0;
}

auto自动类型推导

1)引入

#include 
using namespace std;

int func()
{
  return 8;
} 

int main(int argc, char *argv[])
{
  auto i = 5;
  auto &ri = i;
  auto rf = func();
  const auto *p = &ri;
  static auto si = 100;
  return 0;
}

2)语法

auto 能够实现类型的自我推导,并不代表一个实际的类型声明。auto 只是一个类型声明的占位符。
auto 声明的变量,必须马上初始化,以让编译器推断出它的实际类型,并在编译时将 auto 占位符替换为真正的类型。

3)用法

  • 不用于函数参数

#include 
#include 

using namespace std;

//void foo(auto i)
//{
// cout< vi;
  auto ivcp = vi;
  // vector va = vi;
  return 0;
}
  • 常用于STL

如迭代器的初始化,容器拷贝等。

decltype-类型指示器

1)获取表达式类型

auto 类型,作为占位符的存在来修饰变量,必须初始化,编译器通过初始化来确定 auto 所代表的类型。即必须定义变量。

如果,我仅希望得到类型,而不是具体的变量产生关系,该如何作到呢?decltype(expr); expr 代表被推导的表达式。由decltype推导所声明难过的变量,可初始化,也可不初始化。 

#include 
using namespace std;

int func()
{
  return 1;
} 

int main(int argc, char *argv[])
{
  int a = 10;
  cout<

2)推导规则

decltype(expr); 所推导出来的类型,完全与 expr 类型一致。同 auto 一样,在编译期间完成,并不会真正计算表达式的值。
应用

3)decltype与typedef联合应用

#include 
#include 
#include 

using namespace std;

int main(int argc, char *argv[])
{
  vector vi = {1,2,3,4,5,0};

  typedef decltype(vi.begin()) Itr;

  for(Itr itr = vi.begin(); itr != vi.end(); ++itr)
  {
    cout<<*itr< mis;
  mis.insert(map::value_type(1,"abc"));
  mis.insert(decltype(mis)::value_type(2,"java"));

  typedef decltype(map::value_type()) Int2String;
  
   mis.insert(Int2String(3,"c++"));
  for(auto& is:mis)
  {
    cout<

4)decltype +auto

C++11 增了返回类型后置(trailing-return-type,或跟踪返回类型),将 decltype 和 auto结合起来完成返回类型的推导。 

#include 

using namespace std;

template
R add(T a, U b)
{
  return a+b;
} 

template
auto add2(T a, U b)->decltype(a+b)
{
  return a+b;
} 

int main(int argc, char *argv[])
{
  int a = 1;
  float b = 1.1;
  auto ret = add(a,b);
  cout<(a,b);
  cout<

仿函数(functor)

1)语法

重载了 operator()的类的对象,在使用中,语法类型于函数。故称其为仿函数。此种用法优于常见的函数回调。 

class Add
{ 
public:
  int operator()(int x, int y)
  {
    return x+y;
  }
};

2)应用

#include 
using namespace std;
class Add
{ 
public:
  int operator()(int x, int y)
  {
    return x+y;
  }
};

int main(int argc, char *argv[])
{
  int a = 1 , b = 2;
  Add add;
  cout<

3)提高(带状态的functor)

相对于函数,仿函数,可以拥用初始状态,一般通过 class 定义私有成员,并在声明对象的时候,进行初始化。私有成员的状态,就成了仿函数的初始状态。而由于声明一个仿函数对象可以拥有多个不同初始状态的实例。 

#include 

using namespace std;

class Tax
{ 
public:
  Tax(float r, float b):_rate(r),_base(b){}
  
   float operator()(float money)
  {
  return (money-_base)*_rate;
  }
  private:
  float _rate;
  float _base;
};

int main(int argc, char *argv[])
{
  Tax high(0.40,30000);
  Tax middle(0.25,20000);
  Tax low(0.12,10000);
  cout<<"大于 3w 的税:"<

关于“C++11/14的新特性有哪些”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。


网页题目:C++11/14的新特性有哪些
当前链接:http://cdkjz.cn/article/ijoceo.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220