简体   繁体   English

我从我的字符串函数返回到主函数是什么?

[英]What do I return to the main function from my string function?

I finished my lab problem, but I have a quick question to fix the end.我完成了我的实验室问题,但我有一个快速的问题来解决这个问题。 I have a vector in the function that needs to be returned to main so I can output the elements of the vector.我在函数中有一个需要返回到 main 的向量,以便我可以输出向量的元素。 I put return a;我把 return a; at the end of the function since a is the name of the vector in the function but I'm getting an error.在函数的末尾,因为 a 是函数中向量的名称,但出现错误。

*Where it says "cout << the names are " should be in the main but I can't figure out what to put in the return. *它说“cout << the names are”的地方应该是主要的,但我不知道要在回报中放什么。 *I also put return 0 because it was the only way I had the whole program working since the output is also in the function, but I need it back in main and change the return 0; *我也把 return 0 设置为我让整个程序工作的唯一方法,因为输出也在函数中,但我需要它返回 main 并更改 return 0; Sorry if this is a bad question I'm still learning, thanks.对不起,如果这是一个不好的问题,我仍在学习,谢谢。

string switching(vector<string> a, int n) {
    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
            if (a[i] > a[j]) {
                swap(a[i], a[j]);
            }
        }
    }

    cout << "The order of names are...\n";
    for (int i = 0; i < n; i++) {
        cout << a[i] << "\n";
    }

    return 0;
}

As has been suggested, you can change the function signature to正如所建议的,您可以将函数签名更改为

std::vector<std::string> switching(std::vector<std::string> a, int n)

Or, you could pass the string vector argument by reference:或者,您可以通过引用传递字符串向量参数:

void switching(std::vector<std::string>& a, int n)

This shows a main calling the first version:这显示了主要调用第一个版本:

#include <iostream>
#include <string>
#include <vector>

std::vector<std::string> switching(std::vector<std::string> a, int n) {
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      if (a[i] > a[j]) {
        swap(a[i], a[j]);
      }
    }
  }
  return a;
}

int main()
{
  std::vector<std::string> strings{
    "John",
    "Fred",
    "Alice"
  };

  auto sorted = switching(strings, strings.size());
  std::cout << "The order of names are...\n";
  for (auto const& name : sorted) {
    std::cout << name << "\n";
  }

  return 0;
}

1.You could modify the return type of the function; 1.可以修改函数的返回类型;

   vector<string> switching(vector<string> a, int n)
{
     //Your core-code here;
     return a;    
}
  1. The parameters could be pass by reference.参数可以通过引用传递。
void switching(vector<string> &a, int n)
{
     //Your core-code here;

}

In this way, the parameters could change simultaneously in main function.这样就可以在主函数中同时改变参数。

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

相关问题 如何从函数中返回更改后的字符串? - How do I return my changed string from my function? 无法将字符串返回到主函数? - Not able to return string to main function? 如何制作我的QThread块并等待从主线程调用的函数返回值? - How can I make my QThread block and wait for a function called from the main thread to return a value? 如何将字符串数组从C#函数返回到调用它的C ++函数? - How do I return a String Array from a C# function to a C++ function calling it? 当我从我的 header 和实现文件调用我的 function 到我的主文件时,我没有得到任何 output - When I call my function from my header and implementation file to my main file, I do not get any output 为什么我不能从主函数返回更大的值? - Why can't I return bigger values from main function? 如何找到我的程序的 main(...) function? - How do I find my program's main(…) function? 如何将学生 Function 的值返回到 Main,以便我可以将它们用于显示 Function? - How do I return the values of the Student Function to Main so that I can I use them for the Display Function? 如何将传递给main(在argv中)的参数传递给我的函数? - How do I pass an argument that was passed to main (in argv) to my function? 如何从函数返回数组? - How do I return a array from a function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM