Question:
C++ newbie: how can I return an unknown number of vectors from a function?
Antst
2011-09-22 06:16:34 UTC
Hello everyone:

I have written a function for extracting datasets from a file into vectors. The datasets are then used in a calculation. In the file, a user writes each dataset on a line under a heading like ). The result is i vectors by the time the function finishes. The function works just fine.

My problem is that I don't know how to get the vectors out of the function! What I mean is, (1) I think I can only return one entity from a function. So I can't return i vectors. Also, (2) I can't write the vectors/datasets as function parameters and return them by reference because the number of vectors/datasets is different for each calculation.

I'm sure this is a silly question, but am I missing something here? I would be very grateful for any suggestions!

For each calculation, I DO know the number of vectors/datasets that the function will find in the file because I have that information written in the file and I extract it. Is there some way I could use this information?
Five answers:
Don't sue me!
2011-09-22 06:48:39 UTC
Return a vector of pointers to vectors.

Create a new vector of pointers that point to every vector you have.

This depends on you vector class implementation, but it should be close enough.

Example:



vector vectors;

vectors.add(vector_1);

vectors.add(vector_2);

...



Make the return type for the function "vector" (not vector*) and return this vector.



Note: This may cause some trouble because of object lifetime, so you need to be careful with objects created on the stack since they will be destroyed as soon as the function returns.



There's also a more efficient solution, which is to create a vector of pointers to vectors (like above) in the function that calls this function, and pass it in as reference, populate the vector inside the function and return either void or a bool to indicate success.
Bob M
2011-09-22 06:46:37 UTC
You return a pointer to the array of vectors.



MyVectorType ptrMyVectors[];



private MyVectorType* GetVectorArray()

{

//in here your array is created, for example you might say

ptrMyVectorType = new MyVectorType[5];

//fill in the data

//Return the reference to the array

return &ptrMyVectorType;

}

MyVectorType* ptrData = GetVectorArray;



or, in this case, you could use the array directly ptrMyVectorType->GetFirst() (example only) and then miss out the return statement. But remember the key is reusability, so writing a class or a function that is not dependent on variables in a particular class is advisable, so go for returning the pointer instead.



Note that these days on shared memory systems (Linux , Apple and Windows) you only ever deal with references to memory, but you can treat them as if they were actual memory. It means that some of the work you as a programmer would have had to do at one time is now done for you, such shuffling memory to get a larger contigious memory area. The other plus with modern memory systems is that you are never denied the memory request except in C# and VB and if you choose 'managed' C++ in Windows. This is because it uses only Stack so that when it does the automatic clean up after your program finishes, all it really does is remove it's own stack. So on those you have a 2MB limit, a bit of a pain if you are working with high end graphics, so for those situations you need C++ outside of the managed code system, you get better speed anyway, which you need in graphics.



But if you are in that situation, in managed C++, then can I suggest you take advantage of it while you write the nuts and bolts of your software, you get the just in time debugging etc. Then when you have the bulk of it written change to unmanaged. You will instantly get more warnings, but not a hard job to sort them out.
AnalProgrammer
2011-09-22 06:43:37 UTC
The answer as I see it is to create a VectorFile class that will contain an array of vectors. There will also be a variable or a method that identifies the number of vectors that the class contains.



Alternatively just return the array of vectors. However if you do this then you will not know how many vectors are being returned.



Have fun.
favela
2016-12-02 02:06:38 UTC
right this is what i could do. each and every char has a quantity with regards to it. in case you look at an ascii table, a cut back down case 'a' has a decimal quantity of 97. you could increment chars equivalent way as you need to an integer. So your place must be some difficulty like this: declare a char and set it to ninety six. char ch = ninety six; then upload the guy inputted quantity. So if its an enter of a million, you need to upload a million to ch, optimal to a magnitude of 97 or an 'a'.
oops
2011-09-22 07:10:14 UTC
There is a template class in C++ called 'std::vector' that encapsulates a contiguous array, and that just happens to be the thing you need here. So don't get it confused with the vectors in your file. So, it goes something like this:



    #include

    #include

    #include

    #include



    // I'm just making this up, I don't know how

    // your vector data structure is laid out.

    struct Vector

    {

        int x,y,z;

    };



    std::istream & operator>>(std::istream & is, Vector & v)

    {

        return is >> v.x >> v.y >> v.z;

    }



    std::vector LoadVectors(const std::string & filename)

    {

        std::vector vec;

        std::ifstream fin(filename.c_str());

        Vector v;

        while (fin >> v)

            vec.push_back(v);

        return vec;

    }



    int main()

    {

        std::vector vec = LoadVectors("file.dat");

    }



@Don't sue me! or anyone else who thinks it's inefficient to return large objects by value, please read this:

http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...