nodejs c++ 有变量stdvectorPersistentFunction怎么检查一个LocalFunction在上面的vector里面
可以使用std::find_if算法来查找vector中是否存在指定的Local
#include <algorithm> // for std::find_if
#include <node.h> // for Node.js C++ addon API
// ...
// Check if the given function exists in the vector.
bool hasFunction(std::vector<v8::Persistent<v8::Function>>& functions, v8::Local<v8::Function> function) {
auto it = std::find_if(functions.begin(), functions.end(), [&function](const v8::Persistent<v8::Function>& persistent) {
return persistent.Get(v8::Isolate::GetCurrent()) == function;
});
return it != functions.end();
}
// ...
// Usage:
std::vector<v8::Persistent<v8::Function>> functions;
// ...
v8::Local<v8::Function> myFunction = ...;
if (hasFunction(functions, myFunction)) {
// myFunction is in the vector.
} else {
// myFunction is not in the vector.
}
在上面的代码中,使用了lambda表达式来比较每个Persistent
原文地址: https://www.cveoy.top/t/topic/fILZ 著作权归作者所有。请勿转载和采集!