вложенное использование boost :: bind в boost :: lambda не работает

Моя следующая программа Simple на boost lambda выдает следующую ошибку:

maxInMap.cpp:29:71: instantiated from here
/usr/include/boost/lambda/detail/function_adaptors.hpp:264:15: error: invalid initialization of reference of type ‘std::vector<int>&’ from expression of type ‘const std::vector<int>’

Пожалуйста, помогите мне разобраться в проблеме, так как это поможет мне в общем увеличении лямбды

#include <map>

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

#include <boost/lambda/lambda.hpp>
#include <boost/lambda/if.hpp>
#include <boost/lambda/algorithm.hpp>
#include <boost/lambda/bind.hpp>

using namespace std ;
using namespace boost ;
using namespace boost::lambda ;

int main()
{
map<int, vector<int> > intVecMap ;
int vecSizes[] = {3, 6, 2, 9, 5, 8, 1, 7, 10, 4} ;
for (int i = 0; i < 10; i++)
intVecMap[i] = vector<int>(vecSizes[i], i) ;

map<int, vector<int> >::const_iterator itr =
max_element(intVecMap.begin(), intVecMap.end(),
(bind(&vector<int>::size,
bind(&pair<int, vector<int> >::second, _1)) <
bind(&vector<int>::size,
bind(&pair<int, vector<int> >::second, _2)))) ;
if (itr == intVecMap.end())
cout << "Max Element function could not find any max :-(\n" ;
else
cout << "Max Index = "<<(*itr).first<<" Size = "<<(*itr).second.size()<<endl ;
return 0 ;
}

1

Решение

Ошибка на самом деле не связана с Boost.Lambda или Boost.Bind, это проблема:

                      bind(&pair<int, vector<int> >::second, _2)))) ;

value_type из map<int, vector<int> > не является pair<int, vector<int>> это pair<const int, vector<int>>

Если вы измените оба вхождения pair<int, в pair<const int, это должно скомпилироваться.

(Причина map имеет const тип ключа — это предотвращение аннулирования порядка карты путем изменения ключа элемента.)

4

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector