【发布时间】:2013-05-27 10:11:03
【问题描述】:
我编写了以下程序,它将删除 string1 中的所有常见字符,存在于 string2 中。
#include<iostream>
#include<string>
#include<iterator>
using namespace std;
void DelCommonChar(char *input, string s2)
{
string s1(input);
string::iterator it1;
string::iterator it2;
for(it1=s1.begin();it1<s1.end();it1++)
{
for(it2=s2.begin();it2<s2.end();it2++)
{
if(*it1==*it2){it1=s1.erase(it1);it1--;}
}
}
cout<<s1<<endl; // Line Number 20
}
int main()
{
char c[32];
strncpy(c,"Life of Pie",32);
DelCommonChar(c,"def");
cout<<c<<endl; //Line Number 29
}
Output:Li o pi ......... printed through line number 20.
但现在我想更改 c[32] 中的变量 main function 本身,并且我希望 line number 29 打印输出。
您能帮我吗,如何仅在函数DelCommonChar 内部更改变量c[32]?
注意:我不想更改函数返回数据类型void。
【问题讨论】:
-
请不要发布这样的行号代码;它可以防止读者将其复制粘贴到编辑器中并能够对其进行编译。
-
@OliCharlesworth 我想通过仅在此处查看行号来减轻读者的负担,因为我在问题中提到了行号。使用 Notepad++ 垂直选择列并删除会有所帮助。
-
@CodeCodeCode:我很确定读者并不需要行号。只需用评论或其他东西标记兴趣点。例如,
//<---here is a problem在行尾。 -
@CodeCodeCode:您可以在相关行的末尾添加
// line 29。 -
为什么要混合C风格的字符串和
std::string?