【发布时间】:2017-03-31 09:07:27
【问题描述】:
我无法掌握下限/上限接口的语义。
考虑一下我写的这个测试sn-p:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
// sorted vector
std::vector<std::pair<size_t, std::vector<int>>> v = {
std::make_pair(0, std::vector<int>()),
std::make_pair(0, std::vector<int>()),
std::make_pair(3, std::vector<int>()),
std::make_pair(3, std::vector<int>()),
std::make_pair(5, std::vector<int>()),
std::make_pair(20, std::vector<int>())};
auto key = 3;
auto itr = std::lower_bound(
v.begin(), v.end(), key,
[](const auto &t1, const size_t d) -> bool { return t1.first < d; });
std::cout << itr->first << "\n";
}
为什么我不需要两个向量元素?为什么我只需要key 类型的一个和第二个参数(d)? d 到底是什么?该文档听起来像是一个转换为key 类型的向量元素。但是为什么不接受另一个向量元素作为第二个参数呢?为什么没有与key 进行比较?
为什么界面不是这样的:
auto itr = std::lower_bound(v.begin(), v.end(), 3, [](const auto& t1, const
auto& t2) -> bool {return t1.first < t2.first;});
你能解释一下参数背后的语义,尤其是d吗?
【问题讨论】:
标签: c++ interface lower-bound