Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ isSubstringOf()的哪个方法更有效?_C++_String_Algorithm_Substring_Std - Fatal编程技术网

C++ isSubstringOf()的哪个方法更有效?

C++ isSubstringOf()的哪个方法更有效?,c++,string,algorithm,substring,std,C++,String,Algorithm,Substring,Std,请您看一下这两段代码,它们达到了相同的效果: 其他人的解决方案: bool hasSubstring(const char *word, const char *container) { if (container[0] == '\0' || word[0] == '\0') return false; for(int i = 0; container[i] != '\0'; i++) { bool foundNonMatch = false

请您看一下这两段代码,它们达到了相同的效果:

其他人的解决方案:

bool hasSubstring(const char *word, const char *container) {

    if (container[0] == '\0' || word[0] == '\0')
        return false;

    for(int i = 0; container[i] != '\0'; i++) {

        bool foundNonMatch = false;
        for(int j = 0; word[j] != '\0'; j++) {

            if (container[i + j] != word[j]) {
                foundNonMatch = true;
                break;
            }

        }

        if (!foundNonMatch)
            return true;
    }

    return false;
}
bool isSubstringOf(string word, string container) {

    bool success = false;       

    // if either is empty, automatically return false 
    if (!word.empty() && !container.empty()) {

        // loop through the container and while not successful
        for (unsigned i = 0; i < container.size() && !success; i++) {

            // if the first letter of the word is found in the container...
            if (word.at(0) == container.at(i)) {                        

                success = true; // success is temporarily true

                // loop through the word to make sure it exists in the container
                for (unsigned j = 1; j < word.size(); j++) {

                    // if either a mismatch happens, or container is too small
                    if (container.size() <= (j+i) || word.at(j) != container.at(j+i)) 
                        success = false;    // set the flag to false again

                }

            }
        }
    }

    return success;
}
我的解决方案:

bool hasSubstring(const char *word, const char *container) {

    if (container[0] == '\0' || word[0] == '\0')
        return false;

    for(int i = 0; container[i] != '\0'; i++) {

        bool foundNonMatch = false;
        for(int j = 0; word[j] != '\0'; j++) {

            if (container[i + j] != word[j]) {
                foundNonMatch = true;
                break;
            }

        }

        if (!foundNonMatch)
            return true;
    }

    return false;
}
bool isSubstringOf(string word, string container) {

    bool success = false;       

    // if either is empty, automatically return false 
    if (!word.empty() && !container.empty()) {

        // loop through the container and while not successful
        for (unsigned i = 0; i < container.size() && !success; i++) {

            // if the first letter of the word is found in the container...
            if (word.at(0) == container.at(i)) {                        

                success = true; // success is temporarily true

                // loop through the word to make sure it exists in the container
                for (unsigned j = 1; j < word.size(); j++) {

                    // if either a mismatch happens, or container is too small
                    if (container.size() <= (j+i) || word.at(j) != container.at(j+i)) 
                        success = false;    // set the flag to false again

                }

            }
        }
    }

    return success;
}
bool isSubstringOf(字符串字、字符串容器){
布尔成功=假;
//如果其中一个为空,则自动返回false
如果(!word.empty()&&!container.empty()){
//在容器中循环,但未成功
for(无符号i=0;i如果(container.size(),您不能仅仅通过查看两段代码就看出执行速度的任何差异,除非有明显的速度减慢

大多数编译器都会优化您的代码,因此,除非您喜欢研究操作码,否则告诉您哪一个会更快预编译并不容易

就速度而言,你应该对代码进行基准测试。强调它,看看它的性能如何

<>但是<强>效率不完全是关于速度的。你也应该考虑哪一个适合你的编码风格。个人来说,我讨厌看到甚至在研究代码被复制粘贴之前的随机链接。


+改为在那里发布:

它们都是二次的-在这两种情况下,容器中的每个字母都会根据每个单词的每个字母进行检查。
自从你问起

“时间和复杂性”


一般来说,这是无法回答的。看看你的机器上哪一个速度最快。

或者,与其重新发明轮子,不如使用:

container.find(word)

它来自标准库,因此您可以确信它具有合理的性能和正确性。您可以优化程序员时间、QA时间和用户时间(而不是交付可能存在错误的代码)通过使用经过良好测试的已知构建块,而不是滚动自己的构建块。

您确定它们会获得相同的结果(例如空字符串)吗?@doctorlove噢,是的,感谢您指出,当任一字符串为“”时,它意味着返回false。“包含”没有意义不包含任何内容。问题已更新后者可能会不断检查每个循环中的大小-如果幸运的话,它将被优化。“包含”没有意义-恰恰相反,它非常有意义。欢迎您提出自己对“包含”的定义这不包括空字符串大小写。如果它对您有效,那就更好了(欢迎您在此处共享)。但是,这样的定义似乎对其他任何人都不太有用。请随意查看任何现有的字符串或正则表达式库(有很多),或查阅任何计算机科学文本以了解“子字符串”的定义,或。它毕竟不属于这里。谢谢,这是一个复习问题,所以我觉得这将是一个有趣的练习:)当然,自己尝试一下没什么错,看看涉及到什么,然后在生产代码中使用库函数:)。