Jay Kim
Jay Kim

Reputation: 843

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. 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"?

I tried:

itLow = fooset.lower_bound("string");

I know that's not sufficient...but I'm not sure how to do it.

Thanks!

Upvotes: 0

Views: 640

Answers (1)

Jerry Coffin
Jerry Coffin

Reputation: 490653

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:

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());

Upvotes: 1

Related Questions