使用lambda的多图谓词 [英] multimap predicate using lambda

查看:120
本文介绍了使用lambda的多图谓词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  std:我尝试使用多图表对一些关系数据进行排序, :multimap < 
std :: vector< Message>,std :: string,
bool(*)(const std :: vector< Message>,const std :: vector< Message>
sortmap([&](const std :: vector< Message>& lhs,
const std :: vector< Message>& rhs){
return lhs.size < rhs.size();
});

这些类型似乎是正确的,但视觉工作室另有说明。

解决方案

我将介绍一些提到的选项:



选项1 @Joachim Pilborg

  auto comparator = [](const std :: vector< Message>& lhs,
const std :: vector< Message>& rhs){
return lhs.size() rhs.size();
};

std :: multimap< std :: vector< Message>,std :: string,decltype(comparator)> sortmap(比较器);

消息msg;
std :: vector< Message>消息;
messages.push_back(msg);

std :: string str;
auto test = std :: make_pair(messages,str);

sortmap.emplace(test);

会导致

 code> 1> c:\program files(x86)\microsoft visual studio 12.0 \vc\include\xtree(1007):错误C3497:您不能构造lambda的实例
1。 c:\program files(x86)\microsoft visual studio 12.0 \vc\include\xtree(1006):在编译类模板成员函数'main ::< lambda_2efe1f793bd1dfe8b84381ab9a3a87dc> std :: _ Tree_comp< false,_Traits> :: _ Getcomp(void)const'
1> with
1> [
1> _Traits = std :: _Tmap_traits< std :: vector< Message,std :: allocator< Message>>,std :: string,main ::< lambda_2efe1f793bd1dfe8b84381ab9a3a87dc> ;, std :: allocator< std :: pair& :vector< Message,std :: allocator< Message>>,std :: string>>,true>
1> ]
1> c:\program files(x86)\microsoft visual studio 12.0 \vc\include\xtree(1789):参见函数模板实例化'main ::< lambda_2efe1f793bd1dfe8b84381ab9a3a87dc> std :: _ Tree_comp 1> with
1> [
1> _Traits = std :: _Tmap_traits< std :: vector< Message,std :: allocator< Message>>,std :: string,main ::< lambda_2efe1f793bd1dfe8b84381ab9a3a87dc> ;, std :: allocator< std :: pair& :vector< Message,std :: allocator< Message>>,std :: string>>,true>
1> ]
1> c:\program files(x86)\microsoft visual studio 12.0 \vc\include\xtree(1024):参考类模板实例化'std :: _ Tree_comp< false,_Traits& b 1> with
1> [
1> _Traits = std :: _Tmap_traits< std :: vector< Message,std :: allocator< Message>>,std :: string,main ::< lambda_2efe1f793bd1dfe8b84381ab9a3a87dc> ;, std :: allocator< std :: pair& :vector< Message,std :: allocator< Message>>,std :: string>>,true>
1> ]
1> c:\program files(x86)\microsoft visual studio 12.0 \vc\include\map(275):参见类模板实例化的参考std :: _ Tree< std :: _ Tmap_traits< _Kty,_Ty,_Pr ,_Alloc,true>>'正在编译
1> with
1> [
1> _Kty = std :: vector< Message,std :: allocator< Message>>
1> ,_Ty = std :: string
1> ,_Pr = main ::< lambda_2efe1f793bd1dfe8b84381ab9a3a87dc>
1> ,_Alloc = std :: allocator< std :: pair< const std :: vector< Message,std :: allocator< Message>>,std :: string>
1> ]
1> c:\users\c\documents\visual studio 2013\projects\project1\project1\source.cpp(15):参见类模板实例化'std :: multimap< std :: vector< ; Message,std :: allocator< _Ty>>,std :: string,main ::< lambda_2efe1f793bd1dfe8b84381ab9a3a87dc>,std :: allocator< std :: pair< const _Kty,std :: basic_string< char,std :: char_traits< ; char>,std :: allocator< char>>>>>'正在编译
1> with
1> [
1> _Ty = Message
1> ,_Kty = std :: vector< Message,std :: allocator< Message>>
1> ]

选项2 @PlasmaHH

  std :: multimap< 
std :: vector< Message>,std :: string,
bool(*)(const std :: vector< Message>& const std :: vector< Message>&
sortmap([&](const std :: vector< Message>& lhs,
const std :: vector< Message>& rhs){
return lhs.size < rhs.size();
});
消息msg;
std :: vector< Message>消息;
messages.push_back(msg);

std :: string str;
auto test = std :: make_pair(messages,str);

sortmap.emplace(test);

这个DOES编译,但奇怪的是在可视工作室错误列表中留下错误(并留下恼人的红色波浪):

  1 IntelliSense:没有构造函数的实例std :: multimap< _Kty,_Ty,_Pr,_Alloc> :: multimap [with _Kty = std :: vector< Message,std :: allocator< Message>>>,_Ty = std :: string,_Pr = bool(*)(const std :: vector< Message,std :: allocator< Message> >&& const std :: vector< Message,std :: allocator< Message>&),_Alloc = std :: allocator< std :: pair< const std :: vector< Message,std :: allocator< ; message>>,std :: string>>]匹配参数列表
参数类型是:(lambda [] bool(const std :: vector< Message,std :: allocator< Message> & lhs,const std :: vector< Message,std :: allocator< Message>& rhs) - > bool)c:\Users\c\Documents\Visual Studio 2013\Projects\\ \\ Project1\Project1\Source.cpp 12

}



我宁愿用

  auto comparator = [](const std :: vector< Message> & lhs,
const std :: vector< Message> & rhs){
return lhs.size()< rhs.size();
};
std :: multimap< std :: vector< Message>,std :: string,
std :: function< bool(const std :: vector< Message>&
const std :: vector< Message>&)>>
sortmap(comparator);
消息msg;
std :: vector< Message>消息;
messages.push_back(msg);

std :: string str;
auto test = std :: make_pair(messages,str);

sortmap.emplace(test);

选项1似乎无效:



https://connect.microsoft.com/VisualStudio/feedback/details/727957/vc11-beta-compiler-fails-to-compile-lambda-key-compalle-for-maps-and-sets



显式定义类型工作,并且由于某种原因使用std :: function使得(烦人的)红色波动和错误列表条目消失。希望这有助于!


I am trying to sort some relational data using a multimap but am having issues getting the predicate defined correctly.

  std::multimap<
      std::vector<Message>, std::string,
      bool (*)(const std::vector<Message>, const std::vector<Message>)>
  sortmap([&](const std::vector<Message> &lhs,
              const std::vector<Message> &rhs) {
    return lhs.size() < rhs.size();
  });

The types seem to be correct but visual studio says otherwise.

解决方案

I'll go through some of the options mentioned:

Option 1 @Joachim Pilborg

  auto comparator = [](const std::vector<Message>& lhs,
    const std::vector<Message>& rhs) {
    return lhs.size() < rhs.size();
  };

  std::multimap<std::vector<Message>, std::string, decltype(comparator)> sortmap(comparator);

  Message msg;
  std::vector<Message> messages;
  messages.push_back(msg);

  std::string str;
  auto test = std::make_pair(messages, str);

  sortmap.emplace(test);

results in

1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xtree(1007): error C3497: you cannot construct an instance of a lambda
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\xtree(1006) : while compiling class template member function 'main::<lambda_2efe1f793bd1dfe8b84381ab9a3a87dc> std::_Tree_comp<false,_Traits>::_Getcomp(void) const'
1>          with
1>          [
1>              _Traits=std::_Tmap_traits<std::vector<Message,std::allocator<Message>>,std::string,main::<lambda_2efe1f793bd1dfe8b84381ab9a3a87dc>,std::allocator<std::pair<const std::vector<Message,std::allocator<Message>>,std::string>>,true>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\xtree(1789) : see reference to function template instantiation 'main::<lambda_2efe1f793bd1dfe8b84381ab9a3a87dc> std::_Tree_comp<false,_Traits>::_Getcomp(void) const' being compiled
1>          with
1>          [
1>              _Traits=std::_Tmap_traits<std::vector<Message,std::allocator<Message>>,std::string,main::<lambda_2efe1f793bd1dfe8b84381ab9a3a87dc>,std::allocator<std::pair<const std::vector<Message,std::allocator<Message>>,std::string>>,true>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\xtree(1024) : see reference to class template instantiation 'std::_Tree_comp<false,_Traits>' being compiled
1>          with
1>          [
1>              _Traits=std::_Tmap_traits<std::vector<Message,std::allocator<Message>>,std::string,main::<lambda_2efe1f793bd1dfe8b84381ab9a3a87dc>,std::allocator<std::pair<const std::vector<Message,std::allocator<Message>>,std::string>>,true>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 12.0\vc\include\map(275) : see reference to class template instantiation 'std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,true>>' being compiled
1>          with
1>          [
1>              _Kty=std::vector<Message,std::allocator<Message>>
1>  ,            _Ty=std::string
1>  ,            _Pr=main::<lambda_2efe1f793bd1dfe8b84381ab9a3a87dc>
1>  ,            _Alloc=std::allocator<std::pair<const std::vector<Message,std::allocator<Message>>,std::string>>
1>          ]
1>          c:\users\c\documents\visual studio 2013\projects\project1\project1\source.cpp(15) : see reference to class template instantiation 'std::multimap<std::vector<Message,std::allocator<_Ty>>,std::string,main::<lambda_2efe1f793bd1dfe8b84381ab9a3a87dc>,std::allocator<std::pair<const _Kty,std::basic_string<char,std::char_traits<char>,std::allocator<char>>>>>' being compiled
1>          with
1>          [
1>              _Ty=Message
1>  ,            _Kty=std::vector<Message,std::allocator<Message>>
1>          ]

Option 2 @PlasmaHH

  std::multimap<
      std::vector<Message>, std::string,
      bool (*)(const std::vector<Message> &, const std::vector<Message> &)>
  sortmap([&](const std::vector<Message> &lhs,
              const std::vector<Message> &rhs) {
    return lhs.size() < rhs.size();
  });
  Message msg;
  std::vector<Message> messages;
  messages.push_back(msg);

  std::string str;
  auto test = std::make_pair(messages, str);

  sortmap.emplace(test);

This DOES compile, but strangely leaves an error in visual studios error list (and leaves an annoying red squiggle):

1   IntelliSense: no instance of constructor "std::multimap<_Kty, _Ty, _Pr, _Alloc>::multimap [with _Kty=std::vector<Message, std::allocator<Message>>, _Ty=std::string, _Pr=bool (*)(const std::vector<Message, std::allocator<Message>> &, const std::vector<Message, std::allocator<Message>> &), _Alloc=std::allocator<std::pair<const std::vector<Message, std::allocator<Message>>, std::string>>]" matches the argument list
        argument types are: (lambda []bool (const std::vector<Message, std::allocator<Message>> &lhs, const std::vector<Message, std::allocator<Message>> &rhs)->bool)  c:\Users\c\Documents\Visual Studio 2013\Projects\Project1\Project1\Source.cpp   12

}

I instead settle with

  auto comparator = [](const std::vector<Message> &lhs,
    const std::vector<Message> &rhs) {
    return lhs.size() < rhs.size();
  };
  std::multimap<std::vector<Message>, std::string,
    std::function<bool(const std::vector<Message> &,
    const std::vector<Message> &) >>
    sortmap(comparator);
  Message msg;
  std::vector<Message> messages;
  messages.push_back(msg);

  std::string str;
  auto test = std::make_pair(messages, str);

  sortmap.emplace(test);

Option 1 appears not to work:

https://connect.microsoft.com/VisualStudio/feedback/details/727957/vc11-beta-compiler-fails-to-compile-lambda-key-comparer-for-maps-and-sets

explicitly defining the type works, and using std::function for some reason makes the (annoying!!) red squiggle and error list entry go away. Hope this helps!

这篇关于使用lambda的多图谓词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆