print函数数组 - c ++

时间:2017-10-29 02:04:54

标签: c++ arrays

我正在使用C ++中的print()函数,并且想知道:

template <typename BaseType>
void print(BaseType data, bool newline = false, bool raw = false) {
    // Standard > C Out
    std::cout << data;

    /* Logic
            If
                Newline is true.
    */
    if (newline)
        std::cout << std::endl;
};

如果同一个函数对数组的反应不同并打印出数组的每个成员而不是返回像0x22fe30这样的值,该怎么办?

类似的东西:

print("Hello, World!"); // prints "Hello, World!"
print(array); // prints "[1, 0, 1]"

我只是为了好玩才能看到我的C ++技能到底有多远,并且会感谢任何有用的答案。感谢。

1 个答案:

答案 0 :(得分:0)

您可以迭代到数组并打印出来:

const int arr [ 3 ] = { 5, 6, 7 };

for ( int i = 0; i < 3; i++ )

    std::cout << arr [ i ] << std::endl;