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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:52:24+00:00 2026-05-28T04:52:24+00:00

I would like to store equivalences from Connected-component labeling algorithm . It’s basically making

  • 0

I would like to store equivalences from Connected-component labeling algorithm. It’s basically making a kind of map from one value (one label’s ID) to multiple values (IDs from labels that are equivalent to the former.)

I have already done something like this but it does not work really well:

std::map<unsigned short, std::list<unsigned int>> equivalences;
for(int i = 0; i < MAX_NUMBER_OF_LABELS; ++i )
{
    std::list<unsigned int> temp;
    temp.push_back(i);
    // note that a label is equivalent to itself
    equivalences.insert( std::pair< int, std::list<unsigned int>>(i, temp) );
}

Then I add proper equivalence by:

equivalences.at( i ).push_back( equivalent_labels_int );

The main drawback of this method is that I have to declare map‘s size up front (it has to be big enough) and then for large sizes (e.g. 9999) the initialization time is approximately 2.5s.

Anyone have a better idea?

  • 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-28T04:52:24+00:00Added an answer on May 28, 2026 at 4:52 am

    You do not need to size the map up-front in C++ (or most languages, for that matter). maps can dynamically grow by having new elements added into them, so if you find a new key, you can always add it to the map. For example:

    equivalences[i].push_back(equivalent_labels_int);
    

    This works because the map‘s square brackets operator (operator[]) will automatically add a new key/value pair to the map with the given key and a default value if one doesn’t already exist.

    Additionally, I would advise not using list as the container for storing the sequence of connected blobs. list is good when you don’t need random access and are frequently removing elements in the middle of the sequence, which I don’t think you’re actually doing here. Instead, I would suggest using vector or deque, since those structures are more space efficient and have better locality.

    Finally, depending on your particular needs, you may want to switch data structures entirely. If your algorithm works by running a depth-first search out from some starting point and then storing all of the results it encounters, the approach you have now may be quite good. However, if instead your algorithm works by finding pairs of points that are similar and then merging together the blobs they contain, you may be interested in the disjoint-set forest data structure, which has a simple implementation but extremely good performance. That said, using this structure loses you the ability to check what points are connected to a given point, but the boost in efficiency is pretty remarkable.

    Hope this helps!

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

Sidebar

Related Questions

I would like to store the cookies from one open-uri call and pass them
I would like to store and load numpy arrays from binary files. For that
I would like to store some objects only for one request through the session
I would like to store the properties I read from a Java type property
I have a certificate .p12 and .crt and I would like store the public
I would like to store log4net config data in my application.config file. Based on
I would like to store an object FOO in a database. Lets say FOO
I would like to store large dataset generated in Python in a Django model.
I would like to store lengthy .sql scripts in my solution and execute them
I would like to store very large sets of serialized Ruby objects in db

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.