Navigation

    CFD中文网

    CFD中文网

    • Login
    • Search
    • 最新

    C++11数组初始化

    Algorithm
    3
    4
    2717
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • 李东岳
      李东岳 管理员 last edited by

      记录一下,还没用过。

      在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/

      线上CFD课程 7月1日报名截止 http://dyfluid.com/class.html
      CFD高性能服务器 http://dyfluid.com/servers.html

      1 Reply Last reply Reply Quote
      • R
        random_ran 副教授 last edited by

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

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

        Yours in CFD,

        Ran

        W 1 Reply Last reply Reply Quote
        • W
          wwzhao 教授 @random_ran last edited by

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

          R 1 Reply Last reply Reply Quote
          • R
            random_ran 副教授 @wwzhao last edited by 李东岳

            @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
            

            Yours in CFD,

            Ran

            1 Reply Last reply Reply Quote
            • First post
              Last post

            CFD中文网 | 东岳流体 | 京ICP备15017992号-2
            论坛登录问题反馈可联系 li.dy@dyfluid.com