How to store different types into a single array?

Hi guys, i've been wondering how to store different types into a single array.

For eg. cin >>:
xCoordinate(int)
yCoordinate(int)
type(string)
noOfThings(int)
noOfStuff(int)
density(double)
price(double)

and how to I store into array and compute some of the data?
Thanks for the reply in advance.
Arrays are homogeneous. It is possible to write a heterogeneous container. Here's my post on how to do that : http://www.cplusplus.com/forum/beginner/52963/#msg286873
But then, what use could you possibly have from it? I know it sounds good when you don't have it, but in reality it is quite useless. Unless polymorphism was what you were looking for.
To my knowledge you can't do that with a single array as an array's type needs to be set.

You could create a struct or a class to hold this information then have an array of the struct you declared, but how useful that would be is debatable.

If you could elaborate maybe we can find a better answer for you.
Well for single arrays you have to declare the data type and size I believe. Now if you are wanting them all to be clumped together like one group as all those, then you would want to use a struct or class and have arrays of those types in it.

1
2
3
4
5
6
7
8
9
10
struct groupData
{
   int xCoordinate[3] = {0}; 
   int yCoordinate[3] = {0};
   string type[4] = {""};
   int noOfThings[4] = {0};
   int noOfStuff[4] = {0};
   double density[4] = {0};
   double price[4] = {0};
};


Unless I'm misunderstanding. Though this may be completely useless to you compared to just declaring them separately. I just threw this together so I may have made a lot of errors so please correct me if I have.
Last edited on by closed account z6A9GNh0
Sorry for the vague info. I did separate the types into different class file.

class Point
{
int x;
int y;
float result;
get&set() for x&y.
};

class Location
{
string type;
int noOfThings;
int noOfStuff;
float density;
float price;
get&set() for noOfThings, noOfStuff, density, price.
};

class Plan: public Point, public Location
{
methods();
};

whereas methods() i put in int main() to cin >> all objects and display
eg. Computation recorded. (1 records were updated)

Assuming that its loop 5 times and 5 statistical data records input so far.

The question is how do I put the computed float result and coords (x, y) into array so that i can cout<< the total records of input.
You're going to have to post the actual code because we need to see how you implemented everything.
@euds, I find your post hard to understand.
If you want cout<< my_plan.methods(); to work, the best choice would be to have methods return a string (which you would build using a stringstream).
I don't see what you mean by looping.
Well there are two ways right off that I can think of. Make a float array of 15 components and save x, y, and result to the array. Second way would be to have two int arrays of 5 components and a float array of 5 components and save them that way. Third possible way would be an 2D array that stores x and y and a float array for result and then use for loops to input and output the information.
Make three vectors, each with their own iterator. One for the computed float result, and one each for x and y coordinates. Make sure that you make the iterators always advance in unison, so they're always pointing at the same location in each of their separate vectors.

This way, you only need to cout the value that each iterator is pointing to, and then advance the iterators. If you add, delete, or change the location pointed by ANY of the iterators, you will have to do it to all three, or else you will get corrupted data at run time, even though it will compile.

Edit: Spelling errors.
Last edited on
Topic archived. No new replies allowed.