简体   繁体   中英

C++ Calling Vectors from Function to Main

I'm trying read a large amount of values into a vector in a specific function and then calling it into the main to get the average. My readInput works perfectly. But I believe my main function returns 0 when I cout << values.size();. Why is this? What can I do to change that?

using namespace std;
//function prototype
int readInput(vector<int> vect);


int main()
{
vector<int> values;
int sum, avg;
sum = readInput(values);

//cout << sum;

avg = sum / values.size();
cout << avg;

return 0;
}

int readInput(vector<int> vect)
{

int count;
int total = 0;

 ifstream inputFile("TopicFin.txt"); //open file

 if(!inputFile)
{
    return 0; // if file is not found, return 0
}

 while(inputFile >> count) //read file
 vect.push_back(count); //add to file

 for (int count = 0; count < vect.size(); count++)
 total+=vect[count]; //sum data in vector

return total;

}

You are not passing your vector by reference, so your function only stores the values in a copy of your vector from main.

int readInput(vector<int>& vect);

this tells your program to pass the the vector by reference meaning anything modified in the function directly modifies your vector in main. If you're new to this stuff check out this post explaining the difference between reference and copy.

You need to pass the vector as a reference or as a pointer. The function just creates a copy of the vector currently passed by value, and manipulates that.

Change the function signature to . . .

int readInput(vector<int>& vect)

Or (perhaps more weirdly for this example) . ..

int readInput(vector<int> *vect)

also changing the function call to

sum = readInput(&values);

Although others have already mentioned the possibility of passing the vector by reference, that is not what I think I'd do in this case. I think I'd just return the vector from the function. I'd also pass the file name to the function:

std::vector<int> values = readInput("TopicFin.txt");

At least to me, this seems to reflect the intent far better. Maybe I'm just a little slow, but it doesn't seem at all obvious from the name that the return value from readInput would be the sum of the values it read.

While returning a vector could theoretically cause an efficiency problem with a compiler that supported neither move construction nor return value optimization, any such compiler is pretty much guaranteed to be so ancient that you really want to avoid it for other reasons anyway.

As far as reading the data into the vector goes, I'd use a pair of istream_iterator s:

std::vector<int> data{std::istream_iterator<int>(infile),
                      std::istream_iterator<int>()};

Of course, given how simple this is, I'd tend to wonder whether it's worth having a separate function like readInput at all.

To sum the values, I'd use std::accumulate :

int total = std::accumulate(data.begin(), data.end(), 0);

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