当我尝试使用vector.size()时,它给出了以下错误:
In function 'int main()':
[Error] expected unqualified-id before '(' token
[Error] expected primary-expression before ')' token
[Error] lambda-expression in unevaluated context
[Error] expected identifier before numeric constant
In lambda function:
[Error] expected '{' before ')' token
Run Code Online (Sandbox Code Playgroud)
其余的向量函数都有效.我之前使用过这个功能而且它有效,现在我无法使用它.
这是代码:
while(1)
{
vector<string> frase;
string stringa;
getline(cin,stringa);
{
string temp;
temp.clear();
for(int i=0;i<stringa.length();i++)
{
if(stringa[i]!=' ')temp+=stringa[i];
else
{
frase.push_back(temp);
temp.clear();
}
}
frase.push_back(temp);
}
analizza(frase.data(), /*ERROR HERE*/ frase.size() /*ERROR HERE*/);
frase.clear();
for(int i=0;i<frase.size();i++)frase.pop_back();
}
Run Code Online (Sandbox Code Playgroud)
我能做什么?
以下是linq查询.在这里,我想添加一个条件.条件:如果:Firstname不为空,则选择list where(d => d.firstname =="Firstname") else:选择所有列表无条件
function Ponits(string Firstname)
{
pointsCore.Categories.SelectMany(c => c.Events).Select(e => new
{
e.Firstname,
e.Surname,
e.EntityNumber,
e.Eventdate
}).ToList()
}
Run Code Online (Sandbox Code Playgroud) 我有一个字典表示列表如下:
a = [{'score': 300, 'id': 3}, {'score': 253, 'id': 2}, {'score': 232, 'id': 1}]
Run Code Online (Sandbox Code Playgroud)
我是python的新手,我需要一个python lambda函数,可以通过输出:
dict = [{3:300}, {2:253}, {1:232}]
Run Code Online (Sandbox Code Playgroud)
这样我就可以找到给定值的值
>>> print dict[3]
>>> 300
Run Code Online (Sandbox Code Playgroud)
感谢您对此的帮助.
我有一段时间有这个想法,我很好奇是否有可能以有价值的方式实施.我想获取一个布尔返回的lambda表达式数组,并对其结果执行逻辑运算.这是一个有效列表的无意义的例子:
var tests = new List<Func<int, bool>>() {
(x) => x > 10,
(x) => x < 100,
(x) => x != 42
};
Run Code Online (Sandbox Code Playgroud)
我想做的主要是
bool result = tests.And();
Run Code Online (Sandbox Code Playgroud)
或执行其他逻辑操作.我意识到我可以写一个IEnumerable实现类并执行此操作,但我想知道它是否已经完成.显然,实现必须以同样的方式有效地工作,短路
if (truthyExpression || falseyExpression)
Run Code Online (Sandbox Code Playgroud)
永远不会开始评估falseyExpression.
我在框架中看到的唯一可能有用的是Bit Array,但是我不确定如何在不预先评估每个表达式的情况下使用它,从而破坏了短行程的有用性.
我正在阅读一些代码并看到以下内容:
Method.Find(delegate(Department depts) {
return depts.Id == _departmentId; });
Run Code Online (Sandbox Code Playgroud)
该T Find方法具有以下描述:
public T Find(Predicate<T> match);
// Summary:
// Searches for an element that matches the conditions defined by the specified
// predicate, and returns the first occurrence within the entire
// System.Collections.Generic.List<T>.
//
// Parameters:
// match:
// The System.Predicate<T> delegate that defines the conditions of the element
// to search for.
// (...)
Run Code Online (Sandbox Code Playgroud)
是否有可能重写此方法以将lambda表达式作为参数,如果是,如何?
这就是我的意思.
我有一个像这样的构造函数的类:
Class() : base(GetConstructorArgument1(), GetConstructorArgument2())
{
}
Run Code Online (Sandbox Code Playgroud)
因为尽可能在基数调用中创建参数会非常麻烦和粗糙.必要时,这两种GetConstructorArgument方法都是静态的.
GetConstructorArgument2()创建一个lambdas数组.在那些lambdas中,我想做"这个",我正在构建的对象.
有没有办法做到这一点?
我想创建一个接受函数指针和lambda表达式的函数.函数的返回类型应与函数指针/ lambda表达式的返回类型相同.
以下是一个函数的最小示例,该函数按预期工作,但仅适用于函数指针.我也可以使用模板来接受lambda表达式吗?
template <typename R>
R foo(R (*func)())
{
return func();
}
Run Code Online (Sandbox Code Playgroud)
以下适用于函数指针和lambda表达式,但只接受bool.
bool foo(std::function<bool()> func)
{
return func();
}
bool a = foo( [](){ return true; } );
Run Code Online (Sandbox Code Playgroud)
我尝试使用模板使它变得通用,但是当它被调用时我得到一个编译器错误(没有匹配的函数)
template <typename R>
R foo(std::function<R()> func)
{
return func();
}
bool a = foo( [](){ return true; } );
Run Code Online (Sandbox Code Playgroud) 我想处理2个以某种方式相关的容器,我想彼此明智地处理它们.
简单示例:一个容器包含某种总和,另一个长度相等的容器包含必须从该总和中减去的数字.
std::vector<int> s = {20,56,7,34,234,23,54};
std::vector<int> m = {1,3,5,2,3,5,6};
for(int i=0; i<s.size();++i){
s[i] -= m[i];
}
Run Code Online (Sandbox Code Playgroud)
是否有一种更优雅的方式来实现这一点,例如使用for_each?
如何将以下内容转换为Lambda?
Func<int, string> calcState = delegate(int test)
{
return (!MyList.All(i => i > test) ?
(MyList.Any(i => i > test) ? "ein Paar" : "Keiner") : "alle");
};
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
#include <functional>
//...
typedef int (*Myfun)(int);
std::function<int (int)> fn0([](int a)->int {
return -a;
});
std::cout << "val == " << fn0(3) << std::endl; //"val == -3"
Myfun *fptr = fn0.target<Myfun>(); //fptr is NULL!!
std::cout << "val == " << (*fptr)(3) << std::endl; //Access violation error
Run Code Online (Sandbox Code Playgroud)
实际上,这段代码是来自MSDN的代码,稍有改动:使用lambda而不是普通函数.
为什么调用fn0.target<Myfun>()返回NULL?
当我宣布一个常规功能
int neg(int val) {
return (-val);
}
Run Code Online (Sandbox Code Playgroud)
并写std::function<int (int)> fn0(neg);,一切似乎都有效,但lambda没有得到正确处理.