简体   繁体   English

使用带递归的指针传递从字符串中删除 x

[英]removing x from a string using pass by pointer with recursion

I wrote this code to remove all occurrences of x from the string using recursion我编写这段代码是为了使用递归从字符串中删除所有出现的 x

#include <bits/stdc++.h>
using namespace std;
void removex(string str)
{
    if (str.length()==0)
    {
        return;
    }
    if (str[0] != 'x')
    {
         removex(str.substr(1,str.length()));
    }
    int i = 1;
    for (; str[i] != '\0'; i++)
    {
        str[i-1]=str[i];
    }
    str[i - 1] = str[i];
     removex(str);
    //  cout<<"strq"<<str<<endl;
}
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        string str;
        cin >> str;
        removex(str);
        cout << str << endl;
    }
    return 0;
} 

however it's pass by value and If I try using pass by reference it gives an error as initial value of reference to non-const must be an lvalueC.但是它是按值传递的,如果我尝试使用按引用传递它会给出错误,因为对非常量的引用的初始值必须是左值 C。 which means I need to make the reference constant which is not suitable for rest of the code.这意味着我需要创建不适合代码 rest 的引用常量。 I tried pass by pointer and using arrow operator however unable to get value at index and not sure how to make recursion call.我尝试通过指针传递并使用箭头运算符但是无法在索引处获取值并且不确定如何进行递归调用。 to pass address or ponter?通过地址或ponter? can someone modify it accordingly?有人可以相应地修改它吗?

Given that you are using C++, I would use the features of the standard library.鉴于您使用的是 C++,我会使用标准库的功能。 I believe this problem can be easily solved with a single line of code.我相信这个问题可以用一行代码轻松解决。 Assuming that the string variable is called line, you would just need to do something like:假设字符串变量称为 line,您只需要执行以下操作:

line.erase(remove(line.begin(), line.end(), 'x'), line.end());

Following is a complete example:下面是一个完整的例子:

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
  std::string line = "12djd V x jhrf h58HSFH HUHFuhfdhkdh   uhdfvygh 234 fhj xxx";
  std::cout << "Line before removing the x character: " << line << std::endl;
  line.erase(remove(line.begin(), line.end(), 'x'), line.end());
  std::cout << "Line after removing the x character:  " << line << std::endl;
  return 0;
}

The example above would produce the following output:上面的示例将产生以下 output:

Line before removing the x character: 12djd V x jhrf h58HSFH HUHFuhfdhkdh   uhdfvygh 234 fhj xxx
Line after removing the x character:  12djd V  jhrf h58HSFH HUHFuhfdhkdh   uhdfvygh 234 fhj 

An example that you can run is available here: https://onlinegdb.com/4wzMXTXP5此处提供了一个您可以运行的示例: https://onlinegdb.com/4wzMTXP5

Doing this with std::string and recursion is a formidable template for insanity.使用std::string和递归执行此操作是一个令人生畏的疯狂模板。 The erase/remove idiom exists for just this purpose, functions iteratively, and is highly efficient. erase/remove 习语就是为了这个目的而存在的,它迭代地运行并且非常高效。 Best of all, it already exists;最重要的是,它已经存在; all you have to do is set up the calls.您所要做的就是设置呼叫。

That said, if you're bent on doing this recursively (and inefficiently) you need to convey the result back to the caller (including the recursive calls) somehow .也就是说,如果您一心想以递归方式(且效率低下)执行此操作,则需要以某种方式将结果传回给调用者(包括递归调用)。 The following does that using the function return type, which is std::string .以下代码使用 function 返回类型(即std::string )执行此操作。 This also uses the global free operator + that allows concatenation of a char + std::string to return a new string:这也使用全局自由operator +允许连接char + std::string以返回新字符串:

#include <iostream>
#include <string>

std::string removex(std::string str)
{
    if (!str.empty())
    {
        if (str[0] == 'x')
            str = removex(str.substr(1));
        else
            str = str[0] + removex(str.substr(1));
    }
    return str;
}

int main()
{
    std::string str = "Remove all x chars from this string.";
    std::cout << "Before: " << str << '\n';
    std::cout << "After:  " << removex(str) << '\n';
    return 0;
} 

Output Output

Before: Remove all x chars from this string.
After:  Remove all  chars from this string.

That said, that isn't the way I'd do this.也就是说,这不是我这样做的方式。 I'd use the erase/remove idiom which would be much faster, and much more memory efficient.我会使用erase/remove 惯用语,它会更快效率更高 memory 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM