【发布时间】:2016-12-19 12:04:09
【问题描述】:
我将从对列表中删除元素。当我使用像
这样的一对时std::pair<const int, bool>
我得到以下编译错误:
在 /usr/local/include/c++/6.1.0/utility:70:0 包含的文件中,
来自/usr/local/include/c++/6.1.0/algorithm:60,
来自 main.cpp:1:
/usr/local/include/c++/6.1.0/bits/stl_pair.h:在实例化 'std::pair<_t1 _t2>& std::pair<_t1 _t2>::operator=(std::pair<_t1 _t2>&&) [with _T1 = const int; _T2 = bool]':
/usr/local/include/c++/6.1.0/bits/stl_algo.h:868:16:需要来自 '_ForwardIterator std::__remove_if(_ForwardIterator, _ForwardIterator, _Predicate) [with _ForwardIterator = std::_List_iterator > _Predicate = __gnu_cxx::__ops::_Iter_pred&)> >]'
/usr/local/include/c++/6.1.0/bits/stl_algo.h:936:30:需要来自 '_FIter std::remove_if(_FIter, _FIter, _Predicate) [with _FIter = std::_List_iterator > _Predicate = main()::&)>]'
main.cpp:17:32:从这里需要
/usr/local/include/c++/6.1.0/bits/stl_pair.h:319:8:错误:赋值 只读成员 'std::pair::first'
first = std::forward(__p.first);
这是示例代码:
int main()
{
int id = 2;
std::list< std::pair <const int, bool> > l;
l.push_back(std::make_pair(3,true));
l.push_back(std::make_pair(2,false));
l.push_back(std::make_pair(1,true));
l.erase(std::remove_if(l.begin(), l.end(),
[id](std::pair<const int, bool>& e) -> bool {
return e.first == id; }));
for (auto i: l) {
std::cout << i.first << " " << i.second << std::endl;
}
}
我知道(如果我错了请纠正我):
只要列表的任何元素中存在 constness,我都会遇到完全相同的问题,例如,
list <const int>也会返回编译错误。如果我删除该对的第一个元素中的 const,代码将起作用。
-
更优雅、更高效的方法是使用 remove_if 列表方法,如下所示:
l.remove_if([id](std::pair<const int, bool>& e) -> bool { return e.first == id; });
但我的问题是,std::remove_if 的内部工作原理究竟是什么,它强制容器的元素不是 const?
【问题讨论】:
-
std::remove_if要求取消引用的类型是 MoveAssignable,而std::pair <const int, bool>不是。