我制作了一个小程序,用于删除句子中的单词。每当我尝试运行程序时,它都会给我这些错误
错误2错误C2040:' ==' :' int'间接水平不同 来自' const char [4]' c:\ program files(x86)\ microsoft visual studio 12.0 \ vc \ include \ xutility行:3026列:1个STL字符串擦除
和
错误1错误C2446:' ==' :没有来自' const char *'至 ' INT' c:\ program files(x86)\ microsoft visual studio 12.0 \ vc \ include \ xutility行: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;
}
答案 0 :(得分:0)
你需要的东西 - 正如克里斯在评论中所写的那样 - 是std::string::find。原因基本上是你希望删除单词,而不是单个字符。
答案 1 :(得分:0)
我认为对std::string::erase
和std::string::find
成员函数存在误解。上面评论中列出的参考资料是否有用,为清楚起见,请在此处重复:std::basic_string::find和std::basic_string::erase?
使用这三到四个修改,它应该解决问题。请注意,上述帖子中的所有代码都会重复修改和附加注释,以便将所有代码放在一个位置以供将来审核。
#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;
}