我想通过迭代器打印集合的所有值。打印endl完所有值后,我想打印一个没有最后一个的值。这是我的代码:
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;
}
Run Code Online (Sandbox Code Playgroud)
但是我在检查时遇到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> >}
Run Code Online (Sandbox Code Playgroud)
这里有什么问题?
std::set::iterator是一个Constant LegacyBidirectionalIterator。还没有二进制+这样一个对象和操作者之间int。
您可以使用std::next获取下一个迭代器。那将是惯用的。
if ( std::next(it) != str_set.end() )
cout << endl;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
650 次 |
| 最近记录: |