How to initialize all elements in an array to the same number in C++

后端 未结 16 1819
伪装坚强ぢ
伪装坚强ぢ 2020-12-05 07:17

I\'m trying to initialize an int array with everything set at -1.

I tried the following, but it doesn\'t work. It only sets the first value at -1.

in         


        
相关标签:
16条回答
  • 2020-12-05 08:00

    Just use this loop.

    for(int i =0 ; i < 100 ; i++) directory[i] =0;
    
    0 讨论(0)
  • 2020-12-05 08:00

    the almighty memset() will do the job for array and std containers in C/C++/C++11/C++14

    0 讨论(0)
  • 2020-12-05 08:00

    The reason that int directory[100] = {-1} doesn't work is because of what happens with array initialization.

    All array elements that are not initialized explicitly are initialized implicitly the same way as objects that have static storage duration.

    ints which are implicitly initialized are:

    initialized to unsigned zero

    All array elements that are not initialized explicitly are initialized implicitly the same way as objects that have static storage duration.

    C++11 introduced begin and end which are specialized for arrays!

    This means that given an array (not just a pointer), like your directory you can use fill as has been suggested in several answers:

    fill(begin(directory), end(directory), -1)
    

    Let's say that you write code like this, but then decide to reuse the functionality after having forgotten how you implemented it, but you decided to change the size of directory to 60. If you'd written code using begin and end then you're done.
    If on the other hand you'd done this: fill(directory, directory + 100, -1) then you'd better remember to change that 100 to a 60 as well or you'll get undefined behavior.

    0 讨论(0)
  • 2020-12-05 08:00

    If you are allowed to use std::array, you can do the following:

    #include <iostream>
    #include <algorithm>
    #include <array>
    using namespace std;
    
    template <class Elem, Elem pattern, size_t S, size_t L>
    struct S_internal {
        template <Elem... values>
        static array<Elem, S> init_array() {
            return S_internal<Elem, pattern, S, L - 1>::init_array<values..., pattern>();
        }
    };
    
    template <class Elem, Elem pattern, size_t S>
    struct S_internal<Elem, pattern, S, 0> {
        template <Elem... values>
        static array<Elem, S> init_array() {
            static_assert(S == sizeof...(values), "");
            return array<Elem, S> {{values...}};
        }
    };
    
    template <class Elem, Elem pattern, size_t S>
    struct init_array
    {
        static array<Elem, S> get() {
            return S_internal<Elem, pattern, S, S>::init_array<>();
        }
    };
    
    void main()
    {
        array<int, 5> ss = init_array<int, 77, 5>::get();
        copy(cbegin(ss), cend(ss), ostream_iterator<int>(cout, " "));
    }
    

    The output is:

    77 77 77 77 77

    0 讨论(0)
  • 2020-12-05 08:02

    Starting with C++11 you could also use a range based loop:

    int directory[10];
    for (auto& value: directory) value = -1;
    
    0 讨论(0)
  • 2020-12-05 08:05

    I had the same question and I found how to do, the documentation give the following example :

    std::array<int, 3> a1{ {1, 2, 3} }; // double-braces required in C++11 (not in C++14)
    

    So I just tried :

    std::array<int, 3> a1{ {1} }; // double-braces required in C++11 (not in C++14)
    

    And it works all elements have 1 as value. It does not work with the = operator. It is maybe a C++11 issue.

    0 讨论(0)
提交回复
热议问题