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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:58:30+00:00 2026-05-25T14:58:30+00:00

This question is more about the algorithm and the right use of function rather

  • 0

This question is more about the algorithm and the right use of function rather than the actual code.

In my code, I’m using map to simulate boxes. The maps elements are composed of vector<int> as keys and set<shared_ptr<foo> > as values.

I’m doing a nested loop to go over all the boxes:

mit1 = boxes.begin(); //mit1 is an appropriate iterator
int edge = 10;//represnd periodic boundary conditions
while (mit1 != boxes.end()){
  vector<t> = mit1->first;
  mit2 = mit1++;
  while (mit2 != boxes.end()){
    vector<int> u = (mit2++)->first;
    bool good = true;
    for (int i = 0; i < 3 && good; i++){
      u[i] = (int)fabs(u[i] - t[i]);
      good = u[i] == 0 || u[i] == 1 || u[i] == edge;
    }
    if (!good) continue;
  }
}

My concern is with the whole nested loop as well as the for loop.
Would you consider it to be more efficient to have a function to calculate all the neighboring boxes? Do you know of any better way to do the for loop test?

  • 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-25T14:58:30+00:00Added an answer on May 25, 2026 at 2:58 pm

    Same advice as with every collision detection: Use some algorithm or data structure that allows you to pre-filter candidates for your loop by their spatial distance, and allows to do this pre-filtering in O(n). A quad tree comes to mind, or a coarse-grained spatial map.

    Edit: To make the whole idea a little bit clearer, consider the following algorithm: You have N particles in a 3D space and want to find out which particles are closer than a distance D to each other. You build a 3D array, with each bin representing a cubic volume of your target space, and each volume must be at least of size D. To find all particles that might be closer than D to a given particle X, you determine the array cell Ax in which X is currently and select all particles from the Ax and all surrounding cells. You only need to check this small subset for collisions.

    When using M array cells, the average-case computational complexity for the whole distance check is now O(N*N/M) instead of O(N^2), however at the cost of O(M^2) space.

    If your target space is unbounded, use a quad tree (2D) or an oct tree (3D).

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

Sidebar

Related Questions

This is kind of more generic question, isn't language-specific. More about idea and algorithm
EDIT: This question is more about language engineering than C++ itself. I used C++
More than about LINQ to [insert your favorite provider here], this question is about
I'm not talking about making portable code. This is more a question of distribution.
This is more of a conceptual question than an actual implementation and am hoping
So I'm trying to learn more about lambda expressions. I read this question on
This isn't really a programming question but more about programming and testing tools. Is
I got this in an interview question -- the question was more about what
I know there is already a question about this but my issue is more
This question is more UI/Design-ish than hard-core programming is. Background: I've been coding in

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.