简体   繁体   中英

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. Bob is presented with 50 presents, though some of them are empty. 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.

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. I want my string getValue(vector &arr) to return an array/vector. 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).

So you don't need to return anything from getValue() - just make it void and it should do what you want.

string getValue(vector &arr) - the return type is string, not vector. You need to change its return type or set it to none.

PS: newArr = getValue(myArr); it's behind the SCOPE and it's wrongly positioned... damn, third PS, wrong code rules are assigned

You can't make your string getValue(vector<string> &arr) return an array/vector. It can only return a 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.

  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. 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";
    }
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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