【发布时间】: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))
【问题讨论】:
-
你应该在...第二个问题中问你的第二个问题。