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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:38:49+00:00 2026-05-10T19:38:49+00:00

I find myself writing code that looks like this a lot: set<int> affected_items; while

  • 0

I find myself writing code that looks like this a lot:

set<int> affected_items; while (string code = GetKeyCodeFromSomewhere()) {     if (code == 'some constant' || code == 'some other constant') {         affected_items.insert(some_constant_id);     } else if (code == 'yet another constant' || code == 'the constant I didn't mention yet') {         affected_items.insert(some_other_constant_id);     } // else if etc... } for (set<int>::iterator it = affected_items.begin(); it != affected_items.end(); it++) {     switch(*it)     {         case some_constant_id:            RunSomeFunction(with, these, params);         break;         case some_other_constant_id:            RunSomeOtherFunction(with, these, other, params);         break;         // etc...     } } 

The reason I end up writing this code is that I need to only run the functions in the second loop once even if I’ve received multiple key codes that might cause them to run.

This just doesn’t seem like the best way to do it. Is there a neater 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. 2026-05-10T19:38:49+00:00Added an answer on May 10, 2026 at 7:38 pm

    Since you don’t seem to care about the actual values in the set you could replace it with setting bits in an int. You can also replace the linear time search logic with log time search logic. Here’s the final code:

    // Ahead of time you build a static map from your strings to bit values. std::map< std::string, int > codesToValues; codesToValues[ 'some constant' ] = 1; codesToValues[ 'some other constant' ] = 1; codesToValues[ 'yet another constant' ] = 2; codesToValues[ 'the constant I didn't mention yet' ] = 2;  // When you want to do your work int affected_items = 0; while (string code = GetKeyCodeFromSomewhere())     affected_items |= codesToValues[ code ];  if( affected_items & 1 )     RunSomeFunction(with, these, params); if( affected_items & 2 )     RunSomeOtherFunction(with, these, other, params); // etc... 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I find myself writing a lot of code in my views that looks like
I find myself writing code that looks something like this: $(document).ready(function(){ updateStuff(); $('select').change(function(){ updateStuff();
I frequently find myself writing code like this: List<int> list = new List<int> {
I am finding I'm writing a lot of code that looks like this: <div
I find myself writing a lot of functions in PHP that return HTML code.
I often find myself writing a property that is evaluated lazily. Something like: if
I have a string that looks like this: Name1=Value1;Name2=Value2;Name3=Value3 Is there a built-in class/function
I find myself writing a lot of repetitive code in UITableView, especially for config
In my day-to-day work, I often find myself writing classes like in this simplified
I find myself writing this class often in my python code when I need

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.