相关疑难解决方法(0)

您可以使用自定义比较器将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
查看次数

C++映射表现不正常

我已经定义了我的类SumClass并尝试在地图中使用它,如下面的代码所示.我已经定义了所需的<,===运算符.

#include <iostream>
#include <vector>
#include <map>

using namespace std;

class SumClass {
    public:
    int id;
    int sum;
    SumClass() { id = sum = 0;}
    bool operator<(const SumClass& rhs) const{
        if( (id < rhs.id) && (sum< rhs.sum)) return true;
        else return false;
    }
    bool operator==(const SumClass& rhs) const{
        //if(this == &rhs) return true;
        if( (id == rhs.id) && (sum == rhs.sum) ) return true;
        else return false;
    }
    void set(int idd, int summ) { …
Run Code Online (Sandbox Code Playgroud)

c++ stl map data-structures

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

如何更改pair(>和<)运算符的行为?

我正在尝试排序vector< pair<int,char> >但我想改变对类型的比较运算符的行为,这样如果第一个值相等并且它与(>)运算符进行比较,我希望它将第二个值与(<)运营商.

我正在努力解决" 什么是密码分析?" 关于uva的问题.这是我的方法:

string toLower(string in){
    string out;
    for(int i=0;i<in.length();i++){
        if(in.at(i)<='Z' && in.at(i)>='A'){
            out+=in.at(i)+('a'-'A');
        }
        else if(in.at(i)<='z' && in.at(i)>='a'){
            out+=in.at(i);
        }
    }
    return out;
}


int main(){
    //freopen("in.txt","r",stdin);
    //freopen("tmp.txt","w",stdout);
    vector< pair<int,char> >vp;
    pair<int,char> tp;

    for(char a='a';a<='z';a++){//buliding a table of values and chars
        tp= make_pair(0,a);
        vp.push_back(tp);
    }
    int T;
    cin >> T;
    string s;
    cin.ignore();
    for(int i=0;i<T;i++){
        getline(cin,s);
        s=toLower(s);//remove special chars and convert all to lower
        int l=s.length();
        for(int j=0;j<l;j++){
            vp[s[j]-'a'].first+=1;//increasing the value of each …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading std-pair

0
推荐指数
2
解决办法
177
查看次数

以结构为键的映射工作不正确

我正在尝试使用用户定义的结构作为 std::map 中的键。为了做到这一点,我在结构内部定义了比较运算符。然后我将类的两个实例添加到地图中。

#include <map>
#include <iostream>

struct Fraction {
    int num, den; // numerator and denumenator

    explicit Fraction() : num(1), den(1) {}
    explicit Fraction(int num_, int den_) : num(num_), den(den_) {}

    bool operator<(const Fraction& rhs) const {
        return num * rhs.den < den * rhs.num;
    }

};

int main() {
    std::map<Fraction, int> mymap;
    
    mymap[Fraction(100, 100)] = 1;
    mymap[Fraction(200, 200)] = 2;
    
    std::cout << mymap.at(Fraction(100, 100)) << std::endl;
    std::cout << mymap.at(Fraction(200, 200)) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

我期望得到

1
2
Run Code Online (Sandbox Code Playgroud)

但结果是

2 …
Run Code Online (Sandbox Code Playgroud)

c++ dictionary

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