2014-03-07 29 views
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

您正在將輸出變量傳遞給函數,這意味着它的副本被壓入堆棧,然後堆棧變量被更改並且永遠不會返回到原始變量。你想要做的是傳遞一個參考變量,像這樣:

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

我試了一下,就像你寫它segfaulted。 –

+0

我將如何分配值而不是參考值? –

+0

-1:輸出未初始化,並且引用將是可取的。這是給起始C++程序員的糟糕示例代碼。 – Sjoerd

1

解決方法一:字符串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

爲了實現你必須通過其值by reference或換句話說的output值,則必須通過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_methodvoidreturn typestd::string?通過這種方法void example_method(string in, string out);更改爲string example_method(string in, string out);上面的main聲明。 並且返回的輸出字符串在屏幕上使用cout<<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; 
} 

希望這有助於!

1

將參數傳遞給函數時,該值被複制到參數中。

在您的情況下,該函數將對複製的值進行操作,並保留原始值(在這種情況下爲空)。

這可以通過將參數聲明爲'參考參數'來改變。這是通過將&添加到類型來完成的,如下所示:string& 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. 
}