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

  • SEARCH
  • Home
  • 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 8884601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:05:59+00:00 2026-06-14T21:05:59+00:00

Am i doing this right, I want a map with a Integer as key,

  • 0

Am i doing this right, I want a map with a Integer as key, and struct as value. What is the easiest way to, say I want the object at 1. How do I retrieve the value of isIncluded? The last two lines in the code, I tried doing it, but then I realized I don´t really know what is the way to retrieving values of structs in a numbered Map array.

Do I need to call cells.get(1) and assign that to a new temporarely struct to get its values?

/** set ups cells map. with initial state of all cells and their info*/
void setExcludedCells (int dimension)
{
    // Sets initial state for cells
    cellInfo getCellInfo;
    getCellInfo.isIncluded = false;
    getCellInfo.north = 0;
    getCellInfo.south = 0;
    getCellInfo.west = 0;
    getCellInfo.east = 0;

    for (int i = 1; i <= (pow(dimension, 2)); i++)
    {
        cells.put(i, getCellInfo);
    }
    cout << "Cells map initialized. Set [" << + cells.size() << "] cells to excluded: " << endl;
    cells.get(getCellInfo.isIncluded);
    cells.get(1);
}

the Map, is declared as an private instance variable like this:

struct cellInfo {
    bool isIncluded;
    int north;  // If value is 0, that direction is not applicable (border of grid).
    int south;
    int west;
    int east;
};
Map<int, cellInfo> cells;       // Keeps track over included /excluded cells
  • 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-14T21:06:00+00:00Added an answer on June 14, 2026 at 9:06 pm

    From the documentation for Map, it appears that .get() returns a ValueType.

    You would use it thus:

    // Display item #1
    std::cout << cells.get(1).isIncluded << "\n";
    std::cout << cells.get(1).north << "\n";
    

    Or, since the lookup is relatively expensive, you could copy it to a local variable:

    // Display item #1 via initialized local variable
    cellInfo ci = cells.get(1);
    std::cout << ci.isIncluded << " " << ci.north << "\n";
    
    // Display item #2 via assigned-to local variable
    ci = cells.get(2);
    std::cout << ci.isIncluded << " " << ci.north << "\n";
    

    My best advice is to use the standard library’s std::map data structure instead:

    // Expensive way with multiple lookups:
    std::cout << cells[1].isIncluded << " " << cells[1].north << "\n";
    
    // Cheap way with one lookup and no copies
    const cellinfo& ci(maps[1]);
    std::cout << ci.isIncluded << " " << ci.north << "\n";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Am I on the right tract with this? I'm switching my way of doing
What is the most efficient way to do this? Right now I am doing
Im not sure if I'm doing this right. I have an action that I
Not too sure if I'm doing this right. It looks a bit more convoluted
I don't know what's wrong. It seems like I'm doing this right. I'm trying
Right now I'm doing this: SELECT * FROM messages WHERE location_id = 7 AND
Hi so this may sound odd but lets say currently I am doing this:
I don't know if I'm doing this right, c++ is not a native language
If I want the unordered map find function to return a bool value, how
Am I doing this right? I have a client, they currently have a portal

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.