C++ C++;字符串擦除
我制作了一个小程序,可以从句子中删除单词。每次我试着运行这个程序,它都会给我这些错误 错误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字符串擦除 这是我的代码C++ C++;字符串擦除,c++,string,stl,runtime-error,C++,String,Stl,Runtime Error,我制作了一个小程序,可以从句子中删除单词。每次我试着运行这个程序,它都会给我这些错误 错误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
#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;
int main(){
字符串示例(“hello world”);
cout正如chris在评论中所写,您需要的是。原因基本上是您希望删除单词,而不是单个字符。我认为对std::string::erase
和std::string::find
成员函数存在误解。我上面评论中列出的引用是否有用特德在这里澄清:和
使用这三到四个修改,应该可以解决问题。请注意,上面帖子中的所有代码都经过了修改和附加注释,以便将所有代码放在一个位置供将来查看
#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;
}
#包括
#包括
#包括
使用名称空间std;
int main(){
字符串示例(“hello world”);
请在错误消息中给出行号。std::find
仅查找单个元素(即字符),而不是子集(即子字符串)。使用std::string::find
。转到此参考:有关std::string::find
成员函数的四个重载版本的详细信息。将此函数用于三个不同的擦除重载:。在string::迭代器iCharH=std::string::find(sample.begin(),sample.end(),“h”)中替换“h”
与'h'
声誉高的会员(如100K+)在我30多天前发布的一个回复中通知我,上面的网站不如cppreference.com
好。不幸的是,这是我从这位声誉很高的会员那里得到的唯一评论。我没有任何具体的理由避开列出的网站。我确实尝试过,但它给了我一个错误。ErroR1错误C2665:'std::basic_string::find':4个重载都无法转换所有参数类型line:15 column:1 STL stringerase@Jim对于返工的字。查找(…)
和字。擦除(…)使用的确切实现编码是什么
function calls?@cpluplusooaa我对我的帖子进行了修改,这是对上一篇文章的重新编写one@Jim我已经发布了一个答案,应该澄清,同时重复方便的在线C++参考页。