Thread: Implementation of set class in C++

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Well, if you used find member function of string then I suggest something like this:
    Code:
    void set_intersection(string stringSet, string otherset)
    {
        for(int i = 0; i < otherset.length(); i++)
        { 
           if((stringSet.find( otherset[i] ) != string::npos) && 
                (result.find(otherset[i]) == string::npos))
           result =result + otherset[i];
           commElmts = result;
        }
     cout<< commElmts<<endl;
    };
    P.S.
    I wouldn't use global variables, but that is up to you!
    Last edited by Micko; 09-20-2006 at 11:27 PM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >How do I ensure that a commom element is only outputted once?
    You're faking the STL algorithm, right? What it does is a merge, where only matching items are copied to the destination. Your algorithm doesn't do the same thing, and you'll have a hard time emulating that functionality with string::find. The actual algorithm would be implemented something like this:
    Code:
    template<typename In1, typename In2, typename Out>
    Out set_intersection(In1 first1, In1 last1, In2 first2, In2 last2, Out dst)
    {
      while ( first1 != last1 && first2 != last2 ) {
        if ( *first1 < *first2 )
          ++first1;
        else if ( *first2 < *first1 )
          ++first2;
        else {
          *dst++ = *first1++;
          ++first2;
        }
      }
    
      return dst;
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    9
    is there a method without using pointers?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    9

    Question Template of definition

    I am only passing is 2 strings and want to return a third string, that is the intersection of the 2 strings that were passed in.
    something like what i have below:


    Code:
    StringGroup StringGroup::set_intersection(StringGroup otherGroup)
    {
    
    
    
    
    
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  5. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM