我假设STL函数只能用于STL数据容器(比如vector),直到我看到这段代码:
#include <functional>
#include <iostream>
#include <numeric>
using namespace std;
int main()
{
int a[] = {9, 8, 7};
cerr << "Sum: " << accumulate(&a[0], &a[3], 0, plus<int>()) << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它编译并运行时没有任何警告或g ++错误,给出正确的输出和24.
是否允许 C++/STL 标准允许使用具有STL功能的数组?如果是,那么像数组这样的古老结构如何适应模板化迭代器,容器和函数的宏STL计划?此外,程序员应该注意这些用法中是否有任何警告或细节?