array with ints strings and doubles

Hi folks!
Could you advise the array type to contain both ints, doubles and strings.
Class is not an option
An array can only hold values of one type. Usually if you want multiple types you'd use a struct:

1
2
3
4
5
6
7
struct mydata {
int i {};
double d {};
std::string s;
};

mydata myarray[] {{0, 1.1, "qwe"}, {1, 3.3, "asd"}};

Do it in python with a list.

Actually, I’ve just noticed - your record in keeping posts isn’t exactly great.
Last edited on
Statically typed languages like C++ doesn't make such things so easy. Consider if there is another way to go about it where you don't have to store different types in the same container. That said, C++17 introduced std::variant which is designed to store one of many different types.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
std::vector<std::variant<int, double, std::string>> list;

list.push_back(5);
list.push_back(1.7);
list.push_back("Hello");

for (const std::variant<int, double, std::string>& e : list)
{
	if (const int* int_ptr = std::get_if<int>(&e))
	{
		std::cout << "int: " << *int_ptr << "\n";
	}
	else if (const double* dbl_ptr = std::get_if<double>(&e))
	{
		std::cout << "double: " << *dbl_ptr << "\n";
	}
	else if (const std::string* str_ptr = std::get_if<std::string>(&e))
	{
		std::cout << "string: " << *str_ptr << "\n";
	}
}
int: 5
double: 1.7
string: Hello
Last edited on
Why give you advice when you'll just delete the question after you've hoovered up the free help, impetus.
Why give you advice when you'll just delete the question after you've hoovered up the free help, impetus.

we already discussed it, i put whole banch of code i would not share with others.. It will not happen again.
My appologies for that case...




An array can only hold values of one type. Usually if you want multiple types you'd use a struct:


struct mydata {
int i {};
double d {};
std::string s;
};

mydata myarray[] {{0, 1.1, "qwe"}, {1, 3.3, "asd"}};



Struct might be fine! But what is maximum item quanity to be held by struct?
I need about 12 items, mix ints and doubles, two or three strings...

Thanks! This one looks familiar to QVariant, which is perfect for some cases, but you actually can not easy get std string for example.. I will test both 1) struct and 2) C++ variant and update here.
I hope it has simplicity and power of simple vector of array


Many thanks



Peter87 (10193)
Statically typed languages like C++ doesn't make such things so easy. Consider if there is another way to go about it where you don't have to store different types in the same container. That said, C++17 introduced std::variant which is designed to store one of many different types.




std::vector<std::variant<int, double, std::string>> list;

list.push_back(5);
list.push_back(1.7);
list.push_back("Hello");

for (const std::variant<int, double, std::string>& e : list)
{
if (const int* int_ptr = std::get_if<int>(&e))
{
std::cout << "int: " << *int_ptr << "\n";
}
else if (const double* dbl_ptr = std::get_if<double>(&e))
{
std::cout << "double: " << *dbl_ptr << "\n";
}
else if (const std::string* str_ptr = std::get_if<std::string>(&e))
{
std::cout << "string: " << *str_ptr << "\n";
}
}
Last edited on
If'n you want to have access to multiple types at once instead of one differing type at a time consider C++11's std::tuple.

http://www.cplusplus.com/reference/tuple/tuple/

Clarifying your intended use would be helpful.

Couple a tuple with C++17's structured binding and accessing the stored data will be more intuitive.

https://en.cppreference.com/w/cpp/language/structured_binding

Also consider using a C++ container rather than a regular array. std::vector is a decent, generic choice. As Peter87 shows.

http://www.cplusplus.com/reference/vector/vector/

C++ has other containers available that differ on how they can be used, how elements are laid out in memory, etc.

If'n you need a fixed size container C++ has std::array.

http://www.cplusplus.com/reference/array/array/

Regular arrays have some issues, such as devolving to pointers when passed into a function requiring the array's size be passed as well.

std::array requires a bit more coding verbiage to work, so might not be a quick rewrite solution to existing code as using a vector could be.
Last edited on
Depending upon usage, a std::tuple could be a viable option instead of struct if you want to hold multiple values at once of different types - although IMO a struct is easier to use if possible. If just want to hold one value that could be of several types, then std::variant or even std::any.

But what is maximum item quanity to be held by struct?
I need about 12 items, mix ints and doubles, two or three strings...


I don't know of one. I guess available memory. There's certainly no issue with a struct having 12 members.

What's the data being used for? Why not a class to hold the data but a struct is OK? The difference between a struct and a class is that for a struct members are public by default and for a class they are private.

Last edited on
impetus wrote:
Class is not an option

Why not?


Why don't you just state what your actual application is. Otherwise we are just dealing with an XY problem.

Hi folks!
Could you advise the array type to contain both ints, doubles and strings.
Class is not an option

Struct might be fine! But what is maximum item quanity to be held by struct?
I need about 12 items, mix ints and doubles, two or three strings...

Thanks! This one looks familiar to QVariant, which is perfect for some cases, but you actually can not easy get std string for example.. I will test both 1) struct and 2) C++ variant and update here.
I hope it has simplicity and power of simple vector of array




there is almost 0 diff in struct vs class, just default access. If a class won't work, neither will a struct?

there is no inherit limit to a struct or class size. The computer is finite, as is the compiler, so probably 60+GB or so worth of junk?

you can do this any of many ways. The top 3 takes:

the variant type of C++ is the starting point. Some third party take on it is fine too, but why do that? The one in C++ is fine.
if that won't work, a struct that holds the data and an indicator as to what type it currently is can do it.
if you don't like those 2, you can just shovel bytes around. A C-string array of 8+ bytes can hold anything except a large string, so if you put in a C string of sufficient size, you can store anything at all inside it. Then you can get a little cute and make the first byte of this array be the type (from an enum or whatever) and the first to nth bytes be the data (or cuter still, the size can be the second set of bytes, up to 8 for 64 bit integer). That can hold anything at all... your custom class, an array of doubles, 10 integers -- even a C++ string (via raw bytes, using the char array/ c-string just as a binary bitdump). This is basically serialized data as if you had read it from a binary file, but it will do the job, and you can directly cast it to a struct/class to bust it out into fields you can touch again.

Last edited on
Topic archived. No new replies allowed.