c++ - 如何在 C++ STL 的集合中找到有序的后继或前驱
问题描述
C++ STL 中的集合是使用红黑/AVL 树或任何其他自平衡树实现的。那么,我怎样才能在其中找到有序的继任者或有序的前任?
解决方案
set<int> s;
for (int i = 0; i < n; i++) {
s.insert(i);
}
// returns an iterator pointing to x
auto p = s.insert(x);
// inorder successor can be found with incrementing iterator
// if it doesn't exists iterator to end to will be returned
if (++p.first != s.end()) {
cout << *p.first << endl;
}
推荐阅读
- django - 从页面 Django 自动结束视频通话会话
- python - 在 PyCharm 中调试瓶服务器时出现 SyntaxError "Non-ASCII character '\\xca' in file..."
- odoo - 在 Odoo 11 中禁用编辑和删除选项
- react-navigation - 使用 react-navigation 5.x 在子组件内路由
- ssis - 由于 SSIS 中的错误 0x80040154 dtexec 错误,无法创建 dts.application
- c# - LINQ to SQL 查找句子中的单词以特定字符开头的记录
- javascript - 使用 JS/NodeJS 搜索数据的更好方法
- npm - npm 脚本中的括号
- python - Python文件读写——脚本
- excel - 尝试从网站导入修剪后的价值