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

后端 未结 16 1817
伪装坚强ぢ
伪装坚强ぢ 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 07:52

    u simply use for loop as done below:-

    for (int i=0; i<100; i++)
    { 
    a[i]= -1;
    }
    

    as a result as u want u can get A[100]={-1,-1,-1..........(100 times)}

    0 讨论(0)
  • 2020-12-05 07:54

    I would suggest using std::array. For three reasons:
    1. array provides runtime safety against index-out-of-bound in subscripting (i.e. operator[]) operations,
    2. array automatically carries the size without requiring to pass it separately
    3. And most importantly, array provides the fill() method that is required for this problem

    #include <array>
    #include <assert.h>
    
    typedef std::array< int, 100 > DirectoryArray;
    
    void test_fill( DirectoryArray const & x, int expected_value ) {
        for( size_t i = 0; i < x.size(); ++i ) {
            assert( x[ i ] == expected_value );
        }
    }
    
    int main() {
        DirectoryArray directory;
        directory.fill( -1 );
        test_fill( directory, -1 );
        return 0;
    }
    

    Using array requires use of "-std=c++0x" for compiling (applies to the above code).

    If that is not available or if that is not an option, then the other options like std::fill() (as suggested by GMan) or hand coding the a fill() method may be opted.

    0 讨论(0)
  • 2020-12-05 07:54

    It does work right. Your expectation of the initialiser is incorrect. If you really wish to take this approach, you'll need 100 comma-separated -1s in the initialiser. But then what happens when you increase the size of the array?

    0 讨论(0)
  • 2020-12-05 07:55

    Can't do what you're trying to do with a raw array (unless you explicitly list out all 100 -1s in the initializer list), you can do it with a vector:

    vector<int> directory(100, -1);
    

    Additionally, you can create the array and set the values to -1 using one of the other methods mentioned.

    0 讨论(0)
  • 2020-12-05 07:56

    I'm surprised at all the answers suggesting vector. They aren't even the same thing!

    Use std::fill, from <algorithm>:

    int directory[100];
    std::fill(directory, directory + 100, -1);
    

    Not concerned with the question directly, but you might want a nice helper function when it comes to arrays:

    template <typename T, size_t N>
    T* end(T (&pX)[N])
    {
        return pX + N;
    }
    

    Giving:

    int directory[100];
    std::fill(directory, end(directory), -1);
    

    So you don't need to list the size twice.

    0 讨论(0)
  • 2020-12-05 07:56

    If you really need arrays, you can use boosts array class. It's assign member does the job:

    boost::array<int,N> array; // boost arrays are of fixed size!
    array.assign(-1);
    
    0 讨论(0)
提交回复
热议问题