我见过其他人的问题,但没有发现适用于我在这里尝试实现的目标。
我正在尝试使用 std::sort 和一个通过我的 EntityManager 类对实体进行排序 std::vector<Entity *>
/*Entity.h*/
class Entity
{
public:
float x,y;
};
struct compareByX{
bool operator()(const GameEntity &a, const GameEntity &b)
{
return (a.x < b.x);
}
};
/*Class EntityManager that uses Entitiy*/
typedef std::vector<Entity *> ENTITY_VECTOR; //Entity reference vector
class EntityManager: public Entity
{
private:
ENTITY_VECTOR managedEntities;
public:
void sortEntitiesX();
};
void EntityManager::sortEntitiesX()
{
/*perform sorting of the entitiesList by their X value*/
compareByX comparer;
std::sort(entityList.begin(), entityList.end(), comparer);
}
Run Code Online (Sandbox Code Playgroud)
我收到了十几个错误,比如
: error: no match for …Run Code Online (Sandbox Code Playgroud) 我在一个非常简单的代码上得到了一个非常奇怪的错误,我无法修复.
我已经定义了以下函数对象:
template<const size_t n> class L2Norm {
public:
double operator()(const Point<n>& p) {
/* computes the L2-norm of the point P ... */
}
double operator()(const Point<n>& p,
const Point<n>& q) {
return L2Norm<n>(p-q);
}
};
Run Code Online (Sandbox Code Playgroud)
这里的类Point<n>很好地定义了存储n点在一n维空间中的坐标(带有所需的运算符,......).
我希望得到一个点的l2范数p(Point<5> p例如创建)L2Norm<5>(p).但这给了我以下错误:
no matching function for call to ‘L2Norm<5ul>::L2Norm(Point<5ul>&)’
note: candidates are: L2Norm<n>::L2Norm() [with long unsigned int n = 5ul]
note: candidate expects 0 arguments, 1 provided
note: L2Norm<5ul>::L2Norm(const L2Norm<5ul>&) …Run Code Online (Sandbox Code Playgroud) 在N3421中 - 使运算符函数更大<>,std函数对象的新特化是:
template <> struct plus<void> {
template <class T, class U> auto operator()(T&& t, U&& u) const
-> decltype(std::forward<T>(t) + std::forward<U>(u));
};
Run Code Online (Sandbox Code Playgroud)
代替
template <> struct plus<void> {
template <class T, class U> auto operator()(T&& t, U&& u) const
noexcept(noexcept(decltype(std::forward<T>(t) + std::forward<U>(u))
(std::move(std::forward<T>(t) + std::forward<U>(u)))))
-> decltype(std::forward<T>(t) + std::forward<U>(u));
};
Run Code Online (Sandbox Code Playgroud)
noexcept在这个用例中是否遗漏了问题?编辑:链接到github中的工作草稿行.
我有一个具有独立功能的C++程序.
由于大多数团队对面向对象的设计和编程缺乏经验或知识,我需要避免使用函数对象.
我想将一个函数传递给另一个函数,比如for_each函数.通常,我会使用函数指针作为参数:
typedef void (*P_String_Processor)(const std::string& text);
void For_Each_String_In_Table(P_String_Processor p_string_function)
{
for (unsigned int i = 0; i < table_size; ++i)
{
p_string_function(table[i].text);
}
}
Run Code Online (Sandbox Code Playgroud)
我想删除指针,因为它们可以指向任何地方,并包含无效的内容.
是否有一种通过引用传递函数的方法,类似于通过指针传递,而不使用函数对象?
例:
// Declare a reference to a function taking a string as an argument.
typedef void (??????);
void For_Each_String_In_Table(/* reference to function type */ string_function);
Run Code Online (Sandbox Code Playgroud) 我有一个类应该根据用户提供的谓词过滤其内容。我给出的接口规定引用谓词:
\n\nclass Test {\n vector<int> data;\npublic:\n template <class PREDTYPE>\n void filter(PREDTYPE& pred) {\n return;\n }\n};\nRun Code Online (Sandbox Code Playgroud)\n\n我还得到了一段测试代码,大致如下:
\n\nclass Filter {\npublic:\n bool operator()(int) const {\n return false;\n }\n};\n\nint main() {\n Test test;\n test.filter(Filter());\n}\nRun Code Online (Sandbox Code Playgroud)\n\n这不会\xe2\x80\x99t 编译,说cannot bind non-const lvalue reference of type 'Filter&' to an rvalue of type 'Filter'。如果我将测试代码更改为
int main() {\n Test test;\n Filter filter;\n test.filter(filter);\n}\nRun Code Online (Sandbox Code Playgroud)\n\n它有效,但这取决于最终用户,我无法控制他们的行为。我尝试重载过滤器方法,创建一个版本,该版本将按值接受谓词,然后通过引用传递它,但这也不会\xe2\x80\x99t编译,并显示消息call of overloaded 'filter(Filter&)' is ambiguous。
因此我的问题是:是否可以构造一个同时接受谓词的右值和左值的过滤器?
\n我想将一个函数对象传递给一个类,该类将使用函数对象在类中做一些工作.
但问题是,我没有传递函数对象的内容.所以我想,void *在类中定义一个指针,这个指针将使用将被传入的函数对象进行初始化.
代码如下:
class A
{
public:
//...
void doSomeJob(int x)
{
(*functor)(x); //use the function object to process data x
}
private:
//...
void *functor; //function object's pointer will be assigned to this pointer
};
Run Code Online (Sandbox Code Playgroud)
但代码不起作用.我想,不能用void *functor那种方式.
我知道我可以template class用来完成这项工作,但我的问题是,我还能继续使用这项工作pointer to function object吗?
PS
为了使我的问题更清楚,可能有几个函数对象通过它们如何处理数据而彼此不同,我不会传递什么函数对象,但我知道它们中的每一个都将采用一个int参数.
就像一些答案所说的那样,我可以完成这项工作function pointer,但是函数对象比函数指针有更多的实用工具,比如states,这就是我要用的东西.
在c ++标准库中,几乎所有算法函数都将可调用对象作为参数.现在我想用我的程序尝试这个东西.我开了头的功能类似find_if或search_n(),但不能太了解如何把这些调用对象的参数进行处理和关闭过程参数是如何传递给它们特别适用于拉姆达对象(可bind()用于lambda表达式,我不知道)
谁能解释我这件事是如何运作的.提前致谢
我对如何在C ++中调用函子的语法有疑问。在下面的代码中,为什么前两个示例有效,而第三次和第四次尝试却无效?
Also I can call ob (100); and it will output 300, but why can't I call Class(100) and have it output 300? Is there any way to use the overloaded operator () without instantiating a new object?
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
struct Class
{
void operator() (int a)
{
cout << a * 3 << " ";
}
} ob;
int main()
{
int arr[5] = { 1, 5, …Run Code Online (Sandbox Code Playgroud) 这是我在一本书上看到的一行代码
priority_queue<IteratorCurrentAndEnd, vector<IteratorCurrentAndEnd>, greater<>> min_heap;
Run Code Online (Sandbox Code Playgroud)
其中IteratorCurrentAndEnd是实现方法的类operator>。为什么我们可以用greater<>来代替greater<IteratorCurrentAndEnd>?我检查并读到了这样的东西,
template< class T = void >
struct greater;
Run Code Online (Sandbox Code Playgroud)
但我实在不知道这意味着什么。和类型有关系吗void?它到底是什么?
谢谢。
我的目的是为N任何std::get可以作为参数的第 th 元素编写一个 getter,一旦它被特定的N.
换句话说,std::get<N>不能被传递,因为它不是一个函数对象,而是一个基于它所采用的参数类型模板化的函数。
是否有可能定义一个对象getNth,从而std::invoke(getNth<1>, whatever);有道理,实际上是相当于getNth<1>(whatever)?
我能得到的最多的工作是std::invoke(getNth<1>(), whatever);,通过这样定义getNth:
template<int N>
auto getNth() {
return [](auto const& x){ return std::get<N>(x); };
};
Run Code Online (Sandbox Code Playgroud)
是否可以避免使用两个括号?
我认为模板化的 lambdas 可能有用,但它们不能,因为它们是具有模板化的类,operator()就像通用的非模板化 lambdas 一样(唯一的区别在于前者我们可以让参数的类型成为模板参数)。
相反,为了std::invoke(getNth<1>, whatever);有意义,我认为getNth不应该是模板函数,也不应该是模板类,而是其他东西......在结束后不带括号>,就像变量 templates发生的那样。
但是,根据本页顶部的要点,我可能正在寻找语法根本不提供的内容。是这种情况吗?