我必须编写一个具有以下规范的 c++ 函数:
独特
删除两个参数指针之间的重复章程。字符串保留在其原始位置。
param first - 指向字符串开头的指针
参数 last - 指向最后一个宪章之后的数据的指针
return - 指向新数据系列第一个字符的指针
如果输入是“H\0elllo C++++ +!#”,则输出是“H\0elo C++!#”。我无法弄清楚如何忽略中间的终止空值。这是我迄今为止最好的方法:
char *sajat::unique(char *first, char *last){
char* moving = first + 1;
char* follower = first;
while (moving != last)
{
char *sentinel = first;
if (*follower == *moving)
{
counter++;
sentinel = follower; //here was the duplication
while (follower != last)
{
*follower = *moving;
follower++;
moving++;
}
sentinel = follwer;
moving = follower + 1;
}
moving++;
follower++;
}
return first - counter;
}
所以这段代码显然是错误的......但它可以成功识别重复。(我知道这是家庭作业,让我感到羞耻……但我已经尝试解决了好几个小时。抱歉,代码混乱了。)