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