C++ if()跳过我的变量检查
我有以下代码:C++ if()跳过我的变量检查,c++,C++,我有以下代码: std::vector<std::string> GetSameID(std::vector<string>& allFiles, int id) { std::vector<std::string> returnVector; for(std::vector<string>::iterator it = allFiles.begin(); it != allFiles.end(); ++it) {
std::vector<std::string> GetSameID(std::vector<string>& allFiles, int id) {
std::vector<std::string> returnVector;
for(std::vector<string>::iterator it = allFiles.begin(); it != allFiles.end(); ++it) {
if(GetID(*it) == id) {
int index = (*it).find("_CH2.raw");
if(index > 0) {
continue; //this works
}
if(0 < ((*it).find("_CH2.raw"))) {
continue; //this doesn't
}
string ext = PathFindExtension((*it).c_str());
if(ext == ".raw") {
returnVector.push_back(*it);
}
}
}
return returnVector;
}
std::vector GetSameID(std::vector&allFiles,int-id){
std::向量返回向量;
对于(std::vector::iterator it=allFiles.begin();it!=allFiles.end();++it){
if(GetID(*it)=id){
int index=(*it.find(“_CH2.raw”);
如果(索引>0){
continue;//这是有效的
}
如果(0<(*it.find(“\u CH2.raw”)){
continue;//这不正确
}
字符串ext=PathFindExtension((*it.c_str());
如果(ext==“.raw”){
返回向量。将_向后推(*it);
}
}
}
返回向量;
}
我的问题是,为什么if(0<((*it.find(“_CH2.raw”))
不是这样工作的?我的文件被命名为
ID_0_X_0_Y_128_CH1.raw
ID_0_X_0_Y_128_CH2.raw
(示波器上的通道1和通道2具有不同的ID,X和Y)
当我做了很长一段时间(分配索引,然后检查索引)后,它工作了,我不明白为什么短版本(在我看来更可读)不工作。根据,string::find()
返回一个size\u t
——这是一个无符号类型——因此它永远不能小于零
当它找不到某个东西时,它会返回
string::npos
,这也是一种无符号类型,但当您将其推入int(隐式转换)时,它会变成负值--这就是第一组代码工作的原因。您可能想尝试它->find()
-这不是问题,只是一个建议。也许我没有完全理解这一点。第二个检查不是真的吗?@thefourtheyeif(0<((*it).find(“\u CH2.raw”){continue;}总是返回false,不管CH2是否在字符串中。结果是比较无符号整数和有符号整数的问题。不太合适,这是意料之中的。我对“this works”和“this not”的注释感到困惑。@AntonRoth关于大小,索引的类型实际上应该是auto
(对于C++11)或std::string::size\u type
。