相关疑难解决方法(0)

Java等价于c ++ equal_range(或lower_bound&upper_bound)

我有一个对象列表排序,我想找到一个对象的第一次出现和最后一次出现.在C++中,我可以轻松地使用std :: equal_range(或者只使用一个lower_bound和一个upper_bound).

例如:

bool mygreater (int i,int j) { return (i>j); }

int main () {
  int myints[] = {10,20,30,30,20,10,10,20};
  std::vector<int> v(myints,myints+8);                         // 10 20 30 30 20 10 10 20
  std::pair<std::vector<int>::iterator,std::vector<int>::iterator> bounds;

  // using default comparison:
  std::sort (v.begin(), v.end());                              // 10 10 10 20 20 20 30 30
  bounds=std::equal_range (v.begin(), v.end(), 20);            //          ^        ^

  // using "mygreater" as comp:
  std::sort (v.begin(), v.end(), mygreater);                   // 30 30 20 20 20 10 10 10
  bounds=std::equal_range (v.begin(), v.end(), 20, mygreater); …
Run Code Online (Sandbox Code Playgroud)

java binary-search lower-bound upperbound

11
推荐指数
2
解决办法
1万
查看次数

标签 统计

binary-search ×1

java ×1

lower-bound ×1

upperbound ×1