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 8637655
In Process

The Archive Base Latest Questions

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

I have a map with a struct as a value type map<int id, struct_t*>

  • 0

I have a map with a struct as a value type

map<int id, struct_t*> table

struct_t
{
int prev;
int wt;
string name;
}

Using only prev, I need to find the corresponding id. Thanks so much in advance!

EDIT:

int key=0;
for(auto it = table.begin(); it != table.end(); ++it)
{
     if(table[(*it).first].prev == ?)
}

This is how my map data looks like:

id    prev abundance  thing
1573  -1      0       book
1864  1573    39      beds
2075  1864    41      tray
1760  2075    46      cups

For each id, I need to find the NEXT matching id. So, for 1573 from the prev column I need to find a matching ‘id’ which is 1864. Also, std::next doesn’t work because the data set can have the matching ids not necessarily in the next element.Hope this helps!

PLEASE PLEASE help me!!! MY boss is already disappointed that I’m taking so much time to learn C++ (its been 3 weeks already!)

  • 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-12T10:31:46+00:00Added an answer on June 12, 2026 at 10:31 am

    If you’ve got a modern compiler (supports lambdas), you can do the following:

    const int prevToFind = 10;
    auto findResult = std::find_if(std::begin(table), std::end(table), [&](const std::pair<int, struct_t*> &pair)
    {
        return pair.second->prev == prevToFind;
    });
    
    int foundKey = 0; // You might want to initialise this to a value you know is invalid in your map
    struct_t *foundValue = nullptr
    if (findResult != std::end(table))
    {
        foundKey = findResult->first;
        foundValue = findResult->second;
    
        // Now do something with the key or value!
    }
    

    Let me know if you have an older compiler, and I can update the example to use a predicate class instead.

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

Sidebar

Related Questions

I have this custom type: public struct PasswordString { private string value; public PasswordString(string
I need an OCaml map with keys of type int so I am using
I have a recursive data type like this: template<typename T> struct SomeType { std::map<T,
I have a map<int int> . I need to get a vector of the
Suppose I have a struct containing a std::string, like this: struct userdata{ int uid;
I have a stl::map which key type is a custom struct. I want to
I have a map with various markers and i need to be able to
I have a problem putting data into an unordered_map using a struct as key:
I have a custom user type that is used to map from a decimal
I have struct that store info about ARP packet and I need to save

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.