KMP algorith with a vector

In the following program I have a three class hierarchy. Item is the base class and Product and Service inhererit from it. I want to implement the KMP algorithm and find the items with a certain string in their name. How do I do that when the items are in a vector?

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
 int main() {

vector<Category*> categories;


categories.push_back(new Category("AA-34", "Pasta", "Italian"));        
categories.push_back(new Category("BB-65", "Wine", "French"));
categories.push_back(new Category("CC-120", "Pizza", "Italian"));


vector<Item*> items;                                                                     
Product p1("DD-54", "Spaghetti", "Bolognese", 7.50, 3, amount, 1, *categories[0]);       
Product p2("EE-25", "Cabernet", "Sauvignon", 8.20, 5.0, percentage, 2, *categories[1]);
Product p3("FF-99", "Capricciosa", "Ricetta", 5.50, 2, amount, 1, *categories[2]);

Service s1(1.50, 30.50, 1.50, amount);                                                    
Service s2(2.0, 15.0, 1.0, percentage);
Service s3(2.20, 10.50, 1.0, amount);

//adding the elements to the items vector 
items.push_back(&p1);
items.push_back(&p2);
items.push_back(&p3);

items.push_back(&s1);
items.push_back(&s2);
items.push_back(&s3);
} 
Start by writing the KMP algorithm. Test it. Then call it with your specific data.

1
2
3
// Search for the first occurance of word within text. Returns the position in text,
// or string::npos if not found
size_t kmp(const string &text, const string &word);
Topic archived. No new replies allowed.