如何使用std:fill为C数组的特定范围设置值

yek*_*chi 2 c++ c++11

如何仅为某些数组的特定范围设置值,而不是从零开始.以下代码调用编译错误:

#include <algorithm>;

bool SomeBoolArray[100];
std::fill(SomeBoolArray[50], SomeBoolArray[50] + 10, true);
Run Code Online (Sandbox Code Playgroud)

以下表单也会调用编译器错误.

std::fill(SomeBoolArray[50], SomeBoolArray[60], true);
Run Code Online (Sandbox Code Playgroud)

Mat*_*her 5

std::fill 需要迭代器,或迭代器喜欢,如指针:

std::fill(SomeBoolArray + 50, SomeBoolArray + 60, true);
Run Code Online (Sandbox Code Playgroud)

  • OP也可以把它写成`std :: fill(&SomeBoolArray [50],&SomeBoolArray [50] + 10,true);` (6认同)