Thread: if what?

  1. #1
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117

    if what?

    Code:
    	for(string new_word; cin >> new_word, new_word!="Quit"; )
    	{
    		const pair<WordIter, bool>  &trace = words.insert(new_word);
    		if (trace.second)
    			input_order.push_back(trace.first);
    	}
    hmm.. Im having trouble with that if statement..
    if(trace.second) what does this ask?
    if pointing at trace.second switch to first to put new_word in trace.first?

    Or am I lost?

    thx in advance..
    Luigi

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    if trace.second doesn't equal 0 (or if it equals 1?) do whatever.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by orbitz
    if trace.second doesn't equal 0 (or if it equals 1?) do whatever.
    The first answer is correct. Any "non zero value" is considered true in such an expression.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117
    OK here is the full code:

    Code:
    #include <algorithm>
    #include <iostream>
    #include <list>
    #include <set>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	typedef set<string> WordSet;
    	typedef WordSet::iterator WordIter;
    	typedef list<WordIter> Index;
    	WordSet words;
    	Index input_order;
    	// Input the words uniquely
    	for(string new_word; cin >> new_word, new_word!="Quit"; )
    	{
    		const pair<WordIter,bool>  &trace = words.insert(new_word);
    		if (trace.second)
    			input_order.push_back(trace.first);
    	}
    	// Output unique words in order of input:
    	cout << "\n>>> Unique words in input order:\n";
    	for (Index::iterator p = input_order.begin(); p!=input_order.end(); ++p)
    	{
    		cout << *(*p) << '\n';
    	}
    	// Output unique words in default set<string> order:
    	cout << "\n>>> Unique words in sorted order:\n";
    	copy(words.begin(), words.end(), ostream_iterator<string>(cout, "\n"));
    	return 0;
    }
    this is code i found "somewhere"..
    i figured almost evrything out of it but 1 thing..
    why is he using pair?
    why does he needs a bool there?

    Argh!

    im confused...

    Luigi

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    In my response to the message when posted previously I indicated that from what I can find the insert() method class set in STL returns type void, not pair. Therefore, I don't think his code will work, and I wrote an alternative for him/her. The original objective, as far as I can see, is to determine if new_word was inserted into the set or not. If it was then the bool member of trace will be true, if not then false. set objects guarantee uniqueness among members. Therefore if the string "happy" was already a member of the set and it was re-encountered the second instance would not be inserted into the the set. In addition to uniqueness, set objects must also be ordered in some fashion, the default fashion being in ascending order, though that can be changed (or so I have read). The ordering of the members of the set may or may not be chronological depending on the source of the objects being sent to insert(). Therefore, the author wanted to maintain the input_order as well as the set itself. Since duplicates won't be entered into the set, if a duplicate was encountered, then it shouldn't appear in the input_order container either. Hence the desire for the bool member of trace, which is declared as a reference to a pair (which is another concern to me, but that's a different story). pair is pre-defined STL class that has two members, referred to as first and second, with obvious implications.

Popular pages Recent additions subscribe to a feed