多集索引查找
Multiset Index Finding
本文关键字:索引查找 更新时间:2023-10-16
我有一组多组 int .C++
multiset<int>t;
我需要找到第一个大于等于 val 的元素的位置。我为此使用了lower_bound
multiset<int>::iterator it= lower_bound(t[n].begin(), t[n].end(), val);
但找不到多集开头的相对位置。正如 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))
获取两个迭代器之间距离的通用方法是调用 std::d istance。
auto it = std::lower_bound(t[n].begin(), t[n].end(), val);
const auto pos = std::distance(t[n].begin(), it);
对于std::multiset
,成员类型iterator
和const_iterator
是双向迭代器类型。双向迭代器不支持算术运算符 + 和 -(有关详细信息,请查看 cpp首选项)。
std::distance
可用于计算两个迭代器之间的元素数。
std::distance
使用 operator-
来计算元素数(如果参数是随机访问迭代器)。否则,它将重复使用增加运算符(运算符++)。
这是从 cppreference 稍微更改的代码片段。
#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::multiset
与std::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
相关文章:
- 查找最接近的大于当前数字的数字的索引
- 查找字符在两个索引之间出现的次数
- 如何查找哪个类对象位于数组的特定索引上(多态性)
- 在对向量中查找元素的索引
- 查找索引的最接近的真实值
- C++ char 类查找字符索引
- 保持排序的数据结构,允许log N插入时间,并且可以返回我在log N中查找的元素的索引
- 查找数组中重复(重复)数字的索引
- 查找下一个具有真值C++的数组索引
- 在不进行排序的情况下查找数组中n个最小值的索引
- 在索引0为0的数组中查找非零最小值C++
- 具有 O(1) 查找的 C++ 索引容器
- 在 openCV 矩阵中查找列最大值的索引和值
- 查找数组的第一个和最后一个索引,其中 from 和 to 元素的顺序总和最大
- 查找C++中两个索引之间的子字符串
- 尝试查找从索引 0 开始的偶数长度子字符串
- 在向量C++中查找特定的第一个和最后一个索引<float>
- 如何查找指向数组中元素的指针的索引
- 查找索引数据结构,例如`sTD :: vector`(非数组)
- 多集索引查找