简体   繁体   English

在结构中返回多个值

[英]Returning multiple values in a struct

So I have this code but I have a couple things that need tweaking... 所以我有这个代码,但我有一些需要调整的东西......

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

struct Student {
    string major;
    string name;
    int year;
};

string yearToString(Student s) {
    string stringYear;
    ostringstream convert;
    convert << s.year;
    stringYear = convert.str();
    cout << stringYear << endl; // this prints 3
    return stringYear; // this returns nothing
}

int main() {
    Student* students = new Student[2];

    students[0].major = "Computer Science";
    students[0].name = "Jermaine";
    students[0].year = 2;

    students[1].major = "Biology";
    students[1].name = "Kelsey";
    students[1].year = 3;

    yearToString(students[1]);

    delete[] students;
    return (0);
}

I was wondering why my return statement doesn't return the value 3? 我想知道为什么我的return语句没有返回值3? Also lets say I wanted to return multiple values of the struct. 还可以说我想返回结构的多个值。 How would I return something like "Jermaine, Computer Science (2)". 我如何归还“杰梅因,计算机科学(2)”之类的东西。 Can anyone point me in the right direction? 谁能指出我正确的方向?

How do you know this returns nothing... 你怎么知道这没有什么回报......

yearToString(students[1]);

... when you don't do anything with the return? ......当你没有做任何回报的事情? Did you mean to assign it to something? 你的意思是把它分配给某事吗?

std::string yrStr = yearToString(students[1]);
std::cout << yrStr << std::endl; // prints 3

You don't use the returned value in any way. 您不以任何方式使用返回的值。 Changing the code to: 将代码更改为:

cout << yearToString(students[1]) << endl;

prints 3 as expected. 按预期打印3。

You seems to mix struct s, which represent data in memory, with formatted strings that are presented to the user. 您似乎将表示内存中数据的struct与呈现给用户的格式化字符串混合在一起。 What you probably want to return seems to be the latter. 您可能想要返回的似乎是后者。 In that case, all you need to do is format the struct: 在这种情况下,您需要做的就是格式化结构:

ostringstream ostr;
ostr << s.name << ", " << s.major << " (" << s.year << ")";
return ostr.str();

To return multiple values from a structure, there are two primary choices: 要从结构中返回多个值,有两个主要选择:

  1. Place into a struct, return the struct. 放入结构中,返回结构。
  2. Pass a struct as a parameter, by reference, modify the values in the parameter. 通过引用将结构作为参数传递,修改参数中的值。

First, define the structure: 首先,定义结构:

struct Result
{
  std::string student_name;
  std::string course_name;
};

Returning the values: 返回值:

Result My_Function()
{
  Result value;
  value.student_name = "Jermaine";
  value.course_name  = "Biology 101";
  return value;
}

Modifying a parameter: 修改参数:

void Sams_Function(Result& value)
{
  value.student_name = "Jermaine";
  value.course_name  = "Biology 101";
}

Many variables by reference: 许多变量通过引用:

Another method, not preferred, is to pass many variables by reference: 另一种不优选的方法是通过引用传递许多变量:

void Another_Function(std::string& student_name,
                      std::string& course_name)
{
      student_name = "Jermaine";
      course_name  = "Biology 101";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM