Skip to content
  • 最新
  • 版块
  • 东岳流体
  • 随机看[请狂点我]
皮肤
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • 默认(不使用皮肤)
  • 不使用皮肤
折叠
CFD中文网

CFD中文网

  1. CFD中文网
  2. Algorithm
  3. C++11数组初始化

C++11数组初始化

已定时 已固定 已锁定 已移动 Algorithm
4 帖子 3 发布者 4.9k 浏览
  • 从旧到新
  • 从新到旧
  • 最多赞同
回复
  • 在新帖中回复
登录后回复
此主题已被删除。只有拥有主题管理权限的用户可以查看。
  • 李东岳李 离线
    李东岳李 离线
    李东岳 管理员
    写于 最后由 编辑
    #1

    记录一下,还没用过。

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

    http://dyfluid.com/index.html
    需要帮助debug算例的看这个 https://cfd-china.com/topic/8018

    1 条回复 最后回复
  • R 离线
    R 离线
    random_ran 大神
    写于 最后由 编辑
    #2

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

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

    Yours in CFD,

    Ran

    W 1 条回复 最后回复
  • W 离线
    W 离线
    wwzhao 超神
    在 中回复了 random_ran 最后由 编辑
    #3

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

    R 1 条回复 最后回复
  • R 离线
    R 离线
    random_ran 大神
    在 中回复了 wwzhao 最后由 李东岳 编辑
    #4

    @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 条回复 最后回复

  • 登录

  • 登录或注册以进行搜索。
  • 第一个帖子
    最后一个帖子
0
  • 最新
  • 版块
  • 东岳流体
  • 随机看[请狂点我]