在stl集合中设置lower_bound
setting lower_bound in stl set
我想为多组结构设置lower_bound和upper_bound,以便在一个范围内迭代。如何为字符串正确设置它?
#include ...
...
struct foo{
int bar;
string test;
};
struct comp{
inline bool operator()(const foo& left,const foo& right){
return strcasecmp(left.test.c_str(), right.test.c_str());
}
};
int main(){
std::multiset<foo,comp> fooset;
std::multiset<foo,comp>::iterator it, itLow;
...//insert into fooset
//how do set lower_bound to element where string is "aab" or whatever?
return 0;
}
如何将itLow设置为指向字符串测试以"ab"开头的元素?
我试过了:
itLow = fooset.lower_bound("string");
我知道这还不够。。。但我不知道该怎么做。
谢谢!
您需要从字符串中构造一个foo
,然后使用lower_bound
(或upper_bound
,视情况而定)来搜索位置:
struct foo {
int bar;
string test;
foo(string i) : test(i) {}
};
std::multiset<foo, comp> fooset;
std:multiset<foo,comp>::iterator it =
std::lower_bound(fooset.begin(), fooset.end(), foo("string"), comp());
相关文章:
- 空集"Out of bound iterator"
- 将张量的"lower diagonal"映射到矩阵,作为将矩阵的下三角形部分提取到向量的推广
- 为什么<<低;和 cout<<v.begin;单独给出错误,但 cout <<(lower.begin());没有给出任何错误,并使代码易于编译
- 使用多个文件时"array bound is not an integer constant before ']' token"
- "A reference may be bound only to an object",为什么"const int &ref = 3;"有效?
- 如何修复"reference cannot be bound to dereferenced null pointer"警告
- 当你有一个像 ::lower 这样的参数时,这在C++中是什么意思
- GCC 检测"subindex out of bound"错误
- 如果我没有得到分段错误,如何找到"index out of bound"
- 一个程序将字符串Upper char更改为Lower char得到错误
- std:: lower的行为在不同的语言环境中是如何变化的?
- 在main中声明Array之前,Array bound不是整型常量
- 获取错误"array bound is not an integer constant before ']' token"
- 如何在C或c++中获得与Java中的toLowerCase或Python中的string.lower()相同的结果
- 在运行时创建结构时"array bound is not an integer constant"