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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:47:43+00:00 2026-05-30T17:47:43+00:00

Possible Duplicate: C++ STL set update is tedious: I can't change an element in

  • 0

Possible Duplicate:
C++ STL set update is tedious: I can't change an element in place

I want to use a std::set<> to count the number occurences of a certain value and simultaneosly sort the objects. For this I created a class RadiusCounter

class RadiusCounter
{
public:
    RadiusCounter(const ullong& ir) : r(ir) { counter = 1ULL; }
    void inc() { ++counter; }
    ullong get() const { return counter;}
    ullong getR() const { return r;}
    virtual ~RadiusCounter();
protected:
private:
    ullong r;
    ullong counter;
};

(the destructor does nothing) together with comparison operators:

const inline bool operator==(const RadiusCounter& a, const RadiusCounter& b) {return  a.getR() == b.getR();}
const inline bool operator< (const RadiusCounter& a, const RadiusCounter& b) {return a.getR() < b.getR();}
const inline bool operator> (const RadiusCounter& a, const RadiusCounter& b) {return a.getR() > b.getR();}
const inline bool operator!=(const RadiusCounter& a, const RadiusCounter& b) {return a.getR() != b.getR();}
const inline bool operator<=(const RadiusCounter& a, const RadiusCounter& b) {return a.getR() <= b.getR();}
const inline bool operator>=(const RadiusCounter& a, const RadiusCounter& b) {return a.getR() >= b.getR();}

now I want to use it like this:

set<RadiusCounter> theRadii;
....
ullong r = getSomeValue();

RadiusCounter ctr(r);
set<RadiusCounter>::iterator itr = theRadii.find(ctr);

// new value -> insert
if (itr == theRadii.end()) theRadii.insert(ctr);

// existing value -> increase counter
else itr->inc();

But now the compiler complains at the line with the call to itr->inc():

error: passing 'const RadiusCounter' as 'this' argument of 'void RadiusCounter::inc()' discards qualifiers

Why is the instance in *itr a const here?

  • 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-30T17:47:45+00:00Added an answer on May 30, 2026 at 5:47 pm

    As addition, it seems you want just

    typedef int Radius;
    typedef int Counter
    std::map<Radius, Conunter>theRadii;
    
    ...
    
    theRadii[getSomeValue()]++;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: C++ STL set update is tedious: I can't change an element in
Possible Duplicate: Which sorting algorithm is used by STL’s list::sort()? Which sorting algorithm can
Possible Duplicate: When to use std::size_t? I have a lot of constants in my
Possible Duplicate: STL containers element destruction order Is there a guarantee the the elements
Possible Duplicate: In STL maps, is it better to use map::insert than []? I
Possible Duplicate: Can I list-initialize a vector of move-only type? I use gcc 4.6.1
Possible Duplicate: Why does ReSharper want to use 'var' for everything? I have ReSharper
Possible Duplicate: How to: URL re-writing in PHP? How can a website use an
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: Hashtable in C++? I was originally planning on using a C++ set

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.