问题:有没有比如示例所示逐个调用向量更好的方法来循环遍历结构中的所有向量?#
我正在构建一个分类器。有几个类别的项目,每个项目都由一个字符串向量表示。每个矢量将相当小,<100个元素。我已经阅读了以下链接中的示例:
如何查找std::向量中是否存在项?
下面是我正在实现的代码的一个简化示例。代码编译并运行,但对我来说似乎很笨拙。提前谢谢。
#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;
}
您可以保留结构,并将指向每个结构成员的指针存储在列表或向量中。
struct Categories
{
vector <string> cars;
vector <string> food;
};
std::vector<std::vector<string>*> members;
Categories cat;
members.push_back(&cat.cars); // add a pointer to the cars struct member
members.push_back(&cat.food); // add a pointer to the food struct member
现在,您可以通过成员进行迭代,也可以通过结构进行访问。
for(std::vector<string>* member: members)
{
for(string s: *member)
{
}
}
或者,您可以完全放弃结构,使用向量中的向量,这样您就可以迭代"成员",同时仍然允许每个成员有O(1)访问时间。
const int CARS = 0;
const int FOOD = 1;
members[CARS].push_back(car); // add a car
members[FOOD].push_back(food); // add food
// iterate cars
for(string car: members[CARS])
{
// do something
}
尝试使用std::map
using namespace std;
typedef set<string> CategoryType;
struct ThingsType
{
map<string, CategoryType> iLike;
map<string, CategoryType> dontLike;
};
int _tmain(int argc, _TCHAR* argv[])
{
ThingsType things;
things.iLike['cars'].insert("Mustang");
things.iLike['cars'].insert("Pinto");
things.iLike['food'].insert("Squash");
string item("Pinto");
// I want to loop over all vectors searching for items
for(map<string, CategoryType>::const_iterator i = things.iLike.begin(); i != things.iLike.end(); i++) {
if (i->second.find(item) != set::end)
cout << "I like " << item << endl;
}
for(map<string, CategoryType>::const_iterator i = things.dontLike.begin(); i != things.dontLike.end(); i++) {
if (i->second.find(item) != set::end)
cout << "I don't like " << item << endl;
}
return 0;
}
我认为std::unordered_set
在这种情况下会更好。
#include<unordered_set>
struct Categories{
std::unordered_set <std::string> cars;
std::unordered_set <std::string> food;
};
struct ThingsType{
Categories iLike;
Categories dontLike;
};
int main(){
std::unordered_set<std::string>::iterator result[10];
ThingsType things;
things.iLike.cars = { "Mustang", "Pinto" };
things.dontLike.food = { "Squash" };
string item("Pinto");
result[0] = things.iLike.cars(item);
result[1] = things.iLike.food(item);
// . . .
result[9] = things.dontLike.food(item);
}