c++ - 集合的 C++ 嵌套迭代器
问题描述
我正在尝试对集合使用嵌套迭代器,如下所示,但出现错误:
错误:'operator+' 不匹配(操作数类型是 'std::_Rb_tree_const_iterator' 和 'int')for(auto itj = iti+1; itj != st.end(); itj++){
int alternate(string s) {
set<char> st;
for(char x : s){
st.insert(x);
}
for(auto iti = st.begin(); iti != st.end(); iti++){
//string t = "";
for(auto itj = iti+1; itj != st.end(); itj++){
cout<<*iti<<" "<<*itj<<endl;
}
}
}
解决方案
std::set
的迭代器不满足指定的要求LegacyRandomAccessIterator
。它只满足LegacyBidirectionalIterator
.
所以iti + 1
不是一个有效的表达式,迭代器没有匹配的 operator+。
要解决您的问题,您可以使用std::next
.
for (auto iti = st.begin(); iti != st.end(); ++iti){
for (auto itj = std::next(iti); itj != st.end(); ++itj){
cout << *iti << " " << *itj << endl;
}
}
这是一个演示。
推荐阅读
- macos - NSCursor 获取“移动”(4 个箭头/箭头交叉)光标
- gremlin - 在 TinkerPop 中,顶点属性可以包含复杂对象吗?
- python - 使用 gridspecs 划分子图以具有 6 个相同大小的按钮
- python - 代码执行后 Jupyter Kernel 很忙
- sql - 用于按字段值配对行的 SQL
- r - R从selectInput获取值更新
- flask - Flask grequests 钩子中的附加参数
- c - 学校编程课题(Visual Studio)
- vba - 在展开父表单的第一行之前,无法引用 Access 子表单表单对象
- javascript - 循环和计数数组