Archived

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

Greatwolf

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

Recommended Posts

Greatwolf    125
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.clear();
    m_dictionary.seekg(0, ios::beg);
    PartOfSpeechType buff(string(" "), m_dictionary.tellg(), 0);
    set<PartOfSpeechType>::iterator setpos = m_PartOfSpeechSections.end();

    getline(m_dictionary, currLine);
    while(!m_dictionary.eof())
    {
        if( IsSectionHeading(currLine) )
        {
            buff.sectionName = ExtractHeading(currLine);
            buff.headpos = m_dictionary.tellg();
            buff.wordCount = 0;
            m_PartOfSpeechSections.insert(buff);
            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;
        }//if

        else if( (strisalpha(currLine) || strisdigit(currLine)) &&
                 setpos != m_PartOfSpeechSections.end() )
            setpos->wordCount++;

        getline(m_dictionary, currLine);
    }//while

}//ParseWordFile

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
Fruny    1658
a) Check that your PartOfSpeechType comparison operator is implemented correctly.

b)
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