STL集的交集和输出
STL set intersection and the output
我有一个这样的代码片段,将在VC++2010下编译。
std::set<int> s1;
std::set<int> s2;
std::set<int> res_set;
std::set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), res_set.begin());
据我所知,这应该是可行的。然而,我得到了构建错误:
c:program files (x86)microsoft visual studio 10.0vcincludealgorithm(4494): error C3892: 'std::_Tree_const_iterator<_Mytree>::operator *' : you cannot assign to a variable that is const
1> with
1> [
1> _Mytree=std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>
1> ]
1> c:program files (x86)microsoft visual studio 10.0vcincludealgorithm(4522) : see reference to function template instantiation '_OutIt std::_Set_intersection<_InIt1,_InIt2,_OutIt>(_InIt1,_InIt1,_InIt2,_InIt2,_OutIt)' being compiled
1> with
1> [
1> _OutIt=std::_Tree_const_iterator<std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>>,
1> _InIt1=std::_Tree_unchecked_const_iterator<std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>>,
1> _InIt2=std::_Tree_unchecked_const_iterator<std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>>
1> ]
1> c:program files (x86)microsoft visual studio 10.0vcincludealgorithm(4549) : see reference to function template instantiation '_OutIt std::_Set_intersection1<std::_Tree_unchecked_const_iterator<_Mytree>,std::_Tree_unchecked_const_iterator<_Mytree>,_OutIt>(_InIt1,_InIt1,_InIt2,_InIt2,_OutIt,std::tr1::true_type)' being compiled
1> with
1> [
1> _OutIt=std::_Tree_const_iterator<std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>>,
1> _Mytree=std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>,
1> _InIt1=std::_Tree_unchecked_const_iterator<std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>>,
1> _InIt2=std::_Tree_unchecked_const_iterator<std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>>
1> ]
1> c:p4rpkrcodedepotdevstatspokerprotypestatserverachievementmanager.cpp(175) : see reference to function template instantiation '_OutIt std::set_intersection<std::_Tree_const_iterator<_Mytree>,std::_Tree_const_iterator<_Mytree>,std::_Tree_const_iterator<_Mytree>>(_InIt1,_InIt1,_InIt2,_InIt2,_OutIt)' being compiled
1> with
1> [
1> _OutIt=std::_Tree_const_iterator<std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>>,
1> _Mytree=std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>,
1> _InIt1=std::_Tree_const_iterator<std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>>,
1> _InIt2=std::_Tree_const_iterator<std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>>
1> ]
为此,我做了明确的模板参数声明:
std::set_intersection<std::set<int>::const_iterator, std::set<int>::const_iterator, std::set<int>::iterator>(
s1.begin(), s1.end(), s2.begin(), s2.end(), res_set.begin()
);
但我也有同样的错误。我的问题是,在第二种情况下,如果我要传递const_iterator,它应该会失败,并在const_iteror和迭代器之间出现转换错误,因为参数类型不匹配。我在这里错过了什么?(我知道set_interaction的"inserter"形式,但我想了解我在这里做错了什么)
std::set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), res_set.begin());
最后一个参数应该是输出迭代器。在您的情况下,它不是,甚至是不可变的(bc.std::set
有不可变的元素)。您应该使用insert_iterator:
std::set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), std::inserter(res_set, res_set.end()));
std::set_intersection
的输出参数必须是可变的value_type
。CCD_ 4的迭代器从不支持突变,因为更改元素的值可能会更改它所属的位置布景。使用std::set_iterator
设计组中的功能以处理排序的序列,例如std::vector
。
在您的情况下,您可以将std::set
替换为std::vector
,根据需要对它们进行排序(并且可能使用std::lower_bound
和插入以使它们在插入时保持排序),或使用CCD_ 10。
res_set.begin()
不能用作set_intersection
的输出参数,原因有两个:
- 集合为空,这将尝试覆盖集合的现有元素
- 不能修改集合的元素
相反,您需要一个insert_iterator
,将新元素插入到集合中:
std::set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(),
std::inserter(res_set, res_set.end()))
相关文章:
- 递归函数计算序列中的平方和(并输出过程)
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 请解释"函数1(p1,p2,p3);"的输出
- 为什么这个运算符<重载函数对 STL 算法不可见?
- C++:将控制台输出存储在宏中更好吗
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- STL:在没有输出的情况下处理两个集合
- 使用向量(STL)时,未定义的行为,请说明理性背后的以下代码输出
- stl风格的算法:如何管理输出迭代器
- stl数据结构的堆栈上输出参数与返回值的效率
- 如何在C++中获取任意 (STL) 输出迭代器的"assignable type"
- STL映射的奇怪输出
- 重载输出运算符<<以使用 STL 列表迭代器
- STL集的交集和输出
- STL显示输出
- 无法直接输出 STL 矢量的迭代器值
- c++ STL映射迭代器输出十六进制值
- 用Python生成器替换c++ STL输出迭代器
- STL映射输出迭代器查找值