c++11 - 如何在 C++ 中进行拳击?
问题描述
我有一个以下程序,它无法编译我相信这是因为它试图在 int 即原始类型上执行 begin()、end()。也许,我需要一个对象类型。我在这里正确吗?我该如何解决这个问题,我想在这里使用 lambda。
#include <iostream>
#include <algorithm>
using namespace std;
int main(void) {
int N, i;
cin >> N;
int numArray[N]; // Define an array of four integers
// Get inputs for the array elements
for (i=0;i<N; i++) {
cin >> numArray[i];
}
int sum = 0;
// Write here the logic to add these integers:
for_each(begin(numArray), end(numArray), [&](int n){ sum += n; });
cout << sum << endl; // Print the sum
return 0;
}
编译错误——
main.cpp:22: error: no matching function for call to 'begin(int [N])'
for_each(begin(numArray), end(numArray), [&](int n){ sum += n; });
^
解决方案
使用std::begin(numArray)
和std::end(numArray)
。
推荐阅读
- c++ - 向 GDI+ 位图添加一定百分比的噪点失败
- html - 根据浏览器的宽度大小,网格区域可能不适合
- c# - 由于代码中的格式错误,我无法使用复选框
- android - 屏幕关闭时前台应用程序被杀死
- r - 为什么我的 R Shiny 应用程序会不一致地抛出 MIME 错误?
- python-3.x - IndexError:使用 discord.py 查找提及时列表索引超出范围
- python - WSL2 Ubuntu 18.04 - 性能真的很奇怪
- c++ - 无法打破forloop
- openshift - 如何在 Tekton 中设置通用 webhook 触发器
- javascript - 在事件监听器之后反应组件渲染