help me with some idea...

This is a discussion on help me with some idea... within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by laserlight No. Here is an example of a wrapper: Code: class StringSet { public: // inserts value ...

  1. #16
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    Quote Originally Posted by laserlight
    No. Here is an example of a wrapper:
    Code:
    class StringSet
    {
    public:
        // inserts value to contents
        void insert(const std::string& value)
        {
            contents_.insert(value);
        }
    
        // returns true if string set contents contains value
        bool contains(const std::string& value) const
        {
            return contents_.find(value) != contents_.end();
        }
    private:
        std::set<std::string> contents_;
    };
    Of course, you would have your set_intersection(), set_union() and print() member functions implemented in terms of the std::set<std::string> member variable, possibly using the std::set_intersection() and std::set_union generic algorithms that I demonstrated in the other thread.

    and also i need to #include <algorithm> in my stringset.h right?

  2. #17
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    how could i print those while compile?

    Code:
    // print
    // Pre: None
    // Post: the stringset will be printed to cout
    void StringSet::print()
    {
        for( int i=0; i<contents.size(); i++ ) 
            cout << "\t" << contents[i] << endl;
    }

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,634
    and also i need to #include <algorithm> in my stringset.h right?
    You only need to #include <set> and <string> in stringset.h
    You should #include <algorithm> in stringset.cpp

    The reason being that std::set_intersection and std::set_union never need to be visible to the user of the StringSet class, but std::set and std::string need to be in the class definition (to declare the member variable).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    thank you very much " laserlight " ......

  5. #20
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    Quote Originally Posted by joni
    Have you tried it?

    Is your class called stringset or StringSet? You use both.

    The function is declared to return StringSet but you don't have a "return" statement.
    hi Joni,

    is this the correct way ??

    Code:
    StringSet StringSet::set_intersection(StringSet otherset)
    {
        StringSet result;
    
        for(int i = 0; i < contents.size(); i++)
        {
            if(otherset.contains(contents[i]))
                result.insert(contents[i]);
        }   
        return result;   
    }
    and can someone or JONI please correct my union code...

    Code:
    StringSet StringSet::set_union(StringSet otherset)
    {
        StringSet result;
        
        for(int i = 0; i < contents.size(); i++)
        {
            if( otherset.contains(contents[i]) )
                result.insert( contents[i] );
            if( !otherset.contains(contents[i]) )
                result.insert( contents[i] );
        }   
        return result;
    }
    Last edited by peter_hii; 09-28-2006 at 06:27 AM.

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,634
    is this the correct way ??
    Yes, but since your sets are not ordered, you are looking at order n^2 performance.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    Quote Originally Posted by laserlight
    Yes, but since your sets are not ordered, you are looking at order n^2 performance.
    so you mean i need to sort the contents first ????

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,634
    so you mean i need to sort the contents first ????
    It is not needed, but it may be more efficient. If you make your class a wrapper of std::set<std::string> as I suggested, then you get this optimisation for free since std::set keeps its elements in sorted order.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    The union of two sets is the set that contains all of the elements in the two sets. You can construct the union by first making an empty set and then inserting into it all elements from one set and then all elements from the other set.

  10. #25
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    Quote Originally Posted by joni
    The union of two sets is the set that contains all of the elements in the two sets. You can construct the union by first making an empty set and then inserting into it all elements from one set and then all elements from the other set.
    instead of storing them in the result...i need to create a new set right??

    Code:
    StringSet StringSet::set_union(StringSet otherset)
    {
        StringSet UnionResult;
        
        for(int i = 0; i < contents.size(); i++)
        {
            UnionResult.insert( contents[i] );
            if( !UnionResult.contains( contents[i] )
                UnionResult.insert( contents[i] );
        }   
        return UnionResult;
    }
    am i right with this joni?
    Last edited by peter_hii; 09-28-2006 at 11:12 PM.

  11. #26
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> am i right with this joni?
    Not quite, you forgot to use otherset.

  12. #27
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    Quote Originally Posted by Daved
    >> am i right with this joni?
    Not quite, you forgot to use otherset.
    Code:
    StringSet StringSet::set_union(StringSet otherset)
    {
        StringSet UnionResult;
        
        for(int i = 0; i < contents.size(); i++)
        {
            UnionResult.insert( contents[i] );
            if( !UnionResult.contains( contents[i] ) && !otherset.contains( contents[i] ) )
                UnionResult.insert( contents[i] );
        }   
        return UnionResult;
    }

    is this cool?

  13. #28
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    You don't seem to be getting what the union of two sets really is...

    The union contains all of the elements from both of the sets. Therefore the union is easily constructed by inserting all of the elements from both of the sets. For example if the two sets are {1,2,3,4} and {5,6,7}, their union is {1,2,3,4,5,6,7}.

    Note that you already check for duplicates in the insert method so you don't need to worry about them now.

  14. #29
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    Quote Originally Posted by joni
    You don't seem to be getting what the union of two sets really is...

    The union contains all of the elements from both of the sets. Therefore the union is easily constructed by inserting all of the elements from both of the sets. For example if the two sets are {1,2,3,4} and {5,6,7}, their union is {1,2,3,4,5,6,7}.

    Note that you already check for duplicates in the insert method so you don't need to worry about them now.
    first of all.. i need to thank you joni and daved.... really appreicated lot ...
    so this is my final solution....

    Code:
    StringSet StringSet::set_union(StringSet otherset)
    {
        StringSet UnionResult;
        
        for(int i = 0; i < contents.size(); i++)
        {
            UnionResult.insert( contents[i] );
            if( !UnionResult.contains( contents[i] ))
                UnionResult.insert( contents[i] );
                UnionResult.inser( otherset[i] );
        }   
        return UnionResult;
    }
    am i right with the code?

  15. #30
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,634
    That does not look right. What I suggest is that you copy over the contents of one set to the result set (no need to check for duplicates, hence no need to use insert), and then use the insert member function to insert the contents of the other set into the result set.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. project idea ???
    By gemini_shooter in forum C Programming
    Replies: 2
    Last Post: 06-09-2005, 09:56 AM
  2. Have an idea?
    By B0bDole in forum Projects and Job Recruitment
    Replies: 46
    Last Post: 01-13-2005, 02:25 PM
  3. A little problem about an idea, help please
    By louis_mine in forum C++ Programming
    Replies: 3
    Last Post: 09-10-2004, 09:52 PM
  4. totally screwed up idea
    By iain in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 08-17-2001, 12:09 PM

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