使用递归的指针通过指针从字符串中删除x

发布于 2025-02-04 01:08:53 字数 776 浏览 2 评论 0原文

我编写了此代码以使用递归从字符串中删除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;
} 

但是它是按值来的。 参考非const的初始值必须是LVALUEC。这意味着我需要制作不适合其余代码的参考常数。我尝试通过指针并使用箭头操作员通过,但是无法在索引上获得值,也不确定如何进行递归调用。通过地址还是庞特?有人可以相应地修改吗?

I wrote this code to remove all occurrences of x from the string using recursion

#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. which means I need to make the reference constant which is not suitable for rest of the code. 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? can someone modify it accordingly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绻影浮沉 2025-02-11 01:08:53

使用std :: String和递归是一种精神错乱的强大模板。 erase/remove/remove IDIOM 出于此目的而存在,效法,效法,迭代,迭代,迭代,并且高度高效。最重要的是,它已经存在;您要做的就是设置呼叫。

也就是说,如果您屈服于递归(效率低下),则需要将结果传达给呼叫者(包括递归电话)以某种方式。以下是使用函数返回类型进行的,即std :: String。这还使用全局免费运算符 +,该> char> 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;
} 

输出

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

也就是说,那不是我这样做的方式。我将使用 erase/remove/remove Idiom >更快, 更有效的内存效率。

Doing this with std::string and recursion is a formidable template for insanity. The erase/remove idiom exists for just this purpose, functions iteratively, and is highly efficient. 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. This also uses the global free operator + that allows concatenation of a char + std::string to return a new 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

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.

旧竹 2025-02-11 01:08:53

考虑到您正在使用C ++,我将使用标准库的功能。我相信可以使用一行代码轻松解决此问题。假设字符串变量称为行,您只需要做类似的操作:

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

以下是一个完整的示例:

#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;
}

上面的示例将产生以下输出:

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 

您可以在此处可用的示例: https://onlinegdb.com/4wzmxtxp5

Given that you are using C++, I would use the features of the standard library. 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.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:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文