【问题标题】:Scoping issue with a helper method to manipulate a string使用辅助方法操作字符串的范围问题
【发布时间】:2014-03-07 04:37:17
【问题描述】:

我想使用辅助方法对字符串执行操作。我希望该方法有两个参数:输入和输出。我认为我想如何解决问题存在范围问题,因为当我尝试显示输出时,它是空白的。我已经最小化了我的代码,所以你可以看到我正在采用的方法。

#include <iostream>
#include <string>
using namespace std;

void example_method(string in, string out);

int main(){
    string input;
    string output;
    cin >> input;
    example_method(input, output);
    cout << "The result is: " << output << endl;
    return 0;
}

void example_method(string in, string out){
    out = in;
}  

这个最小化编译并运行,但无论我输入什么字符串,结果总是空白。
解决这个问题的更好方法是什么?

【问题讨论】:

    标签: c++ string scope


    【解决方案1】:

    您将输出变量传递给函数,这意味着它的副本被推入堆栈,然后堆栈变量被更改并且永远不会返回到原始变量。您要做的是传递对变量的引用,如下所示:

    #include <iostream>
    #include <string>
    using namespace std;
    
    void example_method(string in, string &out);
    
    int main() {
        string input = "";
        string output = "";
        cin >> input;
        example_method(input, output);
        cout << "The result is: " << output << endl;
        return 0;
    }
    
    void example_method(string in, string &out) {
        out = in;
    }
    

    【讨论】:

    • 如何分配值而不是引用?
    • -1:输出未初始化,最好有引用。这是给 C++ 初学者的糟糕示例代码。
    • 我给出的描述是准确的。代码并不完美,我试图在没有在我的开发站的情况下提供帮助。我已经更新了代码。
    • @Sjoerd 是具有可变大小数据类型(如字符串)的首选参考?
    • @Dave 可变大小与它无关,您为什么认为这会有所不同?无论是哪种类型,引用都更容易,因为它不需要介绍“新”、“删除”或“内存泄漏”。
    【解决方案2】:

    解决方案一:字符串output 将始终为空白,您的代码仍为当前形式。这是因为output 仅在作为参数传递给example_method 时才被复制到另一个字符串实例。 out 是另一个字符串实例,其值为output。换句话说,这意味着string out=output,仅此而已。代码行out = in; 仅将in 的值复制到out

    So, actually the value of output is not being acted upon at all

    为了使output 的值生效,您必须传递它的值by reference,或者换句话说,您必须将address of output 传递给example_method,并且该地址将由指针获取。 这样,通过指针所做的任何更改也会影响 example_method 范围之外的输出字符串的值

    以下代码段说明了我的观点:

    #include <iostream>
    #include <string>
    using namespace std;
    
    void example_method(string another_string, string *pointer_to_output);
    
    int main()
    {
    string input="I am a test string";
    string output="";
    //cout<<"Enter input string"<<endl;
    //cin>>input;   
    example_method(input,&output);
    cout<<"The result is: "<<output<<endl;
    return 0;
    }
    
    void example_method(string another_string, string *pointer_to_output) 
    {
    *pointer_to_output=another_string;
    }
    

    解决方案二: 为什么不简单地将example_methodreturn typevoid 更改为std::string?通过这种方法,void example_method(string in, string out); 在 main 上方的声明中更改为 string example_method(string in, string out);。 并使用cout&lt;&lt;example_method(input, output);将返回的输出字符串输出到屏幕上 这样您就可以使用cout 简单地将输出返回到屏幕。

    这样你的代码就可以工作了,它实现了你想要做的事情,并且没有真正需要使用指针。

    修改后的代码

    #include <string>
    using namespace std;
    
    string example_method(string in, string out);
    
    int main(){
    string input;
    string output;
    cin >> input;
    cout<<example_method(input, output);
    // cout << "The result is: " << output << endl;
    return 0;
    }
    
    string example_method(string in, string out){
    out = in;
    return out;
    }  
    

    希望这会有所帮助!

    【讨论】:

      【解决方案3】:

      当您将参数传递给函数时,该值将被复制到参数中。

      在您的情况下,该函数将对复制的值进行操作,而原始值保持不变(在这种情况下:空)。

      这可以通过将参数声明为“参考参数”来更改。这是通过向类型添加&amp; 来完成的,如下所示:string&amp; out。则不会进行本地复制,因此将更新原始值。

      完整示例:

      #include <iostream>
      #include <string>
      
      void example_method(std::string in, std::string& out); // note the additional &
      
      int main(){
          std::string input;
          std::string output;
          std::cin >> input;
          example_method(input, output);
          std::cout << "The result is: " << output << std::endl;
          return 0;
      }
      
      void example_method(std::string in, std::string& out){ // Note the additional &
          out = in;
      }
      

      另一种方法是为函数指定返回值。

      您的函数没有返回任何内容 (void),因此您可以返回结果字符串。在这种情况下,不再需要输出参数,但您必须将返回值分配给调用站点的output 变量。

      一个完整的例子:

      #include <iostream>
      #include <string>
      
      std::string example_method(std::string in); // note that 'output' is gone
      
      int main(){
          std::string input;
          std::cin >> input;
          std::string output = example_method(input);
          std::cout << "The result is: " << output << std::endl;
          return 0;
      }
      
      std::string example_method(std::string in){
          return in; // use 'return' to return a value.
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-28
        • 1970-01-01
        • 2023-03-08
        • 2010-11-22
        相关资源
        最近更新 更多