c++ - 为什么我的字符串函数不返回任何内容?
问题描述
该函数要么不返回任何内容,要么返回一个空字符串。我试图-e
在字符串中的文本之后的下一个序列之前擦除所有内容。例如,test -e test/file/path
应该返回test/file/path
不带空格的-e
. 但是,当函数不返回任何内容或返回空字符串时。我找不到我的功能有什么问题,请帮忙!
#include <iostream>
using namespace std;
string fileparse(string str) {
int i;
for (i = 0; i < str.size(); i++)
if (str[i] == '-')
i += 2; //to account for the space after 'e'
str.erase(str.begin(), str.begin()+i);
return str;
}
int main()
{
string str = "test -e test/file/path";
string vars = fileparse(str);
cout << vars << endl; //blank
return 0;
}
解决方案
if (str[i] == '-') {
i += 3;
break;
}
遇到'-'
符号时停止。去掉i
加一1
,去掉字母e
加一个空格加一2
。
推荐阅读
- objective-c - 如何检查 OC 或 Swift 中未使用的变量
- xampp - #2002 - 连接被拒绝 - 服务器没有响应(或本地服务器的套接字配置不正确)
- angular - 如何在谷歌云功能中设置自定义声明?
- java - 如何开发一个可以使用 java 回答问题的消息机器人
- angular - 更改角度组件中的通知图标
- php - oci8、php7 和 Oracle 10.1 兼容性
- exception - 检查失败:带有音乐播放器插件的 CheckException(env)
- c++ - Crypto++ AES 代码和链接上的未定义符号
- python - 为什么我在 Django 中有这个错误?
- react-admin - React Admin:如何使用引导程序而不是 Material Design?