我想通过迭代器打印集合的所有值。打印endl
完所有值后,我想打印不带最后一个的a。这是我的代码:
for (set<string> :: iterator it = str_set.begin();it!=str_set.end(); it++)
{
cout<<*it;
if((it+1)!=str_set.end()) //here I got error ...
cout<<endl;
}
但是我在检查时遇到if((it+1)!=str_set.end())
错误。这是怎么了?
这是错误消息:
error: no match for ‘operator+’ (operand types are ‘std::set<std::__cxx11::basic_string<char> >::iterator’ {aka std::_Rb_tree_const_iterator<std::__cxx11::basic_string<char> >’} and ‘int’)
92 | if(it+1!=str_set.end())
| ~~^~
| | |
| | int
| std::set<std::__cxx11::basic_string<char> >::iterator {aka std::_Rb_tree_const_iterator<std::__cxx11::basic_string<char> >}
这是怎么了
std::set::iterator
是Constant LegacyBidirectionalIterator。+
在此类对象和之间没有二进制运算符int
。
您可以std::next
用来获取下一个迭代器。那是习惯性的。
if ( std::next(it) != str_set.end() )
cout << endl;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句