visual-studio-2010 - VS2010中使用set时remove_if的问题

标签 visual-studio-2010 visual-studio-2005 stl set

我有以下代码。

#include <set>
#include <algorithm>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
 typedef set<long> MySet;

 MySet a;

 for( int i = 0; i < 10; ++i)
 {
  a.insert(i);
 }

 MySet::iterator start,end,last;

 start = a.begin();
 end = a.end();

 last = remove_if(start,end,bind2nd(less_equal<long>(),5));

 return 0;
}

VS2005下用哪个编译好。但是使用 VS2010 时出现以下错误:

错误 1 ​​错误 C3892: '_Next' : 不能分配给 const c:\program files\microsoft visual studio 10.0\vc\include\algorithm 的变量

如果我让容器成为一个载体,一切都很好。

我猜我不知道的标准发生了一些变化,有人可以解释为什么这不再有效吗?

最佳答案

一个 std::set始终按排序顺序保持其元素。 std::remove_if尝试将您不想删除的元素移动到集合的开头。这将违反集合以排序顺序维护元素的不变量。

代码永远不应该工作。较旧的编译器可能没有足够严格地执行规则,让您知道它不应该起作用,但是(显然)您当前的编译器可以。

关于visual-studio-2010 - VS2010中使用set时remove_if的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3180869/

相关文章:

visual-studio-2010 - visual studio 2010 intellisense 不会在一段时间后自动弹出

visual-studio-2010 - Qt Moc在msbuild下并行处理多个文件

c# - 使用 shellExecute 运行 c# 控制台应用程序

c# - 使用文本框调用 C#

c++ - sqlite3中使用汉字

c++ - wstring::find() 不适用于非拉丁符号?

C#、sp_executesql 和不正确的语法

visual-studio-2005 - 如何在 Visual Basic 2005 Express 中禁用 'Just My Code'?

c++ - 在堆栈上使用 std::max_element<int>

c++ - 列表容器上的 std::set_difference