【问题标题】:Trying to create a std::map with a custom ordering尝试使用自定义排序创建 std::map
【发布时间】:2015-07-09 16:21:22
【问题描述】:

我正在回答 Beginner C++ 书中的一个问题。我正在做一个关于std::map 的部分。问题是创建一个带有排序谓词的map,map需要自定义struct(wordProperty)作为key,定义(string)作为value。

我收到错误消息;

错误 1 ​​错误 C2664: 'bool fPredicate::operator ()(const std::string &,const std::string &) const' : 无法将参数 1 从 'const wordProperty' 转换为 'const std::string &' c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility 521 1 c++test1

参考第 52 行,这是这部分代码:

bool operator < (const wordProperty& item) const
{
    return(this->strWord < item.strWord);
}

该程序允许用户输入一个单词,输入该单词是否来自拉丁语并输入一个定义,然后在每次输入后打印字典。

#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <map>
#include <algorithm>
#include <string>

using namespace std;

template <typename T>
void DisplayContents(const T& Input)
{
for (auto iElement = Input.cbegin(); iElement != Input.cend(); ++iElement)
    cout << iElement->first << " -> " << iElement->second << endl;
cout << endl;
}

struct fPredicate
{
bool operator()(const string& str1, const string& str2) const
{
    string str1temp(str1), str2temp(str2);
    transform(str1.begin(), str1.end(), str1temp.begin(), tolower);
    transform(str2.begin(), str2.end(), str2temp.begin(), tolower);

    return(str1temp < str2temp);
}
};

struct wordProperty
{
string strWord;
bool bIsFromLatin;

wordProperty(const string& strWord, const bool & bLatin)
{
    this->strWord = strWord;
    bIsFromLatin = bLatin;
}

bool operator == (const wordProperty& item) const
{
    return(this->strWord == item.strWord);
}

bool operator < (const wordProperty& item) const
{
    return(this->strWord < item.strWord);
}

operator const char*() const
{
    string temp = this->strWord;
    if (this->bIsFromLatin)
    {
        temp += " is from Latin.";
    }
    else
        temp += " is not from Latin.";

    return temp.c_str();
}
};

int main()
{
map<wordProperty, string, fPredicate> mapWordDefinition;
while (true)
{
    cout << "Add a new word: ";
    string newWord;
    cin >> newWord;

    cout << "Is this word from Latin (Y/N)? ";
    string YN;
    bool newLatin;
    if ((YN == "Y") || (YN == "YES") || (YN == "y") || (YN == "yes") || (YN == "Yes"))
        newLatin = true;
    else
        newLatin = false;

    cout << "What is the definition of this word? ";
    string definition;
    cin >> definition;

    mapWordDefinition.insert(make_pair(wordProperty(newWord, newLatin), definition));

    cout << endl;

    DisplayContents(mapWordDefinition);

    cout << endl;

}
}

【问题讨论】:

    标签: c++ sorting dictionary operator-keyword stdmap


    【解决方案1】:

    谓词需要与地图的键类型一起使用。

    改变

    bool operator()(const string& str1, const string& str2) const;
    

    bool operator()(const wordProperty& wp1, const wordProperty& wp2) const;
    

    在函数的实现中,您可以提取对象wp1wp2strWord成员并按照您的逻辑。

    【讨论】:

    • 嘿,谢谢你。所以我把代码改成了这个; struct fPredicate { bool operator()(const wordProperty& wp1, const wordProperty& wp2) const { string str1temp = wp1.strWord;字符串 str2temp = wp2.strWord;返回(str1temp
    • @JonathanCain,我很困惑。您在发表评论后立即接受了我的回答。我应该假设您能够解决您的问题吗?
    • 不抱歉,我只是以为你已经修复了上一个错误所以我勾选了它,我没有设法解决标识符问题...
    • @JonathanCain,问题可能是wordProperty 是在fPredicate 之后定义的。您可以将wordProperty 的定义移到fPredicate 之前,看看是否可以解决问题。
    • 啊等一下,我知道了,我刚刚在 wordProperty 之前声明了谓词。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2018-04-11
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    相关资源
    最近更新 更多