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

  • Context: C/C++ 
  • Thread starter Thread starter WarPhalange
  • Start date Start date
  • Tags Tags
    C++ Vectors
Click For Summary

Discussion Overview

The discussion centers around the use of the line "vector::iterator itr;" in a code snippet involving a vector of "hit" objects. Participants explore whether the iterator is necessary in the context of the provided code, which includes a loop that processes and outputs data from the vector.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant questions the utility of the iterator "itr," suggesting it is never used in the code and may be unnecessary.
  • Another participant agrees that "itr" appears irrelevant, noting that an ostream_iterator is created on the fly for output.
  • A different participant speculates that the term "iterates" might refer to the for-loop cycling through iterations rather than the iterator itself.
  • One participant expresses confusion about the structure of the "hits" vector, initially thinking it might be a 2D array, and seeks clarification on the function of the ostream_iterator in the copy operation.

Areas of Agreement / Disagreement

Participants generally agree that the iterator "itr" is not necessary for the code provided, but there is some uncertainty regarding its potential use elsewhere in the code. The discussion about the structure of "hits" and the functionality of ostream_iterator remains unresolved.

Contextual Notes

There is a lack of clarity regarding the overall structure of the "hit" objects and how they are intended to be used, as well as the specific workings of ostream_iterator, which some participants seek to understand better.

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?
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
2K
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
2
Views
5K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
Replies
1
Views
2K
  • · Replies 49 ·
2
Replies
49
Views
12K
  • · Replies 2 ·
Replies
2
Views
6K