【问题标题】:Multiset Index Finding多集索引查找
【发布时间】:2015-03-16 13:09:39
【问题描述】:

我有一组 int 。 C++

multiset<int>t;

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

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

但找不到从多集开始的相对位置。 正如 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;
}

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

merge(v1.begin(),v1.end(),v2.begin(),v1.end(),back_inserter(v))

【问题讨论】:

  • 你应该在...第二个问题中问你的第二个问题。

标签: c++ multiset


【解决方案1】:

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

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

【讨论】:

  • 感谢 D Drmmr,它起作用了..但是复杂性是线性的...?这可能是给我 TLE ...再次你知道如何合并到多集吗?
【解决方案2】:

对于std::multiset,成员类型iteratorconst_iterator 是双向迭代器类型。双向迭代器不支持算术运算符 + 和 -(详情请查看 cppreference)。

std::distance 可用于计算两个迭代器之间的元素个数。

std::distance 使用operator- 来计算如果参数是随机访问迭代器的元素数。否则,它会重复使用递增运算符(operator++)。

这里是来自 cppreference 的稍微改动的代码 sn-p。

#include <iostream>
#include <set>

int main ()
{
  std::multiset<int> mymultiset;
  std::multiset<int>::iterator itlow, itup;

  for (int i = 1; i < 8; i++) mymultiset.insert(i * 10); // 10 20 30 40 50 60 70

  itlow = mymultiset.lower_bound(30);
  itup = mymultiset.upper_bound(40);

  std::cout << std::distance(mymultiset.begin(), itlow) << std::endl;
  std::cout << std::distance(mymultiset.begin(), itup) << std::endl;

  mymultiset.erase(itlow, itup); // 10 20 50 60 70

  std::cout << "mymultiset contains: ";
  for (std::multiset<int>::iterator it = mymultiset.begin(); it != mymultiset.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}

输出

2
4
mymultiset contains:  10 20 50 60 70

您可以将std::multisetstd::multiset::insert 成员函数合并如下;

#include <iostream>
#include <set>

int main ()
{
  std::multiset<int> mset1;
  std::multiset<int> mset2;

  for (int i = 1; i < 8; i++) mset1.insert(i * 10); // 10 20 30 40 50 60 70
  for (int i = 1; i < 8; i++) mset2.insert(i * 10); // 10 20 30 40 50 60 70

  mset1.insert(mset2.begin(), mset2.end());

  std::cout << "mset1 contains: ";
  for (std::multiset<int>::iterator it = mset1.begin(); it != mset1.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}

输出

mset1 contains:  10 10 20 20 30 30 40 40 50 50 60 60 70 70

【讨论】:

  • 我们可以在恒定时间内完成吗? distance 需要线性时间!
  • @Matrix.code,仅用于随机访问迭代器的恒定时间,std::multiset 不可能。我不知道细节,但根据您的要求,可以实现您自己的数据结构。
猜你喜欢
  • 2014-08-27
  • 1970-01-01
  • 2018-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-22
  • 1970-01-01
相关资源
最近更新 更多