简体   繁体   English

C ++:如何创建返回向量/数组的返回函数?

[英]C++: How do you create a return function that returns a vector/array?

This is the motivation behind the code. 这就是代码背后的动机。 There is a boy named Bob and its his birthday today. 有一个男孩叫鲍勃,今天是他的生日。 He invites 50 friends over but not all of his friends want to buy him gifts. 他邀请50个以上的朋友,但并非所有的朋友都想给他买礼物。 Bob is presented with 50 presents, though some of them are empty. 鲍勃收到了50份礼物,尽管其中有些是空的。 His good friends tell him to close every 2nd box. 他的好朋友告诉他关闭第二个盒子。 For every third box, he is supposed to change every closed to open and every open to closed. 对于第三个盒子,他应该将每个关闭状态更改为打开状态,并将每个打开状态更改为关闭状态。 He continues to do this for every n-th box where n is less than 50. The open boxes in the end will have the presents. 他继续对第n个小于50的第n个框执行此操作。最后打开的框将具有礼物。

This is supposed to assist me in figuring out a problem for my math class, but I am not aware of all the complicated aspects of C++ programming. 本来可以帮助我为数学课解决一个问题,但是我并不了解C ++编程的所有复杂方面。 I want my string getValue(vector &arr) to return an array/vector. 我希望我的字符串getValue(vector&arr)返回一个数组/向量。 This code doesn't compile but it shows what I'm trying to do. 这段代码没有编译,但是显示了我正在尝试执行的操作。

#include <iostream>
#include <vector>
#include<algorithm>

using namespace std;
string getValue(vector<string> &arr);

int main()
{
  vector<string> myArr(2);
  vector<string> newArr(2);

  for(int i=2; i <= 50; i++)
  {
    if(i%2==0)
    {
        myArr.push_back("close");
    }
    else
    {
        myArr.push_back("open");
    }
}

newArr = getValue(myArr);

 for(int i=2; i <=50; i++)
 {
    cout << i << " " << newArr[i] << endl;
 }

}

string getValue(vector<string> &arr)
{

 for(int i=2; i <=50; i++)
 {
    if(arr[i]=="close")
    {
      arr[i]="open";
    }
    else if(arr[i]=="open")
    {
      arr[i]="close";
    }

 }

   return arr;

}

You're passing the vector into getValue() by reference, which means changes you make to it in that function will affect the original (in other words, you're not operating on a copy of the vector - you're actually operating on the vector). 您是通过引用将向量传递给getValue()的,这意味着您对该函数所做的更改将影响原始向量(换句话说,您不是在向量的副本上进行操作-实际上是在向量)。

So you don't need to return anything from getValue() - just make it void and it should do what you want. 因此,您不需要从getValue()返回任何内容-只需使其void ,它就可以执行您想要的操作。

string getValue(vector &arr) - the return type is string, not vector. string getValue(vector&arr)-返回类型是字符串,而不是vector。 You need to change its return type or set it to none. 您需要更改其返回类型或将其设置为none。

PS: newArr = getValue(myArr); PS:newArr = getValue(myArr); it's behind the SCOPE and it's wrongly positioned... damn, third PS, wrong code rules are assigned 它在SCOPE后面,并且定位错误...该死,第三个PS,分配了错误的代码规则

You can't make your string getValue(vector<string> &arr) return an array/vector. 您不能使string getValue(vector<string> &arr)返回数组/向量。 It can only return a string . 它只能返回一个string If you want a function to return an array/vector, then you have to say so in the function signature. 如果要让函数返回数组/向量,则必须在函数签名中这样说。

For the syntax part :- 对于语法部分:

  1. The return type of the function is a string. 函数的返回类型是字符串。 Change it to vector for your function to work properly. 将其更改为vector,以使您的功能正常工作。

  2. You can simply declare the vectors globally. 您可以简单地全局声明向量。 This will eliminate the need to pass it to the function as well as return it. 这将消除将其传递给函数以及将其返回的需要。

For the logic part :- 对于逻辑部分:

Your question says that Bob toggles every third box but in your program Bob is changing every box to open if it is closed and every box to close if it is open. 您的问题是,Bob切换了每个第三个框,但是在程序中,Bob更改了每个框(如果关闭)的打开状态以及每个框(如果打开的关闭)状态。 If what you wrote in the question is correct your code should be like this. 如果您在问题中所写的内容正确,则您的代码应如下所示。

#include <iostream>
#include <vector>

using namespace std;

void getValue();
vector<string> myArr(2);

int main()
{

  for(int i=2; i <= 50; i++)
  {
    if(i%2==0)
    {
        myArr.push_back("close");
    }
    else
    {
        myArr.push_back("open");
    }
}

getValue();

 for(int i=2; i <=50; i++)
 {
    cout << i << " " << myArr[i] << endl;
 }

}

void getValue()
{

 for(int i=3; i <=50; i+=3)
 {
    if(myArr[i]=="close")
    {
      myArr[i]="open";
    }
    else if(myArr[i]=="open")
    {
      myArr[i]="close";
    }
 }
}

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

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