C++11数组初始化


  • 网格教授 OpenFOAM教授 管理员

    记录一下,还没用过。

    在11之前,只能

    class Something
    {
    private:
        const int m_array[5];
     
    public:
        Something(): m_array {} // zero the member array
        {
            // If we want the array to have values, we'll have to use assignment here
        }
     
    };
    

    在11之后,可以数组直接初始化:

    class Something
    {
    private:
        const int m_array[5];
     
    public:
        Something(): m_array { 1, 2, 3, 4, 5 } // use uniform initialization to initialize our member array
        {
        }
     
    };
    

    http://www.learncpp.com/cpp-tutorial/8-5a-constructor-member-initializer-lists/


  • OpenFOAM副教授

    这样改进的好处是什么呢?

    我的c++基础也都是从 learncpp 上学来的,那个博主真的是用心。


  • OpenFOAM副教授

    @random_ran 初始化列表,在构造的同时赋值,提高执行效率


  • OpenFOAM副教授

    @wwzhao

    简单的感受了一下两种赋值方式,效率确实很大提高。

    #include <stdio.h>
    #include <string>
    #include <chrono>
    #include <iostream>
    
    using namespace std ;
    
    class Timer
    {
    private:
      // Type aliases to make accessing nested type easier                                                                                                                                          
      using clock_t = std::chrono::high_resolution_clock;
      using second_t = std::chrono::duration<double, std::ratio<1> >;
    
      std::chrono::time_point<clock_t> m_beg;
    
    public:
      Timer() : m_beg(clock_t::now())
      {
      }
    
      void reset()
      {
        m_beg = clock_t::now();
      }
    
      double elapsed() const
      {
        return std::chrono::duration_cast<second_t>(clock_t::now() - m_beg).count();
      }
    };
    
    class SomethingBefore11
    {
    private:
      int m_array[5];
    
    public:
       SomethingBefore11() // zero the member array                                                                                                                                                  
      {
        // If we want the array to have values, we'll have to use assignment here                                                                                                                    
        m_array[0] = 1;
        m_array[1] = 2;
        m_array[2] = 3;
        m_array[3] = 4;
        m_array[4] = 5;
      }
    };
    
    class SomethingAfter11
    {
    private:
      int m_array[5];
    
    public:
      SomethingAfter11(): m_array { 1, 2, 3, 4, 5 } //zero the member array                                                                                                                          
      {
      }
    
    };
    int main(){
    
      Timer tBefore11;
      SomethingBefore11 m_array_before11;
      std::cout << "Time elapsed: " << tBefore11.elapsed() << ‘n’;
    
      Timer tAfter11;
      SomethingAfter11 m_array_after11;
      std::cout << "Time elapsed: " << tAfter11.elapsed() << ‘n’;
      return 0;
    
    }
    
    
    [xx OFtutorial0_helloWorld]$ whatAboutThisGuy
    Time elapsed: 7.506e-06
    Time elapsed: 1.47e-07
    [xx OFtutorial0_helloWorld]$ whatAboutThisGuy
    Time elapsed: 8.664e-06
    Time elapsed: 1.9e-07
    [xx OFtutorial0_helloWorld]$ whatAboutThisGuy
    Time elapsed: 7.646e-06
    Time elapsed: 1.89e-07