1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
// Example program
#include <iostream>
#include <cstring>
#include <set>
#include <algorithm>
struct lex_compare {
bool operator() (const char* lhs, const char* rhs) const {
return std::strcmp(lhs, rhs) < 0;
}
};
int main()
{
char text_A[512] = "On an exceptionally hot evening early in July a young man came out of the garret in which he lodged";
char text_B[512] = "There was complete silence under the arcade except for the cooing of doves in the garden below";
char* token;
std::set<const char*, lex_compare> word_set_A;
std::set<const char*, lex_compare> word_set_B;
token = strtok(text_A, " ,.");
while (token) {
word_set_A.insert(token);
token = strtok(nullptr, " ,.");
}
token = strtok(text_B, " ,.");
while (token) {
word_set_B.insert(token);
token = strtok(nullptr, " ,.");
}
const char* common_words[512];
auto it = std::set_intersection(word_set_A.begin(), word_set_A.end(),
word_set_B.begin(), word_set_B.end(),
common_words, lex_compare());
int size = it - common_words;
std::cout << "Common words: ";
for (int i = 0; i < size; i++)
{
std::cout << common_words[i] << " ";
}
std::cout << std::endl;
}
|