我有一個字符串的向量。我希望能夠搜索該矢量的字符串,如果我在矢量中找到匹配,我希望能夠返回該位置,例如矢量中項目的矢量索引。我可以讀取矢量<string> ::迭代器的數字位置嗎?
這裏是我試圖解決該問題的代碼:
enum ActorType { at_none, at_plane, at_sphere, at_cylinder, at_cube, at_skybox, at_obj, at_numtypes };
class ActorTypes
{
private:
std::vector<std::string> _sActorTypes;
public:
ActorTypes()
{
// initializer lists don't work in vs2012 :/
using std::string;
_sActorTypes.push_back(string("plane"));
_sActorTypes.push_back(string("sphere"));
_sActorTypes.push_back(string("cylinder"));
_sActorTypes.push_back(string("cube"));
_sActorTypes.push_back(string("skybox"));
_sActorTypes.push_back(string("obj"));
}
const ActorType FindType(const std::string & s)
{
auto itr = std::find(_sActorTypes.cbegin(), _sActorTypes.cend(), s);
uint32_t nIndex = ???;
// I want to be able to do the following
return (ActorType) nIndex;
}
};
我知道我可以只寫一個for循環,並返回了,我覺得這場比賽在循環索引,但我想知道對於更一般的情況 - 我可以得到vector :: iterator的索引值嗎?
'參數nIndex = ITR - _sActorTypes.cbegin();' – ildjarn