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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:12:39+00:00 2026-06-18T11:12:39+00:00

I have the following map in C++ (gcc): map<int, EdgeExtended> myMap; where the definition

  • 0

I have the following map in C++ (gcc):

map<int, EdgeExtended> myMap;

where the definition of EdgeExtended is:

struct EdgeExtended {
    int neighborNodeId;
    int weight;
    int arrayPointer;
    bool isCrossEdge;

    EdgeExtended(Edge _edge, int _arrayPointer) {
        neighborNodeId = _edge.neighborNodeId;
        weight = _edge.weight;
        arrayPointer = arrayPointer;
        isCrossEdge = _edge.isCrossEdge;
    }

    EdgeExtended(const EdgeExtended & _edge) {
        neighborNodeId = _edge.neighborNodeId;
        weight = _edge.weight;
        arrayPointer = _edge.arrayPointer;
        isCrossEdge = _edge.isCrossEdge;
    }

    EdgeExtended(int _neighborNodeId, int _weight, bool _isCrossEdge, int _arrayPointer) {
        neighborNodeId = _neighborNodeId;
        weight = _weight;
        arrayPointer = _arrayPointer;
        isCrossEdge = _isCrossEdge;
    }

    void setValues(int _neighborNodeId, int _weight, bool _isCrossEdge, int _arrayPointer) {
        neighborNodeId = _neighborNodeId;
        weight = _weight;
        arrayPointer = _arrayPointer;
        isCrossEdge = _isCrossEdge;
    }

    EdgeExtended() {
        neighborNodeId = -1;
        weight = -1;
        arrayPointer = -1;
        isCrossEdge = false;
    }
};

I want to do this (plain example):

EdgeMap edge;
int nodeId=18;

edge=map.erase(nodeId);

a) Is this code correct, does erase return the object that corresponds to the key? b) If yes, what does erase return when the key is not present? c) if this code is wrong, how can I check if a key is present, the object mapped to the key and then erase the pair from map. Keep in mind that performance is rather crucial, so I need the most efficient way.

  • 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-18T11:12:40+00:00Added an answer on June 18, 2026 at 11:12 am

    a) No, it is not correct. The std::map::erase method you call returns the number of erased elements.

    What you can do is use std::map::find to check if the an element with the given key is in the map. This returns an iterator to the element if it exists, to end() if it doesn’t. You can pass this iterator to the relevant std::map::erase overload.

    EdgeMap edge;
    int nodeId=18;
    ExtendedEdge removedEdge;
    ....
    EdgeMap::iterator it = edge.find(nodeId);
    if( it != edge.end() ) 
    {
      // found element.
      removedEdge = it->second; // or removeEdge = std::move(it->second) in C++11
      edge.erase(it);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following function: func addCatsToMap(m map[string][]CatHouse, meowId int, treats Set, dog *Dog)
I have the following code. template<class key,class val> bool has_key(key chkey,std::map<key,val> map){ for (std::map<key,val>::iterator
I have the following map structure: map < pair < int,int >, object* >
I have the following map in Scala: var m = Map[Int,Set[Int]]() m += 1
I have the following code: Map<String, ObjectType> objectMap = new HashMap<String, ObjectType>(); for (ObjectType
I have the following code private Map<KEY, Object> values = new HashMap<KEY, Object>(); public
suppose i have the following: boost::unordered_map< string , someValueType > map; someValueType& value =
I have the following enum how do i map in jna ?? This enum
I have the following mapping: public class LogEntryMap { public LogEntryMap() { Map.Id(x =>
I have been following a tutorial on how to write a basic tile map

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.