Initialization of all elements of an array to one default value in C++?

前端 未结 13 1177
礼貌的吻别
礼貌的吻别 2020-11-22 07:59

C++ Notes: Array Initialization has a nice list over initialization of arrays. I have a

int array[100] = {-1};

expecting it to be full with

13条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 08:26

    In the C++ programming language V4, Stroustrup recommends using vectors or valarrays over builtin arrays. With valarrary's, when you create them, you can init them to a specific value like:

    valarray seven7s=(7777777,7);
    

    To initialize an array 7 members long with "7777777".

    This is a C++ way of implementing the answer using a C++ data structure instead of a "plain old C" array.

    I switched to using the valarray as an attempt in my code to try to use C++'isms v. C'isms....

提交回复
热议问题