C++ STL 字符串擦除
C++ STL string erase
我做了一个小程序,可以从句子中删除单词。每次我尝试运行该程序时,它都会给我这些错误
错误2 错误 C2040:"==":"int"在间接级别上有所不同 来自 'const char [4]' c:\Program Files (x86)\Microsoft Visual Studio 12.0\vc\include\x实用程序行:3026 列:1 STL 字符串擦除
和
错误1 错误 C2446:"==":没有从"常量字符 *"到 'int' c:\program files (x86)\Microsoft Visual Studio 12.0\vc\include\x实用程序行:3026 列:1 STL 字符串擦除
这也是我的代码
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(){
string sample("hello world");
cout << "The sample string is: ";
cout << sample << endl;
//erasing world
cout << "Erasing world" << endl;
sample.erase(5, 10);
cout << sample << endl;
//finding h and erasing it
string::iterator iCharH = std::string::find(sample.begin(), sample.end(), "h");
cout << "finding h and erasing it" << endl;
if (iCharH != sample.end()){
sample.erase(iCharH);
}
cout << sample << endl;
//erasing entirely
sample.erase(sample.begin(), sample.end());
if (sample.length() == 0){
cout << "The string is empty" << endl;
}
system("pause");
return 0;
}
你需要的 - 正如克里斯在评论中所写的 - 是std::string::find。原因基本上是您希望删除单词,而不是单个字符。
我认为对
std::string::erase
和std::string::find
成员函数存在误解。 我上面评论中列出的参考文献是否有帮助,为了清楚起见,在这里重复:std::basic_string::find 和 std::basic_string::擦除?
使用这三到四个修改,它应该可以解决问题。 请注意,上面帖子中的所有代码都重复修改和额外的注释,以便将它们全部放在一个位置以供将来查看。
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(){
string sample("hello world");
cout << "The sample string is: ";
cout << sample << endl;
//erasing world
cout << "Erasing world" << endl;
//
// Erase the word "world" from the string, while not sending erase more
// characters to erase than going past the end of the string.
// erase is friendly enough not to have issues with passing a higher count
// in the second parameter, but future C++ versions could throw an exception
// in some future standard, e.g. C++14 or later.
//
sample.erase(5, 6);
cout << sample << endl;
//finding h and erasing it
//
// Use the short version of the find member function to look for the string
// "h". iCharH is now declared as in int, instead of an iterator. If the
// search string is not present, the iCharH value will be std::string::npos,
// otherwise iCharH will contain the starting index position of the search
// string.
// string::iterator iCharH = std::string::find(sample.begin(), sample.end(), "h");
//
int iCharH = sample.find("h");
cout << "finding h and erasing it" << endl;
//
// Using a different overloaded form of erase, make sure to remove the
// exact number of characters. In this case, the number is ONE. The
// std::string::npos can be shortened to string::npos, but only because
// using namespace std; is above.
//
if (iCharH != std::string::npos){
sample.erase(iCharH, 1);
}
cout << sample << endl;
//erasing entirely
//
// Passing no parameters to erase empties a string. This is optional, but
// less typing than the iterator version. The iterator version works
// perfectly too.
// sample.erase(sample.begin(), sample.end());
//
sample.erase();
if (sample.length() == 0){
cout << "The string is empty" << endl;
}
system("pause");
return 0;
}
相关文章:
- C++ 字符串类擦除成员函数的时空复杂性
- 在 stl 中的字符串中擦除关键字的工作?
- 无法使用"擦除"函数从 c++ 字符串中删除数字
- 当找到来自 RHS 的字符"\"时,从主字符串中获取单词或子字符串,然后擦除其余部分
- 使用 str.erase() 的索引擦除字符串的元素?
- 字符串附加会擦除字符串?
- C++ cout 在擦除换行符后打印字符串不正确
- 使用递归擦除字符串上的所有非数字
- 如何更快地擦除我的字符串
- 使用反向remove_if擦除字符串
- 用字符串::擦除在C 中提取数据
- 从 std::字符串中删除字符从 "(" 到 ")" 用擦除?
- 使用擦除和删除从字符串中删除字符
- 字符串的C ++向量,擦除字符的行为很奇怪
- 从字符串中擦除单词(在C++中)
- 找到字符串c++中的字符序列并将其擦除
- 如何擦除字符串中的字符
- 只擦除字符串(C++)中的一个元素
- 为什么我无法擦除字符串的数字字符?
- 为什么我不能在 C++ 中使用 str.erase(str.begin() + index) 擦除字符串中的字符?