Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


set has it but set.find() won't give me it's goodies!

This topic is 5423 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm running into a strange problem with the std::set container. I inserted an item into this set but when I try to do a find() for this item immediately after it keeps giving me set.end() instead of an iterator to that item's location. I've been looking at this for at least an hour and can't figure out why it's doing that. Anyone have any ideas or what I'm forgetting?
void CWordList::ParseWordFile()
    typedef set<PartOfSpeechType>::const_iterator const_it;
    string currLine;

    m_dictionary.seekg(0, ios::beg);
    PartOfSpeechType buff(string(" "), m_dictionary.tellg(), 0);
    set<PartOfSpeechType>::iterator setpos = m_PartOfSpeechSections.end();

    getline(m_dictionary, currLine);
        if( IsSectionHeading(currLine) )
            buff.sectionName = ExtractHeading(currLine);
            buff.headpos = m_dictionary.tellg();
            buff.wordCount = 0;
            setpos = m_PartOfSpeechSections.find(buff);

            //debugging: check setpos value;

            if(setpos == m_PartOfSpeechSections.end())
                cout << "setpos is end, " << m_PartOfSpeechSections.count(buff) << endl;

            //more debugging: print everything in the set container so far

            for(const_it i = m_PartOfSpeechSections.begin();
                i != m_PartOfSpeechSections.end(); i++)
                cout << i->sectionName << ", " << i->wordCount << endl;

        else if( (strisalpha(currLine) || strisdigit(currLine)) &&
                 setpos != m_PartOfSpeechSections.end() )

        getline(m_dictionary, currLine);


So when it prints all the items out in the set container, buff is definitely in there. When I do a m_PartOfSpeechSections.count(buff) it prints 1 out but when I do a find(buff) on this it gives me m_PartOfSpeechSections.end(). So I'm looking at this like, what the fucking hell? That don't make sense. I did a quick program test on this using a set of ints and that didn't exhibit the problem I'm having here. There's probably some kind of quirk in there or something I'm forgetting to implement in my struct/class that's making this go all screwy. Can anyone shed some light on this? Also, I believe there's a version of set.insert() that returns the iterator location of the inserted item but I can't seem to figure out how to use it exactly. The return type seems to have a template signature of < iterator, bool > but how exactly can this be used with my set< PartOfSpeechType >::iterator or do I use it with something else? Thanks a bunch

--{You fight like a dairy farmer!} [edited by - Greatwolf on January 15, 2004 3:32:07 AM]

Share this post

Link to post
Share on other sites
a) Check that your PartOfSpeechType comparison operator is implemented correctly.

std:: pair<bool, iterator> p = myset.insert(value);
bool successfully_inserted = p.first;
iterator insertion_position = p.second;

std:: pair is declared in <utility>

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan (C programming language co-inventor)

[edited by - Fruny on January 15, 2004 3:37:18 AM]

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!