小编Dee*_*mah的帖子

如何找到小于或等于X的最大值和大于或等于X的最小值?

我正在尝试使用C++ 库中的lower_boundupper_bound函数algorithm来查找以下内容:

  • 小于或等于数字 X 的最大值。
  • 大于或等于数字 X 的最小值。

我写了以下代码:

#include <iostream>
#include <algorithm>

int main() {

    using namespace std;

    int numElems;
    cin >> numElems;

    int ar[numElems];
    for (int i = 0; i < numElems; ++i)
        cin >> ar[i];
    stable_sort(ar,ar+numElems);

    cout << "Input number X to find the largest number samller than or equal to X\n";
    int X;
    cin >> X;

    int *iter = lower_bound(ar,ar+numElems,X);
    if (iter == ar+numElems)
        cout << "Sorry, no …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm binary-search divide-and-conquer

4
推荐指数
1
解决办法
5963
查看次数

C++性能:使用声明Vs使用声明Vs直接使用库中的名称

我使用声明而不是使用显式std::方式来使用标准库对象.我得到的是否有使用任何性能提升了这个问题使用声明 S,使用声明 S和直接使用的名字从标准库.

例如:

要打印出"Hello world",我们可以写出以下方法:

  • 通过使用裸露的std::方式:

    std::cout << "Hello world";

  • 通过使用声明:

    using std::cout; cout << "Hello world";

  • 通过使用声明:

    using namespace std; cout << "Hello world";

那么,上述哪种方法具有最佳性能?更有效率?

c++ performance

0
推荐指数
1
解决办法
110
查看次数