Howtomakeafunctionthattakesinastd::stringparameterthatwillreturntheamountofanythingsurroundedbywhitespace

I want to create a function that takes in a std::string parameter and returns an int of the amount of words in it. Like this:

int countWords(std::string){
    // code...
    return amountOfWords;
}

回答

Write a function stub:

int stub(const std::string& s/*don't deep copy the string*/)
{
    return function_you_found_online(s.c_str());
}

The function c_str() returns a const char* to the first character in s.

Using function stubs to wrap third party resources also has the advantage that you can switch out the resources at a later date without impacting your codebase too much.

  • Or just pass `s.c_str()` directly into a `function_you_found_online`

以上是Howtomakeafunctionthattakesinastd::stringparameterthatwillreturntheamountofanythingsurroundedbywhitespace的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>