iterator through user define type vector

Jun 27, 2011 at 6:16pm

I am having a hard time trying to iterator through my own object. I placed my object in vector and able to feed the input into it:

See below:

My object is Env(string server, string version);

I have no problem doing the input via fstream and populate my vector using:
vector<Env> envir;
string server_name;
string serv_version;

while (ifs>>server_name>>server_version)
envir.push_back(Env(server, version));

I just need to know the way to iterator through the envir.server value.

I did the following, but it is not working:

for (vector<Env>::iterator it = envir.begin(); it!=envir.end()
if (*it== {my value of my server} ....then do something.

Please help... I also created the Env.getServer(), but just don't know how to iterator through my Env member.

Thanks,

Noah



Jun 27, 2011 at 6:29pm
You forgot to increment your iterator.

It should be -> for (vector<Env>::iterator it = envir.begin(); it!=envir.end(); ++it)
Jun 27, 2011 at 6:58pm
Sorry, I actually did. Sorry, that was not a full code... my bad.

my problem is the last line where I just don't know how to get the value out of my Env.server to do some comparison...etc.

Thanks for quick reply..
Jun 27, 2011 at 7:04pm
Oh, you want to access a member through the iterator.

There are two ways to do this:

(1) (*it).getServer()

(2) it->getServer()

Choose whichever one you like.

EDIT: Or, is it something else?
Last edited on Jun 27, 2011 at 7:18pm
Jun 27, 2011 at 7:19pm
Thanks m4sterr0shi. I just tried the following:


for (vector<Env>::iterator it = environment.begin(); it!=environment.end(); ++it)
if (*it.getServer()=="EBSTEST.gpi.com")
{cout<< "found it"<<endl;}

And I still get the compile error as:

error: 'class std::vector<Env>::iterator' has no member named 'getServer'


I do have the getServer defined as member of my Env class.

I am just struck...
Jun 27, 2011 at 7:21pm
ok..sorry. I got it working with pointer member of it->getServer().

Thanks so much....
Jun 27, 2011 at 7:25pm
You are welcome. Note that your first attempt didn't work because you forgot the parentheses.
Topic archived. No new replies allowed.