Hi
Problem: I have a large text string and within that large text string I need to look for 30 different words to see if they are part of that text string. And of the 30 target words that are in the text string I need to order them as to which came first, second, third,..., last etc...
Ideally there would be a substring library that returned an integer value to where in the large text string the target string(s) was found and I could do a sort based on the return value(s). But it looks like the only substring library in C returns a pointer to where the substring was found.
Example:
"I have three pets, a dog a cat and a pig"
In latter case you have wide array of choices from std::sring's find member (which returns array subscript) to std::search algorithm which operaes on and returns iterators.
I need to order them as to which came first, second, third,..., last etc...
...
it looks like the only substring library in C returns a pointer to where the substring was found.
Why not sort pointers themselves? They have a total ordering if they belong to same array.
Exactly same way as you would compare two integers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include <cstring>
int main()
{
constchar* string = "A dog is chasing cat";
constchar* cat = strstr(string, "cat");
constchar* dog = strstr(string, "dog");
if(!(cat && dog)) {
std::cerr << "One of the words was not found\n";
return 1;
}
if(cat < dog)
std::cout << "dog comes after cat\n";
else
std::cout << "dog comes before cat\n";
}
use a std::map , it is like a dictionary . Combine it with std::algorithm and it should work fine . I am assuming that you have a text and that you want to find indexation of words .