英文:
Lambda expression:term does not evaluate to a function taking 1 arguments
问题
我遇到了这个错误:错误 C2064:term does not evaluate to a function taking 1 arguments。
#include <iterator>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
vector<int> ivec{ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 };
vector<int>::const_reverse_iterator it = ivec.crbegin(), rend = ivec.crend();
for_each(it, rend, [it]()->void {cout << *it << " "; });
cout << endl;
return 0;
}
这段代码有什么问题?有人可以告诉我问题出在哪吗?
英文:
I got this error:error C2064: term does not evaluate to a function taking 1 arguments
#include <iterator>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
vector<int> ivec{ 1,1,2,3,5,8,13,21,34,55,89 };
vector<int>::const_reverse_iterator it = ivec.crbegin(), rend = ivec.crend();
for_each(it, rend, [it]()->void {cout << *it << " "; });
cout << endl;
return 0;
}
What wrong with these code?Can someone tell me where the problem is?
答案1
得分: 0
for_each 会将 *it 作为引用传递给 lambda 函数,您无需捕获迭代器并解引用它。以下代码展示了这一点:
#include <iterator>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
vector<int> ivec{ 1,1,2,3,5,8,13,21,34,55,89 };
vector<int>::const_reverse_iterator it = ivec.crbegin(), rend = ivec.crend();
for_each(it, rend, [](const int &i)->void {cout << i << " "; });
cout << endl;
return 0;
}
该代码的输出如预期的那样,是反转后的向量:
89 55 34 21 13 8 5 3 2 1 1
对于我们中的语言专家,这在 ISO 标准中得到了覆盖(我正在看的是 C++20)。请特别参考注释 2(我的强调部分),其中以迂回的方式说明迭代器被解引用并将该值传递给函数:
23.5.4 For each [alg.foreach]
template<class InputIterator, class Function>
constexpr Function for_each(
InputIterator first,
InputIterator last,
Function f);
- 要求:
Function必须满足Cpp17MoveConstructible要求(表 25)。[注意:Function不必满足Cpp17CopyConstructible的要求(表 26)。— 结束注释] - 效果:将
f应用于范围[first, last)中每个迭代器的解引用结果,从first开始,到last - 1结束。[注意:如果first的类型满足可变迭代器的要求,f可能通过解引用的迭代器应用非常数函数。— 结束注释] - 返回:
f。 - 复杂度:
f正好应用last - first次。 - 备注:如果
f返回结果,结果将被忽略。
英文:
The for_each will pass *it to the lambda function as a reference, you do not need to capture the iterator and dereference it. The following code shows this in action:
#include <iterator>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
vector<int> ivec{ 1,1,2,3,5,8,13,21,34,55,89 };
vector<int>::const_reverse_iterator it = ivec.crbegin(), rend = ivec.crend();
for_each(it, rend, [](const int &i)->void {cout << i << " "; });
cout << endl;
return 0;
}
The output of that is, as expected, the reversed vector:
89 55 34 21 13 8 5 3 2 1 1
For the language lawyers amongst us, this is covered in [alg.foreach] in the ISO standard (C++20 is the one I'm looking at). See in particular note 2 (my emphasis) where it states, in a roundabout way, that the iterator is de-referenced and that value is passed to the function:
>23.5.4 For each [alg.foreach]
>
>template<class InputIterator, class Function><br>
>constexpr Function for_each(<br>
> InputIterator first,<br>
> InputIterator last,<br>
> Function f);
>
> 1. Requires: Function shall satisfy the Cpp17MoveConstructible requirements (Table 25). [Note: Function need not meet the requirements of Cpp17CopyConstructible (Table 26). — end note]
> 2. Effects: Applies f to the result of dereferencing every iterator in the range [first, last), starting from first and proceeding to last - 1. [Note: If the type of first satisfies the requirements of a mutable iterator, f may apply non-constant functions through the dereferenced iterator. — end note]
> 3. Returns: f.
> 4. Complexity: Applies f exactly last - first times.
> 5. Remarks: If f returns a result, the result is ignored
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论