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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:03:49+00:00 2026-05-25T03:03:49+00:00

I am trying use an iterator to go through a set and then do

  • 0

I am trying use an iterator to go through a set and then do something with the members of that set(if there are any). The problem is that, normally this works, but sometimes, it compares the beginning and the end of an empty set and finds them not to be equal.

The code snippet of interest is:

    for(int i=0;i<input_data.num_particles();i++)
    {
        //loop through pairs contained in particle i's Verlet list
        set<int>::iterator iter;
        for(iter=verlet_vars.verlet()[i].begin();iter!=verlet_vars.verlet()[i].end();iter++)
        {
            //call the force() function to calculate the force between the particles
            force(particles.getpart(i),particles.getpart(*iter),input_data,*iter);  
        }
    }

Sometimes, even though the set contained in verlet_vars.verlet()[i] is empty, the program compares the iterator to the end of the set and finds them not equal, and so it enters the inner loop(ultimately causing the program to crash by trying to call the force() function). What is bizarre is if I do anything with the iterator before the inner loop is called,like doing something like:

iter=verlet_vars.verlet()[i].begin();

then, the comparison for the inner loop always returns true, and the program runs normally.

P.S. the command verlet_vars.verlet()[i] calls a vector of sets, hence the [i]

The verlet() function:

std::vector<std::set<int> > verlet() const {return _verlet;}

Thanks for your time.

  • 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-25T03:03:49+00:00Added an answer on May 25, 2026 at 3:03 am

    Your verlet_vars.verlet() function returns by value, so you actually have two different vectors of sets in play. Comparing iterators of two different containers is undefined. That means it’s possible for some arrangements of code to appear to always work, but you’re still just lucky if it does.

    Some alternatives:

    • Make the function return a vector reference instead:

      std::vector<std::set<int> > const& verlet() const {return _verlet;}
      
    • Call the function once to get a local copy of the vector (or the set) and then work off the local copy during the loop:

      std::set<int> verlet_i = verlet_vars.verlet()[i];
      set<int>::iterator iter;
      for(iter=verlet_i.begin();iter!=verlet_i.end();iter++)
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use BOOST_FOREACH for iterating through the std::queue. But there isn't iterators
I was trying use a set of filter functions to run the appropriate routine,
Trying to use this method (gist of which is use self.method_name in the FunnyHelper
Trying to use this code to connect the AD PrincipalContext context = new PrincipalContext(ContextType.Domain,
I am trying to use the struts iterator to write some data from a
Hey there! I'm doing this project and right now I'm trying to: create some
I'm trying to use STL to solve the following problem (I don't want to
I am just trying to open this file and use the getline function to
I am trying use std::copy to copy from two different iterator. But during course
I am trying to use set.insert (key) as a conditional, where if the key

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.