如何从 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:30:05  来源:igfitidea点击:

How to return a string from a C++ function?

c++stringreturn-value

提问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 s1to return "asdf"it will return asdf. How can I return a string with this function?

它编译但是该函数不返回任何内容。如果我改变return s1return "asdf"它会返回asdf。如何使用此函数返回字符串?

回答by syam

You never give any value to your strings in mainso 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 replaceSubstringfunction:

此外,您的replaceSubstring功能存在几个问题:

int index = s1.find(s2, 0);
s1.replace(index, s2.length(), s3);
  • std::string::findreturns a std::string::size_type(aka. size_t) not an int. Two differences: size_tis unsigned, and it's not necessarily the same size as an intdepending on your platform (eg. on 64 bits Linux or Windows size_tis unsigned 64 bits while intis signed 32 bits).
  • What happens if s2is not part of s1? 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.

为你的字符串分配一些东西。这肯定会有所帮助。