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

    C++17都出来了,是时候整理一波C++11旧特性了。

    struct Foo {
        std::string name;
        int age;
    };
    
    enum class Options {
        None,
        One,
        All
    };
    
    class Person
    {
    public:
    //    Person(int newAge):age(newAge) { std::cout << "Person constructor" << std::endl;}
        int age = 20;
        std::string name = "logan";
        bool isSingle = true;
    };
    #include <iostream>
    #include <string>
    #include <vector>
    #include <map>
    
    #include "myHeader.h"
    
    using namespace std;
    
    
    int main(int argc, char **argv)
    {
        auto i = 42;
        auto l = 42LL;
        auto p = new Foo();
        cout << "sizeof(i) = " << sizeof(i) << endl;
        cout << "sizeof(l) = " << sizeof(l) << endl;
        cout << "sizeof(p) = " << sizeof(p) << endl;
        cout << "sizeof(*p) = " << sizeof(*p) << endl;
        
        map< string, vector<int> > mymap;
        for (int i = 0; i < 5; i++) {
            string key = string("key") + std::to_string(i);
            vector<int> values;
            for (int j = 0; j < 3; j++) {
                values.push_back(i + j * 10);
            }
            mymap[key] = values;
        }
        
        cout << endl;
        cout << "iterate mymap:" << endl;
        for (auto it = begin(mymap); it != end(mymap); ++it) {
            cout << "key = " << it->first << endl;
            cout << "value = ";
            for (auto iter = begin(it->second); iter != end(it->second); ++iter) {
                cout << *iter << " ";
            }
            cout << endl;
        }
        
        cout << endl;
        cout << "Range-based for loops..." << endl;
        for(const auto &it : mymap) {
            cout << "key = " << it.first << endl;
            cout << "value = ";
            for(const auto &iter : it.second) {
                cout << iter << " ";
            }
            cout << endl;
        }
        
        cout << endl;
        cout << "Strongly-typed enums..." << endl;
        Options myoption = Options::One;
        cout << "my option is: " << static_cast<int>(myoption) << endl;
        
        cout << endl;
        cout << "in-class initialization..." << endl;
        Person xiaoming;
        cout << "age = " << xiaoming.age << endl;
        cout << "name = " << xiaoming.name << endl;
        cout << "isSingle = " << xiaoming.isSingle << endl;
    }

    参考资料:

    C++开发者都应该使用的10个C++11特性

     http://zh.cppreference.com/w/cpp/memory/shared_ptr

    http://zh.cppreference.com/w/cpp/memory/weak_ptr

  • 相关阅读:
    仓储模式Repository
    jwt测试
    net core webapi jwt
    net core发布到iis遇到的困难
    新的目标
    L9-2.安装mysql数据库
    L9-1-安装Apache
    L8_2
    Linux 08
    Linux 07 故障恢复
  • 原文地址:https://www.cnblogs.com/gattaca/p/7158960.html
Copyright © 2011-2022 走看看