Hi,
Say you have a program using multiple vectors, and you want to search through them all for individual items. What would be the best way to declare/ use the vectors? In the following program I have two vectors of strings, nouns and verbs. I then search through them both for a word the user enters.
But say if you had loads of different vectors for things like adjectives, adverbs, prepositions, etc. Would it be better to declare the vectors as part of a multi-dimensioned vector? And would that mean you didn't need to write as much code searching through them all? ie you could just use find once?
Thanks.
Code:#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; const char *v[4] = {"run", "scream", "jump", "procrastinate"}; vector<string> verbs(v, v+4); const char *n[4] = {"car", "wall", "dog", "basketball"}; vector<string> nouns(n, n+4); vector<string>::iterator iter; int main() { string string1; cout << "Enter a word: "; cin >> string1; if (find (nouns.begin(), nouns.end(), string1) != nouns.end()) cout << string1 << " is a noun."; else if (find (verbs.begin(), verbs.end(), string1) != verbs.end()) cout << string1 << " is a verb."; else cout << "word not found."; }



LinkBack URL
About LinkBacks



