字符串::查找问题 (C++)
string::find Issue (C++)
我的字符串::find实现有一点问题。
输入是一个长字符串,由以下可能的示例组成:input = "one_thousand_and_fifty_one"。
我的问题似乎是,在包含多个"和"的输入字符串中,只有第一个被删除,而其他的则不被删除。
这是我到目前为止的代码,它找到"和",但只有在"a"之前的字母不是"s"(表示"千")时才删除。
string toKill = "and";
size_t andF = input.find(toKill);
if (andF != string::npos) {
if (input[(andF - 1)] != 's') {
input.erase(andF, 4);
}
}
编辑:我忘了提到,输入中唯一包含"and"的另一个单词是"千",所以这是一个特例。
试试这个:
string toKill = "and";
size_t andF = 0;
while ((andF = input.find(toKill, andF)) != string::npos) {
if (andF == 0 || input[andF - 1] != 's') {
input.erase(andF, 4);
}
else ++andF;
}
我会为此使用正则表达式(来自 boost、PCRE 或 C++11 标准) - 但如果必须我自己做,我的代码会看起来像这样:
string toKill = "and";
size_t pos = 0;
while( ( pos = s.find(toKill, pos) )!=std::string::n_pos )
{
//Check it doesn't start with an additional letter
if( pos!=0 && is_alpha(s[pos-1]) ) { pos++; continue; }
//Check it doesn't end with an additional letter
if( pos+toKill.size()!=s.size() && is_alpha(s[pos+toKill.size()]) { pos++; continue;}
//Remove it and the trailing whitespace (or punctuation)
s.erase(pos,toKill.size()+1);
}
代码中(至少)需要另外两件事。第一个是处理and
字符串的整个字符串的循环,第二个是跳过已检查字符串的能力。
您可能还希望处理字符串可能以 and
开头的可能性,尽管这不太可能:对你的期望要自由,对你交付的东西要具体
以下代码将是一个很好的起点:
#include <iostream>
#include <string>
int main (void) {
std::string inputStr = "one thousand and fifty one";
std::string killStr = "and ";
size_t startPos = 0;
size_t andPos;
while ((andPos = inputStr.find (killStr, startPos)) != std::string::npos) {
if ((andPos == 0) || (inputStr[(andPos - 1)] != 's')) {
inputStr.erase(andPos, killStr.length());
startPos = andPos;
} else {
startPos = andPos + 1;
}
}
std::cout << inputStr << 'n';
return 0;
}
而且,由于我对在字符串的开头使用 and
很偏执,并且 Michael 正确地要求我不在字符串 (a)的末尾处理它,你可以修改它,这样做如下:
#include <iostream>
#include <string>
#include <cstring>
static bool endsWith (std::string s1, std::string s2) {
size_t s1Len = s1.length();
size_t s2Len = s2.length();
if (s2Len > s1Len)
return false;
return (strcmp (s1.c_str() + s1Len - s2Len, s2.c_str()) == 0);
}
int main (void) {
std::string inputStr = "and one thousand and fifty one thousand and";
std::string killStr = "and ";
size_t startPos = 0;
size_t andPos;
while ((andPos = inputStr.find (killStr, startPos)) != std::string::npos) {
if ((andPos == 0) || (inputStr[(andPos - 1)] != 's')) {
inputStr.erase (andPos, killStr.length());
startPos = andPos;
} else {
startPos = andPos + 1;
}
}
if (!endsWith (inputStr, "sand") && endsWith (inputStr, "and"))
inputStr.erase (inputStr.length() - 3);
std::cout << inputStr << 'n';
return 0;
}
(a)如果我要成为一个学究,我最好做得好:-)
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 一个关于在C++中重载布尔运算符的问题
- 首要问题的答案让值班员搞错了
- setlocale的C++土耳其字符串问题
- 如何重构类层次结构以避免菱形问题
- 基于boost的程序的静态链接——zlib问题
- C++格式化输出问题
- 使用mongocxx驱动程序时包含头文件问题