简体   繁体   English

在STL集中设置lower_bound

[英]setting lower_bound in stl set

I want to set the lower_bound and upper_bound for a multiset of structs to iterate through a range. 我想为多个结构体设置lower_bound和upper_bound以遍历一个范围。 How do I set it correctly for strings? 如何正确设置字符串?

#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;
}

How can I set itLow to point to the element with string test starting with "ab"? 如何设置itLow指向以“ ab”开头的字符串测试的元素?

I tried: 我试过了:

itLow = fooset.lower_bound("string");

I know that's not sufficient...but I'm not sure how to do it. 我知道那还不够...但是我不确定该怎么做。

Thanks! 谢谢!

You need to construct a foo from the string, then use lower_bound (or upper_bound , as he case may be) to search for the position: 您需要根据字符串构造一个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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM