相关疑难解决方法(0)

为什么我不能用C++映射结构?

我已经宣布了这样的结构 - >

struct data{
    int x,y;
    bool operator < (const data& other) {
        return x<other.x or y<other.y;
    }
};
Run Code Online (Sandbox Code Playgroud)

现在我希望map它作为一个关键并具有bool价值.

int main()
{
    data a;
    map<data,bool>mp;
    a.x=12, a.y=24;
    mp[a]=true;
}
Run Code Online (Sandbox Code Playgroud)

最后一行给了我这个错误 - >

error: passing 'const' as 'this' argument of 'bool data::operator<(const data&)' discards qualifiers
Run Code Online (Sandbox Code Playgroud)

我怎样才能解决这个问题 ??

c++ mapping structure

4
推荐指数
1
解决办法
169
查看次数

由于比较功能导致的分段错误

我尝试在 spoj 上解决这个问题。http://www.spoj.com/problems/BUSYMAN/

虽然我能够解决它,但我遇到了一个非常奇怪的错误。我试图了解它的原因,但失败了。我有两个代码。

///////////////////////////////////////////////// /

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

class activity
{
    public:
    int start,end;
};

bool comp(activity p, activity q)
{
    if(p.end<q.end)return true;
    if(p.end==q.end&&p.start<=q.start)return true;
    return false;
}

int main()
{
    int t;
    cin>>t;
    vector<activity> v;
    for(int i=0;i<t;i++)
    {
        int n;
        cin>>n;

        v.resize(n);
        for(int j=0;j<n;j++)cin>>v[j].start>>v[j].end;
        sort(v.begin(),v.end(),comp);
        int ans=0,currend=0;
        for(int j=0;j<n;j++)
        {
            if(v[j].start>=currend){ans++;currend=v[j].end;
        }

    }
    cout<<ans<<endl;
    }
}
Run Code Online (Sandbox Code Playgroud)

/////////////////////////////////////////////

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

class activity
{
    public:
    int start,end;
};

bool comp(activity p, activity …
Run Code Online (Sandbox Code Playgroud)

c++ segmentation-fault

3
推荐指数
1
解决办法
1292
查看次数

std:map如何检查两个对象是否相等?

为什么打印下面的代码1,即使我在地图中插入了两个元素?

#include <iostream>
#include <map>
#include <string>
#include <utility>

struct Foo
{
  Foo(int bar, const std::string& baz)
    : bar(bar)
    , baz(baz)
  {}

  int bar;
  std::string baz;

  bool operator<(const Foo& rhs) const
  {
    if (bar < rhs.bar && baz < rhs.baz)
    {
      return true;
    }
    else
    {
      return false;
    }
  }
};

int main()
{
    Foo first(0, "test");
    Foo second(1, "test");
    std::map<Foo, std::string> m;
    m.insert(std::make_pair(first, "test"));
    m.insert(std::make_pair(second, "test1"));
    std::cout << m.size() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

第二个电话insert()说我们已经在地图中有该项目.为什么?

由于输入错误,我之前的问题 …

c++ stdmap

2
推荐指数
1
解决办法
237
查看次数

C++ STL设置lower_bound错误结果

我对lower_bound比较函数有一些问题.

我有一对由该对的第二个值排序的对,我试着通过一个值从这个集合中获取lower_bound.

我目前的代码是:

#include <iostream>
#include <algorithm>
#include <vector>
#include <set>

using namespace std;

struct setCompareFunctor
{
    bool operator( )( const pair< int, int > &lhs, const pair< int, int > &rhs ) const
    {
        return( lhs.second <= rhs.second );
    }
};

struct setCompareFunctorAux
{
    bool operator( )( const pair< int, int > &lhs, const pair< int, int > &rhs ) const
    {
        return( lhs.second <= rhs.second );
    }

    bool operator( )( const pair< int, int > &lhs, int val …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm stl function lower-bound

2
推荐指数
1
解决办法
726
查看次数

std::sort 崩溃 - 没有严格弱排序的排序

我正在尝试对项目向量进行排序。正如代码注释中提到的,顺序应该是:

行动分较多(mAp)的参与者先行。当平局时,mDisposition与战斗发起者 ( )性格相同 ( ) 的参与者mBattleInitiator先行。

以下代码(简化示例)在 macOS 上崩溃,可能是由于我的排序实现不正确:

#include <QtCore>

class AiComponent
{
public:
    enum Disposition {
        Friendly,
        Hostile
    };

    AiComponent(Disposition disposition) : mDisposition(disposition) {}
    ~AiComponent() { qDebug() << "Destroying AiComponent"; }

    Disposition mDisposition;
};

class BattleManager
{
public:
    BattleManager() : mBattleInitiator(AiComponent::Hostile) {}

    class Turn {
    public:
        Turn() : mAp(1) {}

        Turn(QSharedPointer<AiComponent> aiComponent) :
            mAiComponent(aiComponent),
            mAp(1)
        {
        }

        Turn(const Turn &rhs) :
            mAiComponent(rhs.mAiComponent),
            mAp(1)
        {
        }

        QSharedPointer<AiComponent> mAiComponent;
        int mAp;
    };

    void …
Run Code Online (Sandbox Code Playgroud)

c++ sorting strict-weak-ordering

2
推荐指数
1
解决办法
1756
查看次数

如何在c ++中定义cmp?用<或用<=?

我问我如何定义std :: sort和std :: is_sorted中的cmp函数.

这里有两个is_sorted_until文档怎么说它应该是operator <:

en.cppreference.com cplusplus.com

但我认为应该存在相同元素的问题.列表{1,1,1}不应该排序,因为1 <1 == false.但有一个例子说:

...
int *sorted_end = std::is_sorted_until(nums, nums + N);
...
Run Code Online (Sandbox Code Playgroud)

1 1 4 9 5 3:4个初始排序元素

但如果<使用<记录,那么应该返回1.

它适用于<=,但这不是它的记录方式.

我真的很困惑.

c++ sorting stl compare

1
推荐指数
1
解决办法
1886
查看次数

具有个人比较功能的std :: set具有相同的值

我想存储std::setPoint3D对象,我比较函数的定义如下(按字典顺序):

bool operator<(const Point3D &Pt1, const Point3D &Pt2)
{
    const double tol = 1e-5;

    if(fabs(Pt1.x() - Pt2.x()) > tol)
    {
        return Pt1.x() < Pt2.x();
    }    
    else if(fabs(Pt1.y() - Pt2.y()) > tol)
    {
        return Pt1.y() < Pt2.y();
    }
    else if(fabs(Pt1.z() - Pt2.z()) > tol)
    {
        return Pt1.z() < Pt2.z();
    }
    else
    {
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

在某些情况下set包含相同的点,我认为问题来自比较函数,但我找不到确切的问题.任何帮助,将不胜感激!

c++ stdset

1
推荐指数
1
解决办法
166
查看次数

std::sort - 给定一个自定义排序函数,一次比较中是否可以进行多个排序查询?

给定一个std::vector对象Custom,我想根据多个查询对该列表进行排序(我首先对 on 进行排序value1,然后对 on 进行排序value2),因此:

bool customSort(const Custom &a, const Custom &b)
{
    return value1 < b.value1 && a.value2 < b.value2;
}

std::vector<Custom> elements;
std::sort(elements.begin(), elements.end(), customSort);
Run Code Online (Sandbox Code Playgroud)

不幸的是,这不起作用(因为它会发生customSort(a,b)并且customSort(b,a)equal true),因此我必须像这样排序(以相反的顺序):

bool customSortValue1(const Custom &a, const Custom &b)
{
    return value1 < b.value1;
}

bool customSortValue2(const Custom &a, const Custom &b)
{
    return value2 < b.value2;
}


std::vector<Custom> elements;
// note that I first sort by customSortValue2 and than …
Run Code Online (Sandbox Code Playgroud)

c++ sorting algorithm c++11

1
推荐指数
1
解决办法
345
查看次数

C++:“无效比较器”断言

这是代码:

struct Payment
{
    Payment(time_t time, float money) : mTime(time), mMoney(money) {}
    bool operator==(const Payment& p) const // exact comparison
    {
        return mTime == p.mTime && mMoney == p.mMoney;
    }
    time_t  mTime;
    float   mMoney;
};

std::vector<Payment>    payments;

auto sortP = [](const Payment& p1, const Payment& p2) { return p1.mTime < p2.mTime || p1.mMoney <= p2.mMoney; };
std::sort(payments.begin(), payments.end(), sortP);
Run Code Online (Sandbox Code Playgroud)

std::sort(并非总是如此,但有时,当mTime两个元素彼此靠近时)在 Visual Studio 2015 中引发无效比较器断言。代码有什么问题?
在此处输入图片说明

c++ stl c++11

1
推荐指数
1
解决办法
3771
查看次数

您可以使用自定义比较器将std :: map转换为无序映射吗?

由于使用了我不想编辑其代码的库,因此我发现自己需要使用std::map<Identifier, String>

struct compareIdentifiers
{
    bool operator()(const Identifier& a, const Identifier& b) const
    {
        // return a < b;
        return true;
    }
};

typedef std::map<Identifier, String, compareIdentifiers> IdentifierMap;
Run Code Online (Sandbox Code Playgroud)

我应该返回true还是false?无需进行比较。我想返回true或false会在效率上产生巨大的差异,因为一个会导致地图重新排序,而另一个不会...对吗?

我尝试使用std::unordered_map<Identifier, String>但出现错误:

错误C2280'std :: hash <_Kty> :: hash(void)':尝试引用已删除的函数

c++ containers stl

1
推荐指数
1
解决办法
118
查看次数