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

后端 未结 16 1818
伪装坚强ぢ
伪装坚强ぢ 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:06

    Just use the fill_n() method.

    Example

    int n;
    cin>>n;
    int arr[n];
    int value = 9;
    fill_n(arr, n, value); // 9 9 9 9 9...
    

    Learn More about fill_n()

    or

    you can use the fill() method.

    Example

    int n;
    cin>>n;
    int arr[n];
    int value = 9;
    fill(arr, arr+n, value); // 9 9 9 9 9...
    

    Learn More about fill() method.

    Note: Both these methods are available in algorithm library (#include<algorithm>). Don't forget to include it.

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

    use vector of int instead a array.

    vector<int> directory(100,-1);                       // 100 ints with value 1
    
    0 讨论(0)
  • 2020-12-05 08:12

    It is working right. That's how list initializers work.

    I believe 6.7.8.10 of the C99 standard covers this:

    If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static storage duration is not initialized explicitly, then:

    • if it has pointer type, it is initialized to a null pointer;
    • if it has arithmetic type, it is initialized to (positive or unsigned) zero;
    • if it is an aggregate, every member is initialized (recursively) according to these rules;
    • if it is a union, the first named member is initialized (recursively) according to these rules.

    If you need to make all the elements in an array the same non-zero value, you'll have to use a loop or memset.

    Also note that, unless you really know what you're doing, vectors are preferred over arrays in C++:

    Here's what you need to realize about containers vs. arrays:

    1. Container classes make programmers more productive. So if you insist on using arrays while those around are willing to use container classes, you'll probably be less productive than they are (even if you're smarter and more experienced than they are!).
    2. Container classes let programmers write more robust code. So if you insist on using arrays while those around are willing to use container classes, your code will probably have more bugs than their code (even if you're smarter and more experienced).
    3. And if you're so smart and so experienced that you can use arrays as fast and as safe as they can use container classes, someone else will probably end up maintaining your code and they'll probably introduce bugs. Or worse, you'll be the only one who can maintain your code so management will yank you from development and move you into a full-time maintenance role — just what you always wanted!

    There's a lot more to the linked question; give it a read.

    0 讨论(0)
  • If you had a smaller number of elements you could specify them one after the other. Array initialization works by specifying each element, not by specifying a single value that applies for each element.

    int x[3] = {-1, -1, -1 };
    

    You could also use a vector and use the constructor to initialize all of the values. You can later access the raw array buffer by specifying &v.front()

    std::vector directory(100, -1);
    

    There is a C way to do it also using memset or various other similar functions. memset works for each char in your specified buffer though so it will work fine for values like 0 but may not work depending on how negative numbers are stored for -1.


    You can also use STL to initialize your array by using fill_n. For a general purpose action to each element you could use for_each.

    fill_n(directory, 100, -1);
    

    Or if you really want you can go the lame way, you can do a for loop with 100 iterations and doing directory[i] = -1;

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