C/C++ How is the vector<hit>::iterator itr; line used in this code?

  • Thread starter Thread starter WarPhalange
  • Start date Start date
  • Tags Tags
    C++ Vectors
AI Thread Summary
The discussion revolves around a code snippet involving a loop that processes a vector of "hits" five times using a proton propagator. The main point of contention is the use of the iterator "itr," which is declared but never utilized in the code. Participants clarify that the loop iterates five times, but the iterator does not contribute to this process. Instead, the code directly uses an ostream_iterator to write the contents of the "hits" vector to a file, making the iterator "itr" unnecessary. The confusion stems from the assumption that "hits" might represent a two-dimensional array, but it is clarified that "hits" is a one-dimensional vector that is cleared and reused in each iteration. The ostream_iterator is discussed as a tool that facilitates copying the vector's contents to the output file, with a link provided for further reading on its functionality. Overall, the discussion highlights the importance of understanding code structure and the role of iterators in C++.
WarPhalange
I came across a snippet of code like this:

Code:
  protonPropagator pp;
  vector<hit> hits;

  vector<hit>::iterator itr;
  for(int i=0;i<5;i++){
    hits.clear();
    //x,y,z,E,theta,phi
    hits = pp.compute(startX,startY,startZ,startE,theta,phi);

    if(hits.size()>0){
    fout<<"startEvent"<<endl;
    fout<<i<<" "<<hits.size()<<" "<<startX<<" "<<startY<<" "<<startZ<<" "<<startE<<" "<<theta<<" "<<phi<<endl;
    copy(hits.begin(),hits.end(),ostream_iterator<hit>(fout,"\n"));

    }else{
      cerr<<"No hits in this event, skipping"<<endl;
    }
  }

And I was told it will iterate through the vector "hits" 5 times. But by the looks of it, nothing is really being iterated. You have an initialization of "itr", but then it's never used.

So it looks like you get some value for hits and overwrite it on each successful iteration of the for() loop. Since it's printed out, you store it in a file so it doesn't matter if it's written over.

Am I correct here? That the line "vector<hit>::iterator itr;" is useless? Or is it used to print to file, i.e. "copy(hits.begin(),hits.end(),ostream_iterator<hit>(fout,"\n"));"?
 
Technology news on Phys.org
The iterator itr doesn't seem to be necessary, ostream_iterator<hit> will create a hidden one on the fly.
 
You're right, the iterator itr is irrelevant to this section of code. Is it used anywhere else?

It certainly wouldn't be the first time that comments or documentation don't match the actual code. :rolleyes:

There is a for-loop that iterates (cycles) five times. Maybe that's what they're referring to with the verb "iterates", not the iterator itr.
 
Okay, I think I see what is going on now. The vector "hits" has some number of values in it like an array. I had thought that each value of hits carried its own array, i.e. making it a 2d array where each value of hits had its own array associated with it.

That's why it's being cleared and reused every time.

I still can't figure out what this line does, though:

copy(hits.begin(),hits.end(),ostream_iterator<hit>(fout,"\n"));

I know it copies the entire vector hits into the file, but how does ostream_iterator work?
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top