[英]Why can't I return the string array from the string function?
I would like to use my function to create a string array but my string function's return is not working.我想用我的函数来创建一个字符串数组,但我的字符串函数的返回不起作用。
string input() {
int months[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
string array[12];
for (int i = 0; i < 12; i++)
{
int j = 0;
while (j < months[i])
{
array[i] = array[i] + "-";
j++;
}
}
return array[12];
}
int main() {
string array[12];
array[12] = input();
return 0;
}
First of all your function is declared to return a single std::string
object.首先,您的函数被声明为返回单个
std::string
对象。 Secondly, you return array[12]
which is element thirteen of your twelve-element array.其次,您返回
array[12]
,它是十二元素数组的第十三元素。
And you can't actually return plain arrays from functions.而且您实际上无法从函数中返回普通数组。 Arrays decays to pointers to their first element, and that pointer would become invalid immediately as the life-time of the array ends when the function returns.
数组衰减到指向它们的第一个元素的指针,当函数返回时,随着数组的生命周期结束,该指针将立即失效。
There are a couple of solutions: Either pass the array (as a pointer to the first element) as an argument to the function.有几种解决方案:要么将数组(作为指向第一个元素的指针)作为参数传递给函数。 Or return eg an
std::array
object.或者返回例如一个
std::array
对象。
array[12]
is off the end of your array, and even if it were in bounds, it would be one string, not a collection of strings. array[12]
不在数组的末尾,即使它在边界内,它也将是一个字符串,而不是字符串的集合。
C style arrays are not assignable. C 样式数组不可分配。
std::vector<std::string>
and std::array<std::string, 12>
are alternatives that are assignable. std::vector<std::string>
和std::array<std::string, 12>
是可分配的替代方案。
using MonthMarks = std::array<std::string, 12>;
MonthMarks input() {
std::array<int, 12> months = { 31,28,31,30,31,30,31,31,30,31,30,31 };
MonthMarks result;
for (int i = 0; i < 12; i++)
{
int j = 0;
while (j < months[i])
{
result[i] = result[i] + "-";
j++;
}
}
return result;
}
int main() {
MonthMarks = input();
return 0;
}
Note that std::string
has a constructor that takes a size and a fill character, so your input can be simplified to请注意,
std::string
有一个接受大小和填充字符的构造函数,因此您的输入可以简化为
for (int i = 0; i < 12; i++)
{
result[i] = std::string(months[i], '-');
}
Your problem is that your have the wrong syntax for what you're doing, and some of it isn't possible.你的问题是你正在做的事情有错误的语法,有些是不可能的。 Unfortunately, neither of that causes a compile time error, it just does something else, including undefined behavior.
不幸的是,这两者都不会导致编译时错误,它只会执行其他操作,包括未定义的行为。
You need to declare the function like this:您需要像这样声明函数:
std::array<std::string, 12> input() {
You can't return an array, but if you wrap it into an std::array
, you can.你不能返回一个数组,但如果你把它包装成一个
std::array
,你可以。
Then do this inside:然后在里面做这个:
std::array<std::string, 12> array;
And return it as并将其返回为
return array;
Get and print the data like this:像这样获取并打印数据:
int main() {
std::array<std::string, 12> array = input();
for (std::string s : array) {
std::cout << s << std::endl;
}
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.