【问题标题】:upper_bound and lower_bound in different headers?不同标题中的上界和下界?
【发布时间】:2016-06-30 21:42:26
【问题描述】:

lower_bound 不会给出任何错误,但upper_bound 会。在搜索时,它存在于<algorithm> 头文件中。

为什么不一致?知道会很有趣。

C++ 代码:

#include <iostream>                                                
#include <vector>     
using namespace std;                                               
int main() {                                                       
    vector<int> ans={1,5,7,8};                                     
    cout<< upper_bound(ans.begin(), ans.end(), 16) -ans.begin() <<endl;
    cout<< lower_bound(ans.begin(), ans.end(), 16) -ans.begin() <<endl;
    return 0;                                                      
}     

输出:

/tmp/a.cpp: In function ‘int main()’:
/tmp/a.cpp:7:50: error: ‘upper_bound’ was not declared in this scope
     cout<< upper_bound(ans.begin(), ans.end(), 16) <<endl;

编辑

$ g++ -v
gcc version 5.2.1 20151010 (Ubuntu 5.2.1-22ubuntu2) 

【问题讨论】:

  • 两者都在&lt;algorithm&gt; 中定义。您可以在不包含标头的情况下访问其中一个,这一事实说明...关于您的实现,但一般来说与 C++ 无关。
  • 什么编译器?如果不包含 algorithm: webcompiler.cloudapp.net ,此在线 Visual C++ 编译器也无法编译。这可能是一个实现工件,因为通过 iostreamvector 前向声明 lower_bound() 而不是 upper_bound() 包含一些其他代码。
  • 顺便说一句,真的只是为这个问题创建了两个标签吗?为什么你认为它们有用?
  • @Baum 'upper_bound' 和 'lower_bound' 关于 stl 可能有很多问题。
  • @mtk:当然,但没有人能成为“upper_bound”方面的专家。所以没有标签。

标签: c++ c++-standard-library


【解决方案1】:

当您包含任何 std 标头时,可以导入 std 中的任何和所有符号。

基本上,您可以保证您要求的标头中的符号可用。额外的可以来。

所以这是符合要求的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 2013-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多