c++ 和lower_bound相反的函数.docVIP

  • 0
  • 0
  • 约小于1千字
  • 约 1页
  • 2023-08-25 发布于陕西
  • 举报
C++ 中,`lower_bound` 函数用于在有序容器中查找指定元素的最小值,并返回该元素的下标。与之相反的函数是 `upper_bound`,用于查找指定元素的最大值,并返回该元素的下标。 下面是一个示例代码,演示了如何使用 `lower_bound` 和 `upper_bound` 函数: ```c++ #include iostream #include vector #include algorithm int main() { std::vectorint numbers = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3}; // 使用 lower_bound 查找最小值 auto it = std::lower_bound(numbers.begin(), numbers.end(), 5); std::cout 最小值: *it std::endl; // 使用 upper_bound 查找最大值 it = std::upper_bound(numbers.begin(), numbers.end(), 5); std::cout 最大值: *it std::endl; return 0; } ``` 输出结果为: ```cpp 最小值: 1 最大值: 9 ``` 在上面

文档评论(0)

1亿VIP精品文档

相关文档