map access outside class

Hi All,
I'm new to C++. I need some help regarding the usage of map STL container. I've declare map proteinData in the below header file. ProteinData contains key as integer and value as a struct.
#ifndef _READFILE_H_
#define _READFILE_H_
#include<string>
#include<vector>
#include<map>

using namespace std;

class Readfile{

private:
string str;
vector <string> goArray;
vector <string> strArray;
vector <string> phyIntArray;
vector <string> subArray;
vector <string> proArray;
/* member function */
void processString(string str);
void processGoId(string);
void processPhysicalInt(string);
// void processMetabolicInt(string);

public:
typedef struct Info{
string uniprotID;
vector <string> goId;
vector <string> phyInt;
};

map <int,struct Info> proteinData;
map <int,struct Info>::iterator it;

Readfile(string file);//constructor

};
#endif

Can any one help me how to access map outside the class defination.


Thanks in advance
Readfile f( "foo.txt" );

f.proteinData // whatever here;

really not a good idea to hold an iterator as a class data member.
Thank jsmith for your reply...

Readfile *myfile = new Readfile("foo.txt");
cout << myfile->proteinData << endl;

This is showing an error:
error: no match for ‘operator<<’ in ‘std::cout << myfile->Readfile::proteinData’

As per your suggestion how i should represent an iterator, so that i can access the data inside map outside its class defination....

Thanks once again.
i'm able to get the size of the map container proteinData, shown in the below code.

Readfile *myfile=new Readfile('foo.txt');
cout << "this is iterator:" << endl;
cout << myfile->proteinData.size()<< endl;

for(myfile->it=myfile->proteinData.begin(); myfile->it=myfile->proteinData.end();myfile->it++){
cout << myfile->it->first << endl;
}

but couldn't access the iterator defined inside the class, giving an error message

could not convert ‘(myfile->Readfile::it <unknown operator> ((const std::_Rb_tree_iterator<std::pair<const int, Readfile::Info> >&)((const std::_Rb_tree_iterator<std::pair<const int, Readfile::Info> >*)(& myfile->Readfile::proteinData.std::map<_Key, _Tp, _Compare, _Alloc>::end [with _Key = int, _Tp = Readfile::Info, _Compare = std::less<int>, _Alloc = std::allocator<std::pair<const int, Readfile::Info> >]()))))’ to ‘bool’.

I think there is something wrong with defining the iterator inside class as suggested by jsmith.

Can anyone help with this.......


There are many reasons not to store an iterator in your class.
1. Some class operation may invalidate the iterator.
2. Using a local iterator contrains the object to be used by one client, which is unncessary and probably wrong.

You can traverse the map as:
1
2
for (map<int, Readfile::Info>::const_iterator p = myfile->proteinData.begin(); p != myfile->proteinData.end(); ++p)
    std::cout << p->first << std::endl;
Thanks kwb, it is very helpful .....
Topic archived. No new replies allowed.