to swap elements at iterator positions

hi,
i have 2 iterators and i wan tot do a bubble sort.
i am getting a segmentation faut here because i think that the swapping has some error.

structure is avector of type X and temp,temp2 are pointers of type X.can some1 hint me as to how can i swap the element?



for (it=structure.begin(); it!=structure.end(); it++){
for (it2=structure.begin(); it2!= (structure.end()); it2++){
if(myfunction((*it),(*it2))){
temp=it;
temp2=it2;

structure.insert(it,*temp2);

structure.insert(it2,*temp);

}

}
}





You can try with the STL iter_swap algorithm
http://www.cplusplus.com/reference/algorithm/iter_swap.html
yea
just saw the page b4 ur post..........

thanx a lot...
and sorry for the post
Using the STL to swap the elements kind of defaults the purpose of implementing the bubble sort, doesn't it? If you can use iter_swap or swap, consider using sort, too!
i used the iter_swap function...
however its not swapping the elements....

here is the code :
one.printAll();
vector<CellPos>::iterator it;
vector<CellPos>::iterator it2;
it=one.structure.begin();
it2=one.structure.end();
iter_swap(it,it2);
one.printAll();



the output is :

3

32

27

<-- only three elems-->

3

32

27


i tried using sort too...... i implemented the custom function too..........but there were some probs ....... so i fugured i do it myself


i have also tried using make_heap,sort_heap and push_heap with the vector i am using ..
however i am getting the same output
Last edited on
Topic archived. No new replies allowed.