tags:

views:

374

answers:

5

I have this new class Seq that inherits vector and has some additional features. I can use all methods of vector with Seq. Having this data structure:

Seq< vector<int> > sweepEvents;

I want to have a function that takes an element vector edge search for it in sweepEvents and returns the iterator to the position of the found element in sweepEvents (iff edge is found) and the iterator to the last elements of the vector (iff edge is not found)

Then I want to work with this iterator, in that I want to compare the elements from the prev and the next position of iterator.

I have the following function for founding and returning the iterator:

Seq< vector<int> >::iterator QSweep::insertSweepEvents(edge_t edge,int currentDim){
int changePosition;
int found=0;

for (int i=0;i<currentDim;i++){
     if (edge[0]==sweepEvents[i][1]){
   changePosition=i;
   found=1;
   return sweepEvents.begin()+changePosition;
  }
}
if (found==1){
 sweepEvents.rep().insert(sweepEvents.begin()+changePosition,edge);
 sweepEvents.rep().erase(sweepEvents.begin()+changePosition+1);
} 
else{
 sweepEvents.rep().insert(sweepEvents.end(),edge);
}

return sweepEvents.end()-1;

}

and then I call this iterator in the main function, actually I tried but it does not compile and I do not know what syntax to use other than this

int main(){
    Seq< vector<int> > sweepEvents;
    vector<int> edge;
    //.....initialize sweepEvents and edge

    //declare iterator but not working
     Seq< vector<int> >::iterator comparePosition; 

   //not working neither
    comparePosition=insertSweepEvents(edge,sweepEvents.size());
}

Any idea on how I should correctly call the iterator? I see it does not work as an integer index from an array?

thank you in advance for your suggestions, madalina

A: 

Something illogical in your code. See my comments:

int found = 0;
for (int i=0;i<currentDim;i++){
     if (edge[0]==sweepEvents[i][1]){
                 changePosition=i;
                 found=1; 
// This place is one, where we assign 1 to found, and we do return after that (maybe you want do break/?)
                 return sweepEvents.begin()+changePosition;
         }
}
if (found==1){ // as you see we reach this place only when found == 0
bb
so I have no problem with founding what I want.I supposed the element is not in the list!!.Then I compare the new element with all elements iff I found one the same I say I found it and return the position.Then if it is found I inserted in the right position if not at the end of my vector.
madalina
Maybe it is not main problem, but anyway you can remove this if block and leave only else. Also you can remove found variable.
bb
if I remove found then the element that is found is added to the desired position and then also to the last position, as I tried it.
madalina
no you have return if element is found. and when we have reached if ( found ) - found will have 0 value, and if block will be skipped.
bb
+1  A: 

What compilation error ? How is defined Seq<X>::iterator ?

#include <vector>

template<typename T>
struct Seq
  : public std::vector<T>
{ };

typedef Seq< std::vector<int> > SeqI;

SeqI::iterator insertSweepEvents(SeqI &s)
{
  return s.begin();
}

int main()
{
  SeqI s;
  SeqI::iterator e = insertSweepEvents(s);
}

This works fine.

Benoît
so SeqI is my datatype, so as parametr to my function I need the address of my sweepEvents list also?
madalina
No, but you need to work on a reference so the iterator you return is valid.
Benoît
I am quite parallel with vectors I see.So now that I have e (the iterator to the right position), how can I print the element at position e from s?or just the position?
madalina
You can use std::distance(v.begin(), it) to get the distance between two iterators (<iterator>)
Benoît
A: 

The compilation errors are:

 Sweep.cpp:197: error: 'vector' cannot appear in a constant-expression
 QSweep.cpp:197: error: missing '>' to terminate the template argument list
 QSweep.cpp:197: error: template argument 1 is invalid
 QSweep.cpp:197: error: template argument 2 is invalid
 QSweep.cpp:197: error: expected unqualified-id before '>' token

when I uncomment the declaration of iterator.

madalina
please add information by editing your original question, not by making an answer
anon
please identify line 197 in your code
anon
A common issue with templates is that >> is taken as the operator >> and not the template terminate > followed by another template terminate > - not visible in the code above but perhaps something worth looking at.
Greg Domjan
+2  A: 

Is Seq< vector< T > >::iterator defined in your Seq class?

Making the parameter of the template 'vector' does not imply that exists the type Seq< vector< int > >::iterator

jab
A: 

A little sidenote:

After a vector is changed, especially when appended to or inserted in, iterators to it become invalid. That's due to the fact that the vector tries to allocate a minimally sized contiguous block of memory for it's internal data, while at the same time it tries to minimize the number of times it needs to allocate a new, bigger chunk. So the data may move through memory, so iterators to it before a push are no longer valid after it.

Another little note:

You can find the difference between two iterators by using std::difference( it1, it2 ). You can re-apply that difference by using std::advance( it1, d ).

A third little note:

You seem to have a 'return' statement inside the for-loop, but the rest of the code uses variables that are set only when returning...

xtofl