set?

This is a discussion on set? within the C++ Programming forums, part of the General Programming Boards category; what is wrong with the following? Code: #include <algorithm> #include <iostream> #include <list> #include <set> #include <string> using namespace std; ...

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

    set?

    what is wrong with the following?

    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;
    	
    	for (string new_word; cin >> new_word, new_word != "Quit";)
    	{
    		pair<WordIter, bool> &trace = words.insert(new_word);
    		if (trace.second)
    			input_order.push_back(trace.first);
    	}
    	
    	copy(input_order.begin(), input_order.end(),
    		ostream_iterator<string>(cout, "\n"));
    	copy(words.begin(), words.end(),
    		ostream_iterator<string>(cout, "\n"));
    	return 0;
    }
    when I try to compile I get an error at line 18...
    Illegal Implicit conversion

    why? I dont get it..


    Im using code warior 7.0 on mac os X..

    Luigi
    thx..

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    with all those unnecessary typedefs you must be looking forward to a career in Windows@ programming. 8-)

    If I counted right line 18 is this:

    pair<WordIter, bool> &trace = words.insert(new_word);

    In the future you're likely to get better quality and more abundant answers if you actually flag/highlight the offending line for anyone willing to look at the problem.

    The potential problem I have with this line is that I believe the insert method for STL sets returns void. Therefore trying to assign type void to a reference to a pair is an implicit conversion.

    Here's a slightly different function to accomplish what I think you were trying to do, words will be in "ascending" order from smallest string to largest input_order will maintain the chronological sequence of the string as they are inserted into the set, irrespective of the size of of the string.

    for (string new_word; cin >> new_word, new_word != "Quit"
    {
    if(count(new_word) == 0)
    {
    words.insert(new_word);
    input_order.push_back(new_word);
    }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 01-18-2008, 04:06 AM
  2. The new FAQ
    By Hammer in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 08-30-2006, 11:05 AM
  3. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 05:10 AM
  4. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 12:14 PM
  5. Set default directory with GetTempDir?
    By Bajanine in forum Windows Programming
    Replies: 2
    Last Post: 05-05-2003, 12:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21