如何从 C++ 函数返回一个字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16621084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to return a string from a C++ function?
提问by 1934286
This is a simple sample program:
这是一个简单的示例程序:
#include <iostream>
#include <string>
using namespace std;
string replaceSubstring(string, string, string);
int main()
{
string str1, str2, str3;
cout << "These are the strings: " << endl;
cout << "str1: \"the dog jumped over the fence\"" << endl;
cout << "str2: \"the\"" << endl;
cout << "str3: \"that\"" << endl << endl;
cout << "This program will search str1 for str2 and replace it with str3\n\n";
cout << "The new str1: " << replaceSubstring(str1, str2, str3);
cout << endl << endl;
}
string replaceSubstring(string s1, string s2, string s3)
{
int index = s1.find(s2, 0);
s1.replace(index, s2.length(), s3);
return s1;
}
It compiles however the function returns nothing. If I change return s1
to return "asdf"
it will return asdf
. How can I return a string with this function?
它编译但是该函数不返回任何内容。如果我改变return s1
到return "asdf"
它会返回asdf
。如何使用此函数返回字符串?
回答by syam
You never give any value to your strings in main
so they are empty, and thus obviously the function returns an empty string.
您永远不会为您的字符串赋予任何值,main
因此它们是空的,因此显然该函数返回一个空字符串。
Replace:
代替:
string str1, str2, str3;
with:
和:
string str1 = "the dog jumped over the fence";
string str2 = "the";
string str3 = "that";
Also, you have several problems in your replaceSubstring
function:
此外,您的replaceSubstring
功能存在几个问题:
int index = s1.find(s2, 0);
s1.replace(index, s2.length(), s3);
std::string::find
returns astd::string::size_type
(aka.size_t
) not anint
. Two differences:size_t
is unsigned, and it's not necessarily the same size as anint
depending on your platform (eg. on 64 bits Linux or Windowssize_t
is unsigned 64 bits whileint
is signed 32 bits).- What happens if
s2
is not part ofs1
? I'll leave it up to you to find how to fix that. Hint:std::string::npos
;)
std::string::find
返回一个std::string::size_type
(又名。size_t
)而不是一个int
。两个区别:size_t
是无符号的,并且它的大小不一定与int
您的平台相同(例如,在 64 位 Linux 或 Windows 上size_t
是无符号 64 位,而int
有符号 32 位)。- 如果
s2
不是 的一部分会发生什么s1
?我会把它留给你来找到如何解决这个问题。提示:std::string::npos
;)
回答by smskelley
string str1, str2, str3;
cout << "These are the strings: " << endl;
cout << "str1: \"the dog jumped over the fence\"" << endl;
cout << "str2: \"the\"" << endl;
cout << "str3: \"that\"" << endl << endl;
From this, I see that you have not initialized str1, str2, or str3 to contain the values that you are printing. I might suggest doing so first:
由此,我看到您尚未初始化 str1、str2 或 str3 以包含您正在打印的值。我可能建议先这样做:
string str1 = "the dog jumped over the fence",
str2 = "the",
str3 = "that";
cout << "These are the strings: " << endl;
cout << "str1: \"" << str1 << "\"" << endl;
cout << "str2: \"" << str2 << "\"" << endl;
cout << "str3: \"" << str3 << "\"" << endl << endl;
回答by Kirill Kobelev
Assign something to your strings. This will definitely help.
为你的字符串分配一些东西。这肯定会有所帮助。