我的下面的代碼給我編譯器錯誤,我不明白我在做什麼錯誤。任何人都可以幫忙嗎?std :: map與lambda比較器
基本上我所要做的就是通過引用一個函數來傳遞一個STL映射容器來填充它。這個映射容器還有一個與之相關的比較器lambda。
#include "stdafx.h"
#include <functional>
#include <map>
using namespace std;
typedef struct _tagAddressBook
{
string strFirstName;
string strLastName;
long nZipCode;
} AddressBook;
void foo(map<string, AddressBook, function<bool(const string&, const string&)>> &myAddressBook)
{
AddressBook addressBookInstance;
addressBookInstance.strFirstName = "Bob";
addressBookInstance.strLastName = "Parker";
addressBookInstance.nZipCode = 12345;
myAddressBook.insert(std::pair<string, AddressBook>(addressBookInstance.strFirstName, addressBookInstance));
}
int _tmain(int argc, _TCHAR* argv[])
{
auto myComparator = [] (const string &strLeft, const string &strRight) { return(strLeft.compare(strRight) <= 0 ? true : false); };
map<string, AddressBook, decltype(myComparator)> myAddressBook(myComparator);
foo(myAddressBook);
return 0;
}
我得到VS2012下面的編譯錯誤
錯誤1錯誤C2664: '富':無法從 '的std ::地圖< _Kty,_Ty,_Pr>' 轉換參數1爲「STD: :地圖< _Kty,_Ty,_Pr> &「d:\我的項目\ mapwithlambdacomparator \ mapwithlambdacomparator \ mapwithlambdacomparator.cpp 32
2 IntelliSense: a reference of type "std::map<std::string, AddressBook, std::function<bool (const std::string &, const std::string &)>, std::allocator<std::pair<const std::string, AddressBook>>> &" (not const-qualified) cannot be initialized with a value of type "std::map<std::string, AddressBook, lambda []bool (const std::string &strLeft, const std::string &strRight)->bool, std::allocator<std::pair<const std::string, AddressBook>>>" d:\My Projects\MapWithLambdaComparator\MapWithLambdaComparator\MapWithLambdaComparator.cpp 32
Lambdas與std :: function無關。他們是他們自己的班級類型。 – WhozCraig