简体   繁体   English

即使条目相同,也找不到std :: map键

[英]std::map key not found even though the entries are identical

I'm learning C++ and I've been writing a wrapper for std::map and std::string, and I've stumbled upon a problem. 我正在学习C ++,并且一直在为std :: map和std :: string写一个包装器,但我偶然发现了一个问题。 Whenever I add something to the map using a string as key, once I try to access that item using the exact same key it says the key is out of bounds of the map. 每当我使用字符串作为键将某些东西添加到地图时,一旦我尝试使用完全相同的键来访问该项目,就会说该键超出了地图的范围。 Here's my code (irrelevant parts left out): 这是我的代码(忽略了不相关的部分):

ADictionary.h

#ifndef ADICTIONARY_H
#define ADICTIONARY_H

#include <map>

...

template<typename KEY, typename VALUE>
class ADictionary {
public:
    ...

    VALUE operator [](KEY key) const {
        return value.at(key);
    }

    void add(KEY key, VALUE value) {
        this->value.insert(std::make_pair(key, value));
    }

    ...

private:
    std::map<KEY, VALUE> value;
};

#endif

AString.cpp

#include "AString.h"

AString::AString() {
    value = "";
}

AString::AString(const char character) {
    value = character;
}

AString::AString(const char * characters) {
    value = characters;
}

AString::AString(std::string text) {
    value = text;
}

...

AString::operator const char *() const {
    return value.c_str();
}

AString::operator const std::string() const {
    return value;
}

...

ABoolean AString::operator<(AString & text) const {
    return getLength() < text.getLength();
}

ABoolean AString::operator>(AString & text) const {
    return text < *this;
}

ABoolean AString::operator==(AString & text) const {
    return value == text.value;
}

ABoolean AString::operator!=(AString & text) const {
    return !(text == *this);
}

AString & AString::operator=(AString & text) {
    value = text.value;

    return *this;
}

...

The code which uses the above

ADictionary<AString, AString> test;
AString a = "a";
AString b = "b";
test.add(a, b);
std::cout << test[a]; // Error occurs here, according to the program "a" is not a key in the map

I hope someone can explain to me what's going wrong. 我希望有人可以向我解释出了什么问题。 I've tried creating a dictionary with the default std::string as types and it worked correctly: 我尝试用默认的std :: string作为类型创建一个字典,它可以正常工作:

ADictionary<std::string, std::string> test;
std::string a = "a";
std::string b = "b";
test.add(a, b);
std::cout << test[a]; // No error this time

As I've said, I'm pretty new to C++ so there may be other errors. 就像我说过的那样,我对C ++还是很陌生,所以可能还有其他错误。 If so, feel free to point them out. 如果是这样,请随时指出。

Thanks! 谢谢!

EDIT: 编辑:

AString.h

#ifndef ASTRING_H
#define ASTRING_H

#include <string>

#include "ABoolean.h"
#include "AInteger.h"
#include "AList.h"

class ABoolean;
class AInteger;
template<typename VALUE>
class AList;

class AString {
public:
    AString();
    AString(const char);
    AString(const char *);
    AString(std::string);
    ~AString();

    operator const char *() const;
    operator const std::string() const;
    operator const AInteger() const;

    ABoolean operator<(AString &) const;
    ABoolean operator>(AString &) const;
    ABoolean operator==(AString &) const;
    ABoolean operator!=(AString &) const;
    AString & operator=(AString &);
    AString & operator+(AString &);
    AString & operator+=(AString &);

    void clear();
    ABoolean contains(AString) const;
    AInteger getIndex(AString) const;
    AInteger getLength() const;
    AList<AString> getSplit(AString) const;
    AString getSubstring(AInteger, AInteger) const;
    void removeRange(AInteger, AInteger);
    void removeSubstring(AString);
    void toLowercase();
    void toUppercase();

private:
    std::string value;
};

AString & operator+(const char, AString &);
AString & operator+(const char *, AString &);

#endif

Your string operators appear to be incorrect. 您的字符串运算符似乎不正确。

std::map uses the less than operator by default. std :: map默认使用小于运算符。 While you provide one for AString, the only thing it does is check the length of the string. 为AString提供一个字符串时,唯一要做的就是检查字符串的长度。 What if the two strings are of equal length? 如果两个字符串的长度相等怎么办?

The correct thing to do is to lexicographically compare the characters in the string. 正确的做法是按字典顺序比较字符串中的字符。 While there is a standard library function to do this, you can use operator < of the std::string values in your class: 尽管有一个标准的库函数可以执行此操作,但是您可以在类中使用std :: string值的运算符<:

friend bool operator<(AString const& a, AString const& b)
{
    return a.value < b.value;
}

EDIT: You may also wish to remove your conversion operators, or at least make them explicit, which prevents surprising and unwanted implicit conversions. 编辑:您可能还希望删除您的转换运算符,或者至少使其明确,以防止出现意外的和不需要的隐式转换。 Constructors taking one parameter (other than copy or move constructors) should also be declared explicit. 带有一个参数的构造函数(复制或移动构造函数除外)也应声明为显式的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM