我知道我可以使用我的定义函数对数组进行排序,如下所示:
bool cmp(int a, int b){
return a > b;
}
class SortTest{
public :
void testSort(){
int a[] = { 1, 3, 4, 7, 0 };
int n = 5;
sort(a, a + n, cmp); //work
for (int i = 0; i < n; i++){
cout << a[i]
我有一个EnemyRhombus课程。它是一个单位,可以移动到地图上的不同点。
我想处理点,它可以移动,以增加与他们的距离。
为了做到这一点,我想对要点进行排序。
代码1:
class EnemyRhombus
{
public:
int x,y;
int dist(Point p)
{
int dx=abs(p.x-x);
int dy=abs(p.y-y);
return dx+dy-min(dx,dy);
}
bool points_cmp(Point f, Point s)
{
我创建了一个名为MyClass的类,其中一个成员函数使用C++ sort()函数,并将另一个成员函数cmp作为参数传递给它。 #include <algorithm>
using namespace std;
/* If I use this cmp for sort(), there is no error.
bool cmp(int& x, int& y) {
return x < y;
}
*/
class MyClass {
public:
/* Or I use this cmp, there is no error too.
我希望从另一个类中传递一个函数作为当前类中的参数。我正在尝试这样做(我将代码标准化,这样您仍然可以得到这样的想法):
B class:
bool B::myCmpFunc(int a, int b) {
return a > b;
}
vector<int> B::sort(bool (*cmp)(int a, int b)) {
vector<int> elems = getElems();
for (int i = 0; i < elems.size() - 1; i++) {
for (int j = i +
我想在特定的类中对某些结构的向量进行排序,有一定的顺序。我在类中编写了struct和谓词函数的定义,并使用这些结构和函数在类的方法中运行std::sort。但编译错误已经发生。gcc版本为4.0.1,操作系统为Mac。守则如下:
class sample {
public:
struct s {
int x;
int y;
};
bool cmp (struct s a, struct s b) {
if (a.x == b.x)
return a.y < b.y;
else
return a.x < b.x;
当我从下面的代码调用我的build函数时,我会得到以下编译错误:
error C3867: 'SuffixArray::cmp': function call missing argument list; use '&SuffixArray::cmp' to create a pointer to member
error C2780: 'void std::sort(_RanIt,_RanIt)' : expects 2 arguments - 3 provided
这是我的密码:
class A{
private:
std::
编写一个简单的模板函数示例。代码按照预期进行编译和工作。但我的问题是,为什么"Cmp“和"Lit”都需要"static“?否则,它不会编译吗?
非常感谢!
template<class T> class Cmp{
public:
static int work(T a, T b) {
std::cout << "Cmp\n";
return 0;
}
};
template<class T> class Lit{
public:
static int work(
我应该如何在结构体中传递一个函数作为函数器?我认为这应该可以很好地工作,但它并没有:
#include <algorithm>
using namespace std;
struct s {
int a[10];
bool cmp(int i, int j) {
// return something
}
void init() {
sort(a, a + 10, cmp);
}
};
它得到了<unresolved overloaded function type>
我正在做leetcode 406。按高度进行队列重构。我想首先对vector>进行排序,但当我完成排序和cmp部分并运行代码时,它给出了编译错误。
solution.cpp: In member function reconstructQueue
Line 12: Char 47: error: invalid use of non-static member function 'bool Solution::cmp(std::pair<int, int>, std::pair<int, int>)'
sort(people.be
这是相当标准的,工作良好:
#include <set>
auto cmp = [](int a, int b) { return a > b; };
using stype = std::set<int, decltype(cmp)>;
stype mySet(cmp);
但为什么我不能这么做?
#include <set>
auto cmp = [](int a, int b) { return a > b; };
using stype = std::set<int, decltype(cmp)>;
struct foo
我在C++中的专用函数模板有问题。我正在编写一个比较函数模板,它将用于对不同数据类型进行排序。下面是我代码的一部分:
template < typename T >
bool cmp(T a, T b)
{
std::cout<<"normal"<<std::endl;
return(a >= b);
}
template < typename T >
bool cmp(T* a, T* b)
{
std::cout<<"pointer"<<std::endl
我试图根据cakeTypes向量的重量大小对它们进行排序。但得到排序实现中的错误。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class CakeType
{
public:
const unsigned int weight_;
const unsigned int value_;
CakeType(unsigned int weight = 0, unsigned int value = 0) :
我只是有一个非常快速的问题,因为我试图掌握C++类的概念。为什么我们不使用单个成员函数来设置和获取数据成员值,而不是单独的访问器/getter和mutator/setter函数?
示例如下:
class Student
{
private:
int rollno;
public:
int setGetRollNo(int rn) {
rollno = rn;
return rollno;
}
};
int ma
当我尝试用我的自定义比较器对我的成分进行排序时,我得到了这个编译器错误。
kitchen.cpp: In member function ‘void Kitchen::printContents(std::ofstream&)’:
kitchen.cpp:172: error: no matching function for call to ‘std::list<Ingredient, std::allocator<Ingredient> >::sort(<unresolved overloaded function type>)’
/usr/in
我认为C++规范说在引用函数时,函数前面的符号是不必要的,即
void bar();
void foo(void(*bar)());
foo(bar);
foo(&bar); // Same as above.
然而,我发现了一个不正确的例子。我试图对lambda进行模板专门化(仅针对单个参数),以便能够访问lambda的返回参数和输入参数的类型。
// The ampersand in front of 'Fn::operator()' is necessary to make
// this code work.
template <typename La