WITH VTE AS(
SELECT V.customer_id,
CONVERT(date,V.purchase_date,101) AS purchase_date,
V.category
FROM (VALUES(1,'9/1/2018 ','Electronics'),
(1,'9/2/2018 ','Art'),
(1,'9/3/2018 ','Books'),
(1,'9/5/2018 ','CPG'),
(2,'9/2/2018 ','Books'),
(2,'9/4/2018 ','Electronics'),
(2,'9/20/2018','CPG')) V(customer_id,purchase_date,category))
SELECT V2.category,
COUNT(DISTINCT V2.customer_id) AS DistinctCustomers
FROM VTE V1
CROSS APPLY (SELECT TOP 1
customer_id,
purchase_date,
category
FROM VTE ca
WHERE ca.customer_id = V1.customer_id
AND ca.purchase_date < V1.purchase_date
ORDER BY ca.purchase_date DESC) V2
WHERE V1.category = 'CPG'
GROUP BY V2.category;
和f1
之间有区别吗?
f2
在这两种情况下,lambda内的void foo(const std::string& test){
auto f1 = [&test](){
std::cout << test << std::endl;
};
f1();
auto f2 = [test](){
std::cout << test << std::endl;
};
f2();
}
int main()
{
std::string x = "x";
foo(x);
}
变量的类型看起来都是test
,但确实是一样的吗?
答案 0 :(得分:6)
f1和f2之间有区别吗?
是的
但是真的一样吗?
不。 f2
捕获std::string const
。
lambda捕获的类型推断与auto
声明的工作原理相同:
[&test](){} // a reference
[ test](){} // an object
auto &var = test; // a reference
auto var = test; // an object
std::string &var = test; // a reference
std::string var = test; // an object
template<class T> void foo1(T& var);
template<class T> void foo2(T var);
foo1(test); // a reference
foo2(test); // an object