Teen Programmers Unite  
 

 

Return to forum top

creating a subset

Posted by bgbg [send private reply] at January 12, 2003, 12:13:11 PM

I have a system of points in 2D space. Each point have an attribute (represented
as int) and coordinates. The data is organized in a class with two vectors as data
members: one for the attributes, and another for coordinates (which is twice long).
The class has public methods that return references to the vectors. There is a template
function that recives begin(), and end() of a vector and modifies it's data.
This function is used to modify the coordinates of the points.
My goal is to iterate over some subset of the given object using the standart iterator idioms:
(namely begin(), end, operator++), so I can apply the same template function on the subset.
Any idea?

Here what I have done:


#include <vector>
#include <iostream>
using namespace std;
class Molecule{
vector<int> attr;
vector<float> coords;
public:
class iterator{
public:
vector<int>::iterator ati;
vector<float>::iterator coi;
public:
iterator(){}
iterator(vector<int>::iterator i, vector<float>::iterator f):ati(i), coi(f){}
const iterator& operator ++(){
++ati;++coi;++coi;
return *this;
}
bool operator !=(iterator i){return !(ati==i.ati || coi==i.coi);}
iterator& operator =(const iterator& i){ ati=i.ati; coi=i.coi;return *this;}
operator float* (){return coi;}
iterator operator +(int n){iterator i(*this); i.ati+=n; i.coi+=(2*n); return i;}
};
public:
Molecule(){}
Molecule(vector<int> a, vector<float> c):attr(a),coords(c){}
vector<int>& Attr(){return attr;}
vector<float>& Coords() {return coords;}
iterator begin(){return iterator(attr.begin(), coords.begin());}
iterator end() {return iterator(attr.end(), coords.end());}
};



template <class ForwardIterator>
void apply(ForwardIterator start, ForwardIterator end){
int count=0;
for(ForwardIterator i=start; i!=end && count<6; ++i, ++count){
*i=(*i) * (-1); *(i+1)=*(i+1) * 2;
cout<<count<<endl;
}
}
int main(int argc, char *argv[]){
vector<int> av; vector<float> cov;
for(int i=0;i<5;++i){
av.push_back(i);
cov.push_back((float)i+0.1);
cov.push_back((float)i+0.2);
}
Molecule m(av, cov);
cout<<"before:\n"<<endl;
for(int i=0;i<5;++i){
cout<<m.Attr()[i]<<' '<<m.Coords()[2*i]<<' '<<m.Coords()[2*i+1]<<endl;
}

apply(m.Coords().begin(), m.Coords().end());

cout<<"after:\n";
for(int i=0;i<5;++i){
cout<<m.Attr()[i]<<' '<<m.Coords()[2*i]<<' '<<m.Coords()[2*i+1]<<endl;
}
vector<Molecule::iterator> im;
for(Molecule::iterator i=m.begin();i!=m.end(); ++i){
if((*i.ati%2)==0){
im.push_back(i);
}
}
cout<<"subset:\n";
for(int i=0;i<im.size();++i){
cout<<*im[i].ati<<' '<<*im[i].coi<<' '<<*(im[i].coi+1)<<endl;
}
apply(im.begin(), im.end());
cout<<"subset after:\n";
for(int i=0;i<im.size();++i){
cout<<*im[i].ati<<' '<<*im[i].coi<<' '<<*(im[i].coi+1)<<endl;
}
cout<<"m after:\n";
for(int i=0;i<5;++i){
cout<<m.Attr()[i]<<' '<<m.Coords()[2*i]<<' '<<m.Coords()[2*i+1]<<endl;
}


cout<<"\nend"<<endl;



}


===================================================================
===================================================================
===================================================================
COMPILER OUTPUT (reduced)
===================================================================
===================================================================
===================================================================

main.cpp: In function `void apply (ForwardIterator, ForwardIterator)
[with ForwardIterator = Molecule::iterator *]':
main.cpp:101: instantiated from here
main.cpp:68: no match for `Molecule::iterator & * int'
main.cpp:68: no match for `Molecule::iterator & * int'
gmake: *** [main.o] Error 1
*** failed ***

Posted by CViper [send private reply] at January 12, 2003, 01:06:42 PM

I'd suggest you but the attribute and the coords in a struct / class; something like

struct MyStruct
{
  int attrib;
  float x, y;
};

That way you only have to store a single iterator - and it will make you code less messy (alot).

BTW: this was waaay too much code. I think someone said max 20 (?) lines...
[edit] Actually it was 5 lines :D[/edit]
Posted by gian [send private reply] at January 13, 2003, 03:23:08 AM

It's gratifying to see that you read the instruction about not posting huge amounts of code, bgbg!

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.