zoukankan      html  css  js  c++  java
  • C++11新特性

    一、原生字符串(raw string literals)

    比如我们写硬盘上一个文件的访问路径:"C:Program FilesMicrosoft.NETADOMD.NET",你需要把它写成以下格式

    string path = "C:\Program Files\Microsoft.NET\ADOMD.NET";

    这样取消字符串转义太麻烦了,C++11下可以这样写:

    string path = R"(C:Program FilesMicrosoft.NETADOMD.NET)";

    除了上面这样,你还可以使用引号

    string path = R"(this "word" is good)";

    二、委托构造函数(Delegating constructors)

    说简单一点就是你的一个构造函数可以调用另一个构造函数

    #include <bits/stdc++.h>
    using namespace std;
    multimap<int, int> r;
    multimap<int, int>::iterator it;
    class people
    {
    public:
        string name;
        int age;
        int id;
        people(int age,int id):age(age),id(id)
        {
            
        }
        //委托构造函数不能具有其他成员初始化表达式
        // people(int age,int id,string name):name(name),people(age,id)
        // {
    
        // }
        people(int age,int id,string name):people(age,id)
        {
            
        }
    }

    三、初始化列表

    用列表的形式来初始化数组,这种方式非常直观,但只能适用于数组,不能适用于我们自定义的容器: 

    int anArray[5] = { 3, 2, 7, 5, 8 }; // ok

    std::vector<int> vArray = { 3, 2, 7, 5, 8 }; // 不支持

    C++11现在支持了

    #include <bits/stdc++.h>
    using namespace std;
    template<typename T>
    class test{
        private:
         vector<T> arr;
        public:
         test(){}
         test(const initializer_list<T>& li){
             for(auto x : li){
                 arr.push_back(x);
             }
         }
    };
    class Value{
        public:
        int x,y;
        double z;
        Value(int a,int b,double c):x(a),y(b),z(c){
    
        }
    };
    int main(){
        test<int> t={1,2,3,4};
        return 0;
    }

    四、统一初始化

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

    #include <bits/stdc++.h>
    using namespace std;
    multimap<int, int> r;
    multimap<int, int>::iterator it;
    class Value{
        public:
        int x,y;
        double z;
        //不加这个构造函数也不会报错,但是为了看起来清晰建议还是加上
        Value(int a,int b,double c):x(a),y(b),z(c){
    
        }
    };
    Value te(){
        return {1,2,0.1};
    }
    int main(){
        Value v = te();
        printf("%d %d %lf
    ",v.x,v.y,v.z);
        return 0;
    }
    #include <bits/stdc++.h>
    using namespace std;
    multimap<int, int> r;
    multimap<int, int>::iterator it;
    int main()
    {
        r.insert({1,2});
        r.insert({1,3});
        for(it=r.begin();it!=r.end();it++){
            printf("%d %d
    ",it->first,it->second);
            //std:cout << it->first << " is " << it->second << std::endl;
        }
        return 0;
    }
  • 相关阅读:
    Python学习(二十四)—— 前端基础之Bookstrap
    Python学习(二十三)—— 前端基础之jQuery
    Python心得基础篇【4】文件操作
    Python心得基础篇【3】基本知识
    Python心得基础篇【2】函数
    WCF使用net.tcp绑定的配置实例
    WCF:如何将net.tcp协议寄宿到IIS
    WCF使用net.Tcp绑定时的注意事项
    解决WCF跨机器调用时发生“调用方未由服务进行身份验证”的错误
    在IIS中部署好WCF服务站点后,本机访问服务无问题,局域网中其他电脑访问不到
  • 原文地址:https://www.cnblogs.com/kongbursi-2292702937/p/14724058.html
Copyright © 2011-2022 走看看