c++ - 如何制作一个接受 std::string 参数的函数,该参数将返回由空格包围的任何内容的数量
问题描述
我想创建一个接受std::string
参数并返回其中int
单词数量的函数。像这样:
int countWords(std::string){
// code...
return amountOfWords;
}
解决方案
编写一个函数存根:
int stub(const std::string& s/*don't deep copy the string*/)
{
return function_you_found_online(s.c_str());
}
该函数c_str()
将 a 返回const char*
到 中的第一个字符s
。
使用函数存根包装第三方资源还有一个优势,即您可以在以后切换资源,而不会过多地影响您的代码库。
推荐阅读
- c++ - 使用 clang libtooling 重命名所有出现的变量名并具有 namedDecl 句柄的最简单方法是什么?
- lua - 如何检查多个 Lua 表是否包含共同值?
- javascript - NS_ERROR_XPC_SECURITY_MANAGER_VETO 发生在 Ajax 和 Django 中
- javascript - 在 HTML 表单中包含 jQuery 函数
- objective-c - 在 Mojave 上使用 Core Animation 图层绘图时如何解决透明系统颜色问题?
- laravel - Laravel 广播到私人频道失败
- react-native - React Native Navigation - 在 React Native v0.59 上运行 npm 运行 android 构建错误
- java - 如何在包含句子的字符串数组中搜索确切的单词(JAVA)
- java - JUnit Mockito 测试:零交互
- c# - 创建泛型类c#的实例