对于循环变体比较
For loop variants comparison
有一个for
循环,如下所示:
uint32_t WORD_COUNT = // ... a constant number
using Word = uint64_t;
Word mWords[WORD_COUNT];
uint32_t n = WORD_COUNT;
for (Word* w = mWords; n--; ++w) *w = Word(0); // <= Original loop
原始循环可以这样写吗?
for (uint32_t i = 0; i < n; ++i) mWords[i] = Word(0); // => Is this the same loop?
用原来的形状写循环有什么好处吗?
如果循环用于初始化该数组,则只需编写
uint32_t WORD_COUNT = // ... a constant number
using Word = uint64_t;
Word mWords[WORD_COUNT] {};
// ^^
// See e.g. https://en.cppreference.com/w/cpp/language/aggregate_initialization
否则,正如约翰提到的
您也可以使用for范围循环
for (auto& w : mWords) w = Word(0);
或std::fill_n(mWords, n, Word(0));
我看不出像第一个发布的片段那样编写循环有任何好处。
相关文章:
- 对于循环变体比较
- 比较两个字符串后卡在无限循环中
- string1 == string2 和你自己的 for 循环比较有什么区别?
- 循环比较(优化)
- 比较嵌套 for 循环中的两个 wchar 数组?
- 使用循环进行词典字符串比较
- 如何使用 for 循环来比较不同大小的数组
- 包含比较的循环的自动矢量化
- 在 while 循环中使用 Module32Next() 时比较wchar_t
- C++和Java的字符串循环性能比较
- 在循环C 时进行比较
- 符号和未签名整数表达式之间的比较,以进行循环误差
- For 循环仅比较数组的第一部分而不是整个数组
- r语言 - C++ 或 Rcpp:两个不带循环的向量的比较
- C++:循环浏览地图并比较键
- 如何准确测量和比较opencl速度以实现循环功能的简单速度
- 比较二维数组中的数字时的无限循环
- C++ 比较 2 个不同列表中的值以使用 2 for 循环摆脱重复的数字。无法正确检测重复项
- C++ 中 for 循环中的比较运算符
- 比较循环的不同类型的C++