Monster
Monster

Reputation: 7

More than one variable type in array

I am busy with a dynamic 2d array and I have declared it as follows:

string** MakeArray(int row,int col)
{
    string** array;
    array = new string* [col];

    for(int r = 0; r < row; r++)
    {
        array[r] = new string [col];
    }
    return array;
}

Now I can place string values in the array. How can I place Integer values in the first column and strings in second and integers in third, if my array is 4 by 99?

Upvotes: 0

Views: 266

Answers (4)

yuri kilochek
yuri kilochek

Reputation: 13486

Don't do that. Instead create a struct that will represent single record in a table, and contain a string and two integers. Then create one dimensional array of those structures.

struct record
{
    int a;
    std::string b;
    int c;
};

record* MakeArray(int row)
{
    return new record[row];
}

better yet, ditch arrays and use std::vector:

std::vector<record> array(99); 

Upvotes: 2

Jerry Coffin
Jerry Coffin

Reputation: 490028

The elements in an array are all the same type. To get what you're after, you probably want to start off rather differently, with an array of structs:

struct whatever { 
    int a;
    std::string b;
    int c;
};

std::vector<whatever> your_array;

Edit: although it's almost certainly a lousy idea, if you really need this to be a 2D array, you could try making all your elements the same type of union:

union whatever { 
    int a;
    std::string b;
};

This has some pretty severe limitations though -- in fact, putting a std::string in a union isn't officially supported at all. There's a fairly decent chance it'll work, but no guarantee of it at all. If you really, really need to do something like this, you can make that member of the union a pointer instead. That is guaranteed to work, but also guaranteed to be so clumsy that making mistakes with it is nearly inevitable.

Upvotes: 3

Thomas Russell
Thomas Russell

Reputation: 5970

Have you looked at having a vector/array of tuples, if you have C++11 available to you? So you could do something such as:

#include <tuple>
#include <vector>

typedef std::tuple<int, std::string, int> MyTuple;

std::vector<MyTuple> MakeVector()
{
    std::vector<MyTuple> vecTuples;

     for( int i = 0; i < 5; ++i )
     {
         vecTuples.emplace_back( std::make_tuple<int, std::string, int>( i, "Test"+i, i+5 ) );
     }

     return vecTuples;
}

Upvotes: 1

YePhIcK
YePhIcK

Reputation: 5856

C++ is a "strong-typed" language. One of the things that means is you cannot mix data types (unless they are related, like base-derived class hierarchical relationship).

In other words what you are doing is not what C++ directly supports.

Having said that there's something you can do that would do what you want - have an array of triplets, like this:

struct triplet
{
  int first;
  string second;
  int third;
};

triplet** MakeArray(...

What you are doing in your example looks alot like a JS code though. Maybe what you want is to store all your data as strings? Then yes, you can use a 2D array of strings, but that requires you to convert datum into string when storing it and converting back from string for calculations. Which is a major performance issue

Upvotes: 0

Related Questions