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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:57:39+00:00 2026-05-31T16:57:39+00:00

I am really trying to be a better programmer, and to make more modular,

  • 0

I am really trying to be a better programmer, and to make more modular, organized code.

As an exercise, I was trying to make a very simple Graph class in C++ with STL. In the code below, my Node object does not compile because the commented line results in a reference to a reference in STL.

#include <set>

class KeyComparable
{
public:
  int key;
};

bool operator <(const KeyComparable & lhs, const KeyComparable & rhs)
{
  return lhs.key < rhs.key;
}

class Node : public KeyComparable
{
public:
  // the following line prevents compilation
  // std::set<Node &> adjacent;
};

I would like to store the edges in a set (by key) because it allows fast removal of edges by key. If I were to store list<Node*>, that would work fine, but it wouldn’t allow fast deletion by key.

If I use std::set<Node>, changes made through an edge will only change the local copy (not actually the adjacent Node). If I use std::set<Node*>, I don’t believe the < operator will work because it will operate on the pointers themselves, and not the memory they index.

I considered wrapping references or pointers in another class, possibly my KeyComparable class (according to the linked page, this is how boost handles it).

Alternatively, I could store the std::list<Node*> and a std::map<int, iterator>' of locations in thestd::list`. I’m not sure if the iterators will stay valid as I change the list.

Ages ago, everything here would just be pointers and I’d handle all the data structures manually. But I’d really like to stop programming C-style in every language I use, and actually become a good programmer.

What do you think is the best way to handle this problem? Thanks a lot.

  • 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-05-31T16:57:41+00:00Added an answer on May 31, 2026 at 4:57 pm

    As you have deduced, you can’t store references in STL containers because one of the requirements of items stored is that they be assignable. It’s same reason why you can’t store arrays in STL containers. You also can’t overload operators without at least one being a user-defined type, which makes it appear that you can’t do custom comparisons if you store pointers in an STL class…

    However, you can still use std::set with pointers if you give set a custom comparer functor:

    struct NodePtrCompare {
        bool operator()(const Node* left, const Node* right) const { 
            return left->key < right->key;
        }
    };
    
    std::set<Node*, NodePtrCompare> adjacent;
    

    And you still get fast removals by key like you want.

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

Sidebar

Related Questions

Im really trying to keep my code clean by using the switch statement but
I'm really trying to get better with this stuff. I'm pretty functional with internationalization
Im trying to better understand how the huffman decoder works. Ive got a code
Like a good programmer I guess I'm trying to be lazy and make the
Trying to learn pointer math better I wrote this code. The intention was to
What I am trying to do is really simple . I'd like to convert
I am really trying to show what htmlentities gives me, but it doesn't give
What i am really trying to achieve is to full control the active TabItem
i am really trying to do this but i am failing to find a
I'm really trying to understand the difference between OpenID and OAuth? Maybe they're two

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.