'替换'字符串中带有int的char

时间:2014-08-09 12:50:51

标签: c++ string char int

起初这似乎很容易,但我错了。 我想用int(value)'替换'字符串中的char(变量)。但是如何?

我尝试使用replace(),因为我正在使用字符串并且它工作但是如果我希望将变量的值再次更改为另一个,它将无法工作 值因为那时找不到原始变量。过去2天我一直在努力解决这个问题。任何帮助将非常感谢如何做到这一点。

changeVar(string startingExpr, char var, int val)
{
    for(int i = 0; i < startingExpr.length(); i++)
    {
        if(startingExpr[i] == var)
        {
            cout << "I found x! Now to replace it!";
            startingExpr[i] = val;       //'Replace' x with 5, but x but how?
        }
    }
}

非常感谢您对此的帮助。

Ĵ

3 个答案:

答案 0 :(得分:0)

您的错误是分配与ASCII字符represntation不同的数字。在ASCII表格字符&#39; 0&#39;,&#39; 1&#39; ..&#39; 9&#39;一个接一个地去。因此,您可以将代码重写为:

startingExpr[i] = '0' + val;

但请注意,这仅适用于一个角色案例。如果您需要替换多个字符,那么您的解决方案无需功能就是:

#include <iostream>
#include <string>

int
main()
{
  std::string a("SomeVar1"), b("SomeVar356");

  std::string::size_type index = std::string::npos;

  std::cout << "Before: " << a << std::endl;
  if ((index = a.rfind("1")) != std::string::npos)
    a.replace(index, 1, std::to_string(2));
  std::cout << "After: " << a << std::endl;

  std::cout << "Before: " << b << std::endl;
  if ((index = b.rfind("356")) != std::string::npos)
    b.replace(index, 3, std::to_string(673));
  std::cout << "After: " << b << std::endl;

  return 0;
}

这是稍微优化的,因为它使用的是rfind(从字符串末尾搜索)。

P.S。正如评论建议的那样 - 你可以使用std :: replace和reverse迭代器以及lambda for condition。由于它在C ++ 11中可用,我用通用样式编写了一个小例子。

答案 1 :(得分:0)

如果您希望能够根据需要多次将“变量”替换为“值”,则应保留原始字符串的副本并仅从那里进行替换。

另一种选择是撤消替换(用变量替换值)并重做另一个值,前提是可以明确地完成撤消。

答案 2 :(得分:0)

我很遗憾听到这个问题浪费了你超过2天。你应该仔细阅读一本基本的C ++教科书。因为你的问题是一个非常基本的问题。如果你理解为函数传递的参数,你将对它进行排序!

更具体地说,你的功能没有错,但只是没有提供你想要的结果。因为你的函数使用参数,它将在函数体内复制参数startingExpr,当你使用“startingExpr [i] = val”进行替换时,替换发生在startingExpr的副本上(这是一个局部变量,只是可见在函数内部,你原来的startingExpr确实发生了变化。

解决方案非常简单,通过引用更改参数,只需添加&amp;,现在声明你的函数应该是:“changeVar(string startingExpr,char var,int val)”

尝试以下代码,这将演示我的解释:

    #include <iostream>
    #include <string>

    using namespace std;

    //the original function which was reported had problem
    // This because the function using parameter not reference

    void changeVar(string startingExpr, char var, int val)
    {
        for(int i = 0; i < startingExpr.length(); i++)
        {
            if(startingExpr[i] == var)
            {
                cout << "I found x! Now to replace it!(But the replace is happend inside the function, the original string is not changed!)"<<endl;
                startingExpr[i] = val;       //'Replace' x with 5, but x but how?
            }
        }
    }
    // updating using the reference 
    //void changeVar(string & startingExpr, char var, int val)
    void changeVar_refe(string & startingExpr, char var, int val)
    {
        for(int i = 0; i < startingExpr.length(); i++)
        {
            if(startingExpr[i] == var)
            {
                cout << "I found x! Now to replace it!(Now using reference, the replace is happend inside the original string, which will be changed!)"<<endl;
                startingExpr[i] = val;       //'Replace' x with 5, but x but how?
            }
        }
    }

    int main()
    {

        //lets test the original function

        string my_name="Hi there, I am C++ taoism .";
        cout<<"The original string is: "<<my_name<<endl;
        //lets change a to A
        changeVar(my_name,'a',65);    
        cout<<"The changed string is: "<<my_name<<endl;

        cout<<endl;
        cout<<"Using the reference to test ... "<<endl;
        cout<<endl;

        cout<<"The original string is: "<<my_name<<endl;
        //lets change a to A
        changeVar_refe(my_name,'a',65);    
        cout<<"The changed string is: "<<my_name<<endl;

        //cout <<"Char A is int 65:"<<int('A')<<endl;

    }