我正在学习C ++,我一直在为std :: map和std :: string编写一个包装器,我偶然发现了一个问题。每当我使用字符串作为键向地图添加内容时,一旦我尝试使用完全相同的键访问该项目,它就表示该键超出了地图的范围。这是我的代码(不相关的部分遗漏):
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
我希望有人可以向我解释出现了什么问题。我已经尝试使用默认的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
正如我所说,我对C ++很陌生,所以可能还有其他错误。如果是这样,请随意指出它们。
谢谢!
编辑:
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
答案 0 :(得分:4)
您的字符串运算符似乎不正确。
std :: map默认使用小于运算符。当你为AString提供一个时,它唯一做的就是检查字符串的长度。如果这两个字符串长度相等怎么办?
正确的做法是按字母顺序比较字符串中的字符。虽然有一个标准的库函数来执行此操作,但您可以使用运算符&lt;您的类中的std :: string值:
friend bool operator<(AString const& a, AString const& b)
{
return a.value < b.value;
}
编辑:您可能还希望删除转化运算符,或者至少使它们明确,这可以防止意外和不需要的隐式转换。采用一个参数(复制或移动构造函数除外)的构造函数也应声明为显式。