将原始数组初始化为一个值

编程入门 行业动态 更新时间:2024-10-20 03:25:04
本文介绍了将原始数组初始化为一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有没有办法初始化一个基本数组,比如一个整数数组,到0?不使用for循环?寻找不涉及for循环的简明代码。

Is there a way to initialize an array of primitives, say a integer array, to 0? Without using a for loop? Looking for concise code that doesn't involve a for loop.

:)

推荐答案 int array[10] = {}; // to 0 std::fill(array, array + 10, x); // to x

请注意,通用方式结束:

Note if you want a more generic way to get the end:

template <typename T, size_t N> T* endof(T (&pArray)[N]) { return &pArray[0] + N; }

获取:

std::fill(array, endof(array), x); // to x (no explicit size)

应该提到 std :: fill 只是一个包装你想要避免的循环, = {}; 可以用这样的术语来实现。

It should be mentioned std::fill is just a wrapper around the loop you're trying to avoid, and = {}; might be implemented in such terms.

更多推荐

将原始数组初始化为一个值

本文发布于:2023-10-09 06:00:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1474904.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   原始

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!