Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,563 questions

56,781 answers

573 users

How to initialize array elements in C++

2 Answers

0 votes
#include <iostream>
#include <algorithm> // For std::copy and std::fill_n

// Helper function to print array elements using C++17 template parameter deduction
template <typename T, std::size_t N>
void printCArray(const char* title, const T(&arr)[N]) {
    std::cout << "--- " << title << " ---\n[ ";
    for (std::size_t i = 0; i < N; ++i) {
        std::cout << arr[i] << (i + 1 < N ? ", " : " ");
    }
    std::cout << "]\n\n";
}

int main() {
    // -------------------------------------------------------------------------
    // EXAMPLE 1: Zero/Value Initialization
    // Zero-initializes all elements. Safe, clear, and avoids garbage memory.
    // -------------------------------------------------------------------------
    int zeroInit[5] = {}; 
    printCArray("1. Zero / Value Initialization", zeroInit);

    // -------------------------------------------------------------------------
    // EXAMPLE 2: Direct List Initialization (Full)
    // Explicitly provides values for every element.
    // -------------------------------------------------------------------------
    int fullInit[5] = {10, 20, 30, 40, 50};
    printCArray("2. Direct List Initialization (Full)", fullInit);

    // -------------------------------------------------------------------------
    // EXAMPLE 3: Partial List Initialization
    // Unspecified trailing elements are automatically zero-initialized.
    // -------------------------------------------------------------------------
    int partialInit[5] = {1, 2}; // Elements will be [1, 2, 0, 0, 0]
    printCArray("3. Partial List Initialization", partialInit);

    // -------------------------------------------------------------------------
    // EXAMPLE 4: Automatic Size Deduction
    // Let the compiler deduce the array size from the initializer count.
    // -------------------------------------------------------------------------
    int deducedInit[] = {100, 200, 300, 400}; // Size deduced as 4
    printCArray("4. Automatic Size Deduction", deducedInit);

    // -------------------------------------------------------------------------
    // EXAMPLE 5: Algorithm-Based Fill (std::fill_n)
    // Useful for setting non-zero default values at runtime.
    // -------------------------------------------------------------------------
    int filledInit[5];
    std::fill_n(filledInit, 5, 42); // Sets all 5 elements to 42
    printCArray("5. Standard Algorithm Fill (std::fill_n)", filledInit);
}



/*
run:

--- 1. Zero / Value Initialization ---
[ 0, 0, 0, 0, 0 ]

--- 2. Direct List Initialization (Full) ---
[ 10, 20, 30, 40, 50 ]

--- 3. Partial List Initialization ---
[ 1, 2, 0, 0, 0 ]

--- 4. Automatic Size Deduction ---
[ 100, 200, 300, 400 ]

--- 5. Standard Algorithm Fill (std::fill_n) ---
[ 42, 42, 42, 42, 42 ]

*/

 



answered Oct 6 by avibootz
0 votes
#include <iostream>
#include <array>
#include <algorithm>

// Modern Standard Arrays (std::array)

template <typename T, std::size_t N>
void printStdArray(const char* title, const std::array<T, N>& arr) {
    std::cout << "--- " << title << " ---\n[ ";
    for (std::size_t i = 0; i < arr.size(); ++i) {
        std::cout << arr[i] << (i + 1 < arr.size() ? ", " : " ");
    }
    std::cout << "]\n\n";
}

int main() {
    // -------------------------------------------------------------------------
    // EXAMPLE 1: Value Initialization (std::array)
    // -------------------------------------------------------------------------
    std::array<int, 5> stdZeroInit{}; // All elements initialized to 0
    printStdArray("1. std::array Value Initialization", stdZeroInit);

    // -------------------------------------------------------------------------
    // EXAMPLE 2: List Initialization (std::array)
    // -------------------------------------------------------------------------
    std::array<int, 5> stdListInit = {1, 2, 3, 4, 5};
    printStdArray("2. std::array List Initialization", stdListInit);

    // -------------------------------------------------------------------------
    // EXAMPLE 3: Member fill() Method
    // Assigns a value to all elements after construction.
    // -------------------------------------------------------------------------
    std::array<int, 5> stdFilled;
    stdFilled.fill(7); // Sets every element to 7
    printStdArray("3. std::array Member fill()", stdFilled);

    // -------------------------------------------------------------------------
    // EXAMPLE 4: Deduction Guides (C++17 std::to_array)
    // Converts a C-style array/string literal to std::array while deducing size and type.
    // -------------------------------------------------------------------------
    auto stdDeducted = std::to_array({11, 22, 33, 44});
    printStdArray("4. std::to_array Deduction (C++17)", stdDeducted);
}



/*
run:

--- 1. std::array Value Initialization ---
[ 0, 0, 0, 0, 0 ]

--- 2. std::array List Initialization ---
[ 1, 2, 3, 4, 5 ]

--- 3. std::array Member fill() ---
[ 7, 7, 7, 7, 7 ]

--- 4. std::to_array Deduction (C++17) ---
[ 11, 22, 33, 44 ]

*/

 



answered Oct 6 by avibootz
...