Originally Posted by laserlight
and also i need to #include <algorithm> in my stringset.h right?
Originally Posted by laserlight
and also i need to #include <algorithm> in my stringset.h right?
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; }
You only need to #include <set> and <string> in stringset.hand also i need to #include <algorithm> in my stringset.h right?
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).
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
thank you very much " laserlight " ......
hi Joni,Originally Posted by joni
is this the correct way ??
and can someone or JONI please correct my union code...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; }
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.
Yes, but since your sets are not ordered, you are looking at order n^2 performance.is this the correct way ??
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
so you mean i need to sort the contents first ????Originally Posted by laserlight
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.so you mean i need to sort the contents first ????
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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??Originally Posted by joni
am i right with this joni?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; }
Last edited by peter_hii; 09-28-2006 at 11:12 PM.
>> am i right with this joni?
Not quite, you forgot to use otherset.
Originally Posted by DavedCode: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?
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 ...Originally Posted by joni
so this is my final solution....
am i right with the code?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; }
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)