for (std::vector<const std::string>::const_iterator it = serverList.begin(); it != serverList.end(); it++)
{
// found a match, store the location
if (index == *it) // index is a string
{
indexResult.push_back(std::distance(serverList.begin(), it)); // std::vector<unsigned int>
}
}
我编写了上面的代码,用于查找字符串向量并返回另一个向量,其中包含任何“命中”的位置。有没有更快的方法来做同样的事情?(如果容器中有10,000个项目,那么需要一段时间)。 请注意,我必须检查所有项目是否匹配,并将其位置存储在容器中。
额外奖励:谁知道如何/在哪里可以搜索部分结果的方法/链接(例如:搜索“coolro”并存储变量“coolroomhere”的位置)
i
,而是使用std::distance(serverList.begin(), it)
;或者直接通过索引访问向量。 - Kerrek SB