bool comp(const pair<int, int>& a, const pair<int,int>& b){
if (v[a.first]>v[b.first]) {
return true;
}
else if(v[a.first] == v[b.first] && a.second < b.second){
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
所以,我正在通过一个代码,我遇到了这个比较器函数,用于排序对的向量.现在,我对C++很新.我试着阅读关于这个比较器如何工作的其他问题?但我无法理解.为什么返回类型是bool?返回值true意味着什么?
我想std::find在一个shared_ptr抽象类的列表上使用,但我收到一个错误.有没有办法shared_ptr通过解除引用来比较两个std::find?
是否有可能让一个operator==超载的朋友shared_ptr<A>?
最小的例子:
#include "point.h"
#include <list>
#include <algorithm>
#include <memory>
using namespace std;
class A {
protected:
Point loc;
public:
virtual void foo() = 0;
virtual bool operator==(const Point& rhs) const = 0;
};
class B: public A {
virtual void foo() override{}
virtual bool operator==(const Point& rhs) const override {
return rhs == loc;
}
};
class C {
list<shared_ptr<A>> l;
void bar(Point & p) { …Run Code Online (Sandbox Code Playgroud) 我在某个库中有这个功能:
class myConsole
{
void addCommand( std::string command, void* fn );
...
}
Run Code Online (Sandbox Code Playgroud)
在我班上我有这个功能:
void myApp::TestFn( const std::vector<std::string> & args )
{
// do something
}
Run Code Online (Sandbox Code Playgroud)
在同一个班级我称之为:
void myApp::initApp( )
{
myConsole::getSingleton( ).addCommand( "FirstTest", &myApp::TestFn );
}
Run Code Online (Sandbox Code Playgroud)
但这给了我这个错误:
错误c2664无法将参数2从'void(__ thiscall myApp ::*)(const std :: vector <_Ty>&)'转换为'void*'
我该怎么解决这个问题?
提前致谢!
我试图找到模板函数:
template <typename T>
T add(T lhs, T rhs) {
return lhs + rhs;
}
Run Code Online (Sandbox Code Playgroud)
(用于加,减,乘和除).
我记得有一套标准的功能 - 你还记得它们是什么吗?
我想传递一个函数作为参数.我知道你可以传递一个函数指针,就像我的例子中的第一个测试一样,但是可以像我的第二次测试一样传递一个hold函数(不是指针)吗?
#include <iostream>
using namespace std;
/* variable for function pointer */
void (*func)(int);
/* default output function */
void my_default(int x) {
cout << "x =" << "\t" << x << endl << endl;
}
/* entry */
int main() {
cout << "Test Programm\n\n";
/* 1. Test - default output function */
cout << "my_default\n";
func = &my_default; // WORK! OK!
func(5);
/* 2. Test - special output function 2 */
cout << "my_func2\n";
func = void my_func1(int …Run Code Online (Sandbox Code Playgroud) 我想在C中做这样的事情:
typedef int (*func)(int);
func make_adder(int a) {
int add(int x) {
return a + x;
}
return &add;
}
int main() {
func add_42 = make_adder(42);
// add_42(10) == 52
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用.它可行吗?我的错误在哪里?
我在python中编写了一个使用max()方法的脚本,我试图在c ++中重新创建一个类似的程序,但是我无法获得掩码中最大轮廓的值.
我试图在C++中使用算法库中的max_element()函数,但无济于事.我也尝试取消引用迭代器但收到一系列错误,这是我的代码:
if (contours.size() > 0)
{
c = *max_element(contours.begin(), contours.end());
//not compiling
}
Run Code Online (Sandbox Code Playgroud)
这是错误:
no match for 'operator=' (operand types are 'std::vector<std::vector<cv::Point_<int> > >' and 'std::vector<cv::Point_<int> >')
Run Code Online (Sandbox Code Playgroud)
以下是我在Python中的使用方法:
if len(contours) > 0;
#find largest contour in mask, use to compute minEnCircle
c = max(contours, key = cv2.contourArea)
(x,y), radius) = cv2.minEnclosingCircle(c)
M = cv2.moments(c)
Run Code Online (Sandbox Code Playgroud) 我希望有一个函数可以返回不带参数调用的函数,即使原始函数必须带参数调用也是如此。该参数将传递给第一个函数,并且每次调用返回的函数时都将隐式使用该参数。
我知道C中的函数指针,但不知道是否可以这种方式使用它们。我知道如何在python中执行此操作,如以下示例代码所示:
def foo(some_argument):
print(some_argument)
def register_callback(function, argument):
def new_function():
function(argument)
return new_function
bar = register_callback(foo,"bar")
bar()
Run Code Online (Sandbox Code Playgroud)
据我了解,在C语言中这种方法是不可能的,因为我们不能嵌套函数定义。这是可能的,如果可以的话,这样做的正确方法是什么?
我有一个函数,它将一个指向函数的参数作为参数:
void MySolver::set_pt2func(double(*pt2function)(double*))
{
pt2Function = pt2function;
}
Run Code Online (Sandbox Code Playgroud)
在另一个类中,某些方法想要使用以下代码初始化实例my_solver:
double (MyClass::*pt2apply)(double* parameter) = NULL;
pt2apply = &MyClass::apply_func;
my_solver->set_pt2func(pt2apply);
Run Code Online (Sandbox Code Playgroud)
最后一行显示编译错误:
error: double (MyClass::*pt2apply)(double*)
argument of type (MyClass::* )(double*) is not compatible
with argument of type double(*)(double*)
Run Code Online (Sandbox Code Playgroud)
怎么修?
谢谢!
编辑
我用这个小费
http://www.parashift.com/c++-faq/memfnptr-to-const-memfn.html
我设置了typedef:
typedef double (MyClass::*Pt2apply_func)(double*) const;
Run Code Online (Sandbox Code Playgroud)
在我的方法中,我试试这个:
Pt2apply_func pt2apply = &MyClass::apply_func;
my_solver->set_pt2func(pt2apply);
Run Code Online (Sandbox Code Playgroud)
和编译错误显示&MyClass,再次兼容类型.处理我的问题的其他方法?谢谢!
我已阅读以下问题:
我明白什么C++ functors是好的。但是我无法推断如果执行以下操作会发生什么:
template <typename T, unsigned int state>
class Foo {
public:
static Foo_func() { /* Do something */ };
}
// Partial specialization:
// -- state == 1
template <typename T>
class Foo <T, 1> {
public:
static Foo_func() { /* Do something */ };
}
template <typename F>
void call_func(F func) {
// Do something... //
func();
// Do something... //
}
int main() {
Foo <double, /*state*/ …Run Code Online (Sandbox Code Playgroud) c++ ×8
c ×2
function ×2
functor ×2
c++11 ×1
closures ×1
opencv ×1
pointers ×1
polymorphism ×1
python ×1
shared-ptr ×1
templates ×1
types ×1
visual-c++ ×1