3

Is an array possible to store int, string and float all at the same time? I have been seeing some array syntax but all starts with int array[] or string array[], is there a way which a array can store all kind of primitive and string values?

Im not very familiar with C++ but in java there is a iterator which can help you roll those stored values out and allow you to display what is stored in there. Does C++ also have this feature?

4
  • @amit, pointers are clearly possible. my experience shows that algorithms can get quite difficult (memory-wise) that way. and: RTTI will get troublesome. Commented Oct 16, 2012 at 17:38
  • 1
    I know this isn't really helpful, but why do you need to do this? Having an array with several types on it can be quite confusing. Could you try modelling your solution differently? Commented Oct 16, 2012 at 17:39
  • Using std::vector is more idiomatic (and maintainable) than using arrays. Commented Oct 16, 2012 at 17:44
  • 2
    For what it's worth, I doubt that a class would give you an assignment that requires boxing types like this. You may be misunderstanding a requirement. Perhaps an opportunity for polymorphism? Commented Oct 16, 2012 at 17:46

2 Answers 2

5

For basic types, this would be easy: they can be mixed into a union, http://www.cplusplus.com/doc/tutorial/other_data_types/ (->Unions).

For complex types, like string, it gets a little more difficult.

You might want to look at boost::variant, http://www.boost.org/doc/libs/1_36_0/doc/html/variant.html or boost::any http://www.boost.org/doc/libs/1_51_0/doc/html/any.html

Sign up to request clarification or add additional context in comments.

3 Comments

boost::variant being preferable as they know all the types they want to store?
C++ allows non-POD types in a union now (with C++11). std::string will work, though of course some extra work is required to correctly manage the union member objects' lifetimes. OTOH boost::variant already does all the work for you.
@bames53 thanks! didn't know that. never got around C++11! How shameful!
5

if these values are related, then create a struct and store that instead of individual values. ex:

struct person {
    string name;
    int age;
};
person pArray[];

4 Comments

great answer, if you don't care about memory footprint. you could also add a type-identifier enum { IS_FLOAT, IS_STRING, ... } to identify the type at runtime.
How would they know which value to use? boost::optional might help?
@RudolfMühlbauer: I don't think he's recommending a union-like thing, he's actually recommending a rethinking of what it means to put multiple objects in an array (as an object)
@MooingDuck yes, i know. still you might be interested to get the actual (inner) type at runtime. i was just suggesting, what seems as 'next steps' to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.