尝试使用自定义排序创建std::map
Trying to create a std::map with a custom ordering
我正在回答一本初学者c++书中的问题。我正在做一个关于std::map
的部分。问题是创建一个带有排序谓词的映射,该映射需要是一个自定义结构(wordProperty)作为键,定义(string)作为值。
我得到错误;
错误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.0vcincludexutility 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;
}
}
谓词需要使用映射的键类型。
改变bool operator()(const string& str1, const string& str2) const;
bool operator()(const wordProperty& wp1, const wordProperty& wp2) const;
在函数的实现中,您可以提取对象wp1
和wp2
的strWord
成员,并遵循您的逻辑。
相关文章:
- 使用std::multimap迭代器创建std::list
- ";结果类型必须是可从输入范围的值类型""构造的;创建std::vector时
- C++如何创建 std::map
- 在共享缓冲区内存中创建 ::std::string 对象
- 创建 std::string 的二维数组的最佳做法
- 从 T 创建 std::future 的最佳方式<T>
- 是否可以从 std::any 创建 std::any 与 std::reference_wrapper?
- 从编译时已知的日历日期创建"std::chrono::time_point"
- 创建 std::函数,它返回具有函数成员值的变量.分段错误
- 如何从可变参数模板参数创建 std::tuple<>?
- 使用成员函数创建std::函数不会编译
- 通过预处理器创建 std::p air<std::string,some_enum>
- 创建 std::set 只复制一个元素,如何解决这个问题?
- 动态创建 std::vector 并将其传递给另一个函数的各种方法
- 编译问题 C++ 同时,尝试通过调用另一个对象中的成员函数来创建 std:: 线程
- 从函数应用程序创建 std::vector
- 创建 std::thread 时出现错误 C2280,未在 C++ 中初始化
- 使用std :: to_string()创建std :: String时的segfault
- 将C 数组写成文件,避免创建std :: String
- 稍后使用<Class>调用类构造函数创建 std::vector