| | |
Finding the union of two strings
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2006
Posts: 16
Reputation:
Solved Threads: 0
I am programming in C++ and am attempting to find the intersection of two strings, only the items common to both and thus return a third string with these items. Essentially, I am trying to write
the code for the STL library set_intersection.
To approach this problem I thought of I can use two nested loops one loop to cycle thru each string. Then having a variable to compare values in common, which means I would
have to have a temp variable to store common elements.
something similar to this:
Any further sugestions would be great,
Thanks,
I
the code for the STL library set_intersection.
To approach this problem I thought of I can use two nested loops one loop to cycle thru each string. Then having a variable to compare values in common, which means I would
have to have a temp variable to store common elements.
something similar to this:
C++ Syntax (Toggle Plain Text)
string names[] = {"AMC", "ANZ", "AMX"}; for( int i = 0; i < 3; i++ ) { for( int j = 0; j < 3; j++ ) { cout << names[i].compare(names[j] ) << " "; } cout << endl; }
Thanks,
I
the compare() function is not what you need. It only tells you whether one string is the same as the other. you need to check the two strings one character at a time and create a third string that contains the characters which are in both strings. For example, the first two strings have the letters 'A' and 'M' in both strings, so a third string will contain "AM".
you can use a loop. Something like below. The find() method will search one string for an instance of another string and return the position where its at.
C++ Syntax (Toggle Plain Text)
string str1 = "ABC"; string str2 = "BCD"; string result; for(int i = 0; i < str2.length(); ++) { if( str1.find( str2[i] ) != string::npos ) result += str2[i]; }
now you are asking a completly different question. vectors of what? strings? do you want to create a vector that is a union of two other vectors, where each string in the new vector is the union of the corresponding strings in two or more other vectors? The algorithm would be almost identical to what has already been posted.
![]() |
Similar Threads
- fopen: No such file or directory (*nix Software)
Other Threads in the C++ Forum
- Previous Thread: Buffering
- Next Thread: adding a '\' to a CString object
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets







:rolleyes: