多集索引查找

Mat*_*ode 7 c++ multiset

我有一个多组 int 。C++

multiset<int>t;
Run Code Online (Sandbox Code Playgroud)

我需要找到大于等于 val 的第一个元素的位置。我为此使用了lower_bound

multiset<int>::iterator it= lower_bound(t[n].begin(), t[n].end(), val);
Run Code Online (Sandbox Code Playgroud)

但是找不到从多集开始的相对位置。正如 The Cplusplus.com 建议使用...作为向量。

// lower_bound/upper_bound example
#include <iostream>     // std::cout
#include <algorithm>    // std::lower_bound, std::upper_bound, std::sort
#include <vector>       // std::vector

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::sort (v.begin(), v.end());                // 10 10 10 20 20 20 30 30

  std::vector<int>::iterator low,up;
  low=std::lower_bound (v.begin(), v.end(), 20); //          ^
  up= std::upper_bound (v.begin(), v.end(), 20); //                   ^

  std::cout << "lower_bound at position " << (low- v.begin()) << '\n';
  std::cout << "upper_bound at position " << (up - v.begin()) << '\n';

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我可以在多组中进行吗..?另一个问题是:我可以合并到多组,如如下所示的向量,v1,v2,v 是向量吗?

merge(v1.begin(),v1.end(),v2.begin(),v1.end(),back_inserter(v))
Run Code Online (Sandbox Code Playgroud)

D D*_*mmr 5

获取两个迭代器之间距离的通用方法是调用std::distance

auto it = std::lower_bound(t[n].begin(), t[n].end(), val);
const auto pos = std::distance(t[n].begin(), it);
Run Code Online (Sandbox Code Playgroud)