Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8621197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:45:59+00:00 2026-06-12T06:45:59+00:00

In c++, I am attempting to create an array to store two different datatypes.

  • 0

In c++, I am attempting to create an array to store two different datatypes. Essentially, I want an ID and the actual datatype.

psuedocode:

array[int][myObj]

0, myObj1
1, myObj2
2, myObj3

I understand this is not how to declare an array. Is this functionality possible? Do I have to create a custom class with a struct?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T06:46:01+00:00Added an answer on June 12, 2026 at 6:46 am

    It depends on how you want to use it. There’s a number of options that all have valid use-cases:

    1. A simple array of myObj: myObj array[]. This matches the example you’ve given because the associated ID of an object is simply the index of that object in the array. The first element has ID 0, second has ID 1, etc. However, for the sake of flexibility and good C++ style, you should really use either std::vector or std::array.

    2. If you want to associate each object with a fixed ID regardless of its position in the array (and possibly in other places too). You can use a std::pair<int, myObj> to pair an integer to an instance of your object. You would use it like so:

      std::vector<std::pair<int, myObj>> v;
      v.push_back(std::make_pair(0, myObj1));
      

      A similar alternative to this is to encapsulate the ID within your myObj class. However, this may not be appropriate in certain cases (ID is really not part of myObj, separate IDs may be used for the same object at any time).

    3. If you need to be able to access the element in the array by its ID, you want an std::map<int, myObj>. This will map integer IDs to the instances of myObj. Use it like so:

      std::map<int, myObj> m;
      m.insert(std::make_pair(0, myObj1));
      

      Now you can access an element with, for example, m[0]. If you want to speed up access time and don’t care about the elements being ordered, you could use a std::unordered_map instead

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to create an array of strings that represent the directories stored in
I am attempting to create a basic Hangman program. It's an array of tags
I'm attempting to create a search function, built something like this: function Search(array, key,
I'm attempting to create a 2D array of integers that load in data from
I am attempting to create a character array of a fixed size on the
I am attempting to dynamically create the following using data from an array <li><a
I'm attempting to create an array from parsed JSON. I know that the JSON
I'm attempting to create a method that checks an array for increasing elements. True
While attempting to create a replacement tablespace for USER, I accidentally created a datafile
Im attempting to create a new operator :? on lists, which operates the same

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.