【问题标题】:C++: Manipulating String using pointer through a functionC++:通过函数使用指针操作字符串
【发布时间】: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:我很确定读者并不需要行号。只需用评论或其他东西标记兴趣点。例如,//&lt;---here is a problem 在行尾。
  • @CodeCodeCode:您可以在相关行的末尾添加// line 29
  • 为什么要混合C风格的字符串和std::string

标签: c++ string iterator


【解决方案1】:

如果您无法修改函数签名。您可以使用“c_str()”返回 C 字符串。不建议这样做。

#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--;}
                }
        }
        std::strcpy (input, s1.c_str());
}


int main()
{
        char *c = (char *)malloc(32);
        strncpy(c,"Life of Pie",32);
        DelCommonChar(c,"def");
        cout<<c<<endl;
}

【讨论】:

    【解决方案2】:

    当你初始化s1:

    string s1(input);
    

    它将input 的内容复制到其内部缓冲区中。修改 s1 不会改变原始缓冲区。如果您想在输入中存储内容,请将它们复制回来(strcpystrncpymemcpy - 不过它们都是“不安全的”)或直接在 input 上操作。

    更好的办法是避免使用 C 字符串 (char*) 并使用带有引用的 std::strings。

    void DelCommonChar(std::string &input, string s2)
    {
          std::string &s1 = input;//you don't need that, you can use input directly.
          ...
    
     }
    

    【讨论】:

    • 根据您的建议进行更改会引发错误。 void DelChar1(string &amp;input, string s2) {...............} 下面的错误:it01.cpp:28: error: invalid initialization of non-const reference of type 'std::string&amp;' from a temporary of type 'char*' it01.cpp:7: error: in passing argument 1 of 'void DelChar1(std::string&amp;, std::string)' 这是因为我试图将 char* 传递给 string 对象。哪个编译器不接受。
    • @CodeCodeCode:显然,这意味着您必须摆脱*char并使用std::string而不是它作为变量。
    【解决方案3】:

    您可以维护两个指针,一个用于检查,一个用于写入。比如:

    int check=0,write=0;
    while (input[check])
    {
        if (input[check] is not to be deleted)
        {
            input[write++]=input[check];
        }
        ++check;
    }
    input[write]=0;
    

    【讨论】:

    • 你想提的。我不明白你的逻辑。
    • 这段代码比你的方法效率,也更简单。发生的情况是我们将您的字符串“压缩”为只需要的字符。
    猜你喜欢
    • 2022-08-05
    • 2013-06-20
    • 2014-10-29
    • 2013-11-10
    • 2014-09-22
    • 1970-01-01
    • 2018-04-17
    • 1970-01-01
    • 2022-01-07
    相关资源
    最近更新 更多