Question: Is there a better way to loop through all the vectors in the structure than by calling them one by one as shown in the example? #
I am building a classifier. There are several categories of items each represented by a vector of string. Each of the vectors will be fairly small, < 100 elements. I have read the examples in the following link:
How to find if an item is present in a std::vector?
Here is a simplified example of the code that I am implementing. The code compiles and runs but seems clunky to me. Thanks in advance.
#include "stdafx.h"
#include <vector>
#include <string>
#include <iostream>
using namespace std;
struct Categories
{
vector <string> cars;
vector <string> food;
};
struct ThingsType
{
Categories iLike;
Categories dontLike;
};
typedef vector<string>::const_iterator vIter;
int FindValue(vector<string>& vec, string keyWord)
{
int indx = -1;
vIter iter = find(vec.begin(), vec.end(), keyWord);
if (iter != vec.end()){
// found it
std::cout << "Value: " << *iter << " found in location: " << iter - vec.begin() << endl;
indx = iter - vec.begin();
}
else
{
// did not find it.
std::cout << "Value: " << keyWord << " not found." << endl;
}
return indx;
}
int _tmain(int argc, _TCHAR* argv[])
{
int result[10];
ThingsType things;
things.iLike.cars = { "Mustang", "Pinto" };
things.dontLike.food = { "Squash" };
string item("Pinto");
// I want to loop over all vectors searching for items
result[0] = FindValue(things.iLike.cars, item);
result[1] = FindValue(things.iLike.food, item);
// . . .
result[9] = FindValue(things.dontLike.food, item);
return 0;
}