string.find函数返回奇数

时间:2014-04-23 00:43:33

标签: c++ string

我试图找到找到角色的位置。

const char* normalize(std::string path) 
{
    std::cout << "executed   " << path << std::endl;
    //"foo//\\\bar////bar2///../.\bar2" -- foo/bar/bar2
    std::size_t found;
    std::size_t found2;
    std::size_t curchar = 0;
    std::string final;
    std::string buffer;
    bool notdone = true;

    while (notdone) {
        //std::cout << "loop" << std::endl;
        //find the current element
        // can be / or \
        found = path.find("/", curchar);
        found2 = path.find("\\",curchar);
        std::cout << found << std::endl;
        SDL_Delay(2000);
        if (found != std::string::npos && found2 != std::string::npos) {
            if (found < found2){
                //read from the curchar to the slash
                if (curchar-found > 1){
                    buffer = path.substr(curchar,found-curchar-1);
                    //add to path
                    final = final + "/" + buffer;
                }
                curchar = found+1;
                //buffer will be the file/component
            }else{
                if (curchar-found2 > 1){
                    buffer = path.substr(curchar,found2-curchar-1);
                    //add to path
                    final = final + "/" + buffer;
                }
                curchar = found2+1;
            }
        }else if(found != std::string::npos){
            //std::cout << "loop2" << found == std::string::npos << std::endl;
            //std::cout << "loop2   " << path.substr(curchar, 1) << std::endl;
            if (curchar-found > 1){//
                buffer = path.substr(curchar,found-curchar-1);
                //add to path
                final = final + "/" + buffer;
            }
            curchar = found+1;
        }else if(found2 != std::string::npos){
            std::cout << "loop3" << std::endl;
            if (curchar-found2 > 1){
                buffer = path.substr(curchar,found2-curchar-1);
                //add to path
                final = final + "/" + buffer;
            }
            curchar = found2+1;
        }else{
            std::cout << "finishing" << std::endl;
            final = final + "/" + path.substr(curchar,path.size()-curchar);
            notdone = false;
        }
    }

    return final.c_str();
}

normalize("test/");

此代码应打印出来&#39; 4&#39;,而是打印出18.它在无限循环中打印出18。但是,如果我使用std::cout << path.find("/", curchar) << std::endl,它会打印4.首先我认为它实际上并没有返回std::size_t,但我检查过它。

1 个答案:

答案 0 :(得分:3)

您的以下行正在创建问题

  //find the current element
  // can be / or \
  found = path.find("/", curchar);

我在我的linux终端上运行,GCC被视为下一行作为上述行的评论的延续。

basic.cpp:18:9: warning: multi-line comment [-Wcomment]
         // can be / or \
         ^
basic.cpp: In function ‘const char* normalize(std::string)’:
basic.cpp:21:22: warning: ‘found’ may be used uninitialized in this function [-Wmaybe-uninitialized]
         std::cout << found << std::endl;
                      ^

现在由于上述评论风格,您的下一行(代码)被视为评论。由于发现没有被初始化所以它有垃圾值,这搞砸了你的逻辑,因为它没有进入你重置标志 notdone 的路径。

然而,GCC或任何其他编译器应该发出警告(使用uninitialize变量),如果我们仔细阅读,我们可能会回溯并理解问题。

解决方法是将评论样式更改为

/* // can be / or \ */