C++ 指定索引的范围
在函数中,我有一个while循环,如下所示:C++ 指定索引的范围,c++,C++,在函数中,我有一个while循环,如下所示: void unpack(std::vector<std::string>& leftcol, std::vector<std::string>& rightcol, std::vector<std::string> origv, std::string perm) { .... while(perm != origv[0] && perm != origv[1])
void unpack(std::vector<std::string>& leftcol, std::vector<std::string>& rightcol, std::vector<std::string> origv, std::string perm) {
....
while(perm != origv[0] && perm != origv[1])
....
}
我不理解您描述的约束,但是测试对象是否与集合的元素进行比较的方法是使用
std::find
:
while (std::find(origv.begin(), origv.end(), perm) == origv.end()) {
// perm not found
}
那么
leftcol
和rightcol
呢?他们是scaring@P0W. 它们在循环退出之前不会被使用,所以不要介意这些。更清楚的是,您需要类似于的东西,而(perm不在origv中){do something}
,对吗?@Kaiged。是的,这是正确的。@Dochevsky,Pete Becker比我快,但我认为他的答案是你想要的。基本上,我正在修改perm
,直到它匹配向量的一个字符串元素。向量中只有一个元素经过多次修改后能正确匹配。无论如何,你的方法是有效的。谢谢。@Dochevsky请确保您没有@Dochevsky-那么,在循环中您正在修改perm
?这是有道理的。
while (std::find(origv.begin(), origv.end(), perm) == origv.end()) {
// perm not found
}