我试图捕获thislambda 函数中的指针,该函数用作方法的默认参数。
我的目标是从 lambda 中调用此类的方法,这需要捕获指针this。
但是,以下代码会导致错误:
错误 C3639:作为默认参数一部分的 lambda 只能有一个 init-capture
克服此限制的常见解决方案是什么?
#include <functional>
class A{
public:
void B(std::function<void()> a = [this](){}); //error C3639: a lambda that is part of a default argument can only have an init-capture.
};
Run Code Online (Sandbox Code Playgroud) 我想以排序的方式循环遍历向量而不修改底层向量。
可以std::views和/或std::range用于此目的吗?
我已经使用 成功实现了过滤views,但我不知道是否可以使用谓词进行排序。
您可以在此处找到要完成的示例:https ://godbolt.org/z/cKer8frvq
#include <iostream>
#include <ranges>
#include <vector>
#include <chrono>
struct Data{
int a;
};
int main() {
std::vector<Data> vec = {{1}, {2}, {3}, {10}, {5}, {6}};
auto sortedView = // <= can we use std::views here ?
for (const auto &sortedData: sortedView) std::cout << std::to_string(sortedData.a) << std::endl; // 1 2 3 5 6 10
for (const auto &data: vec) std::cout << std::to_string(data.a) << std::endl; // 1 …Run Code Online (Sandbox Code Playgroud)