Ste*_*eve 1 boost stl visual-studio-2010
以下代码
#include "stdafx.h"
#include <string>
#include <set>
#include <boost/algorithm/string/trim.hpp>
int _tmain(int argc, _TCHAR* argv[])
{
std::set<std::string> test;
test.insert("test1 ");
test.insert("test2 ");
for(std::set<std::string>::iterator iter = test.begin(); iter != test.end(); ++iter)
{
boost::algorithm::trim(*iter);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在VS2008中编译但在VS2010中因错误而失败
error C2663: 'std::basic_string<_Elem,_Traits,_Ax>::erase' : 3 overloads have no legal conversion for 'this' pointer \boost\include\boost\algorithm\string\trim.hpp
Run Code Online (Sandbox Code Playgroud)
这表明函数中的const匹配存在问题.如果我将set更改为vector,一切都很好.我也可以做一个
boost::algorithm::trim(const_cast<std::string&>(*iter));
Run Code Online (Sandbox Code Playgroud)
但我讨厌把它放在我的代码中,似乎我不应该这样,因为我没有在集合上使用const迭代器.如果这是预期的行为,为什么有人有任何想法?
std :: set的元素是不可变的.如果你可以在适当的位置更新它们,那么每当你更新一个元素(这将是非常困难,可能不可能实现)时,它要么需要重新排序,或者更新元素会破坏集合的排序保证.
允许集合的元素是可变的是原始C++ 98标准中的疏忽.它在C++ 11中得到了纠正; 新标准要求设置迭代器以取消引用const元素.VS10实现了新规则; VS08遵循旧标准,其中更新set元素会调用未定义的行为.
(参见最终草案C++ 11标准,第23.2.4节第6段)
归档时间: |
|
查看次数: |
906 次 |
最近记录: |