iterating through a set<string> when passed by reference

Z

Zootal

The following code will compile. I have one line commented out because it
won't compile. Why can I not do this:

//set<string>::iterator iterator = s->begin();

Why do I have to deference the set when I receive it as a pointer? Is that
just the way we are supposed to do it, or am I missing something?

-------------------------------------------------

#include <iostream>
#include <set>
#include <string>
using namespace std;

typedef set<string> StringSet;

void zoot(set<string> * s);

int main()
{
set<string> s;
s.insert("aaa");
s.insert("bbb");
zoot( &s );
return 0;
}

void zoot(set<string> * s )
{
// This won't compile - why?
//set<string>::iterator iterator = s->begin();

// dereference s, use s1, and it compiles
set<string> s1 = *s;
for( set<string>::iterator iterator1 = s1.begin();iterator1 !=
s1.end();iterator1++)
{
cout << *iterator1;
}
}
 
I

Ismo Salonen

Zootal said:
The following code will compile. I have one line commented out because it
won't compile. Why can I not do this:

//set<string>::iterator iterator = s->begin();

Why do I have to deference the set when I receive it as a pointer? Is that
just the way we are supposed to do it, or am I missing something?

-------------------------------------------------

#include <iostream>
#include <set>
#include <string>
using namespace std;

typedef set<string> StringSet;

void zoot(set<string> * s);

int main()
{
set<string> s;
s.insert("aaa");
s.insert("bbb");
zoot( &s );
return 0;
}

void zoot(set<string> * s )
{
// This won't compile - why?
//set<string>::iterator iterator = s->begin();

// dereference s, use s1, and it compiles
set<string> s1 = *s;
for( set<string>::iterator iterator1 = s1.begin();iterator1 !=
s1.end();iterator1++)
{
cout << *iterator1;
}
}

What compiler and what error message ?

You could try (*s).begin() , MSVC6 had problems with s-> syntax
sometimes, it did not correctly implement templates for obvious reasons.

ismo
 
A

advice.of.deal

I use MSVC6 to compile your code. It's OK. There is not any complile
errors. (before compile I and a '}' and then restore the line that you
comment out )
 
C

Cari Elf

Have you tried passing the set by reference?

void zoot(set<string> & s )
{
set<string>::iterator it;
for (it = s.begin(); it != s.end(); ++it)
{
cout << *it;
}
}
 
G

gethostbyname

The following code will compile. I have one line commented out because it
won't compile. Why can I not do this:

//set<string>::iterator iterator = s->begin();

Why do I have to deference the set when I receive it as a pointer? Is that
just the way we are supposed to do it, or am I missing something?

-------------------------------------------------

#include <iostream>
#include <set>
#include <string>
using namespace std;

typedef set<string> StringSet;

void zoot(set<string> * s);

int main()
{
set<string> s;
s.insert("aaa");
s.insert("bbb");
zoot( &s );
return 0;

}

void zoot(set<string> * s )
{
// This won't compile - why?
//set<string>::iterator iterator = s->begin();

// dereference s, use s1, and it compiles
set<string> s1 = *s;
for( set<string>::iterator iterator1 = s1.begin();iterator1 !=
s1.end();iterator1++)
{
cout << *iterator1;
}

}

It compiles here, VS 2005.

gethostbyname
 
O

Ook

It compiles here, VS 2005.

I'm using VS2003. I tried it on my desktop, and it compiles fine. My
laptop has VS2003 academic version, and it gives an error - I don't
have it with me now so I can't post the actual error, but I can do
that tonight. WTF? Is anyone aware of any differences between the
academic version and the retail version?
 
O

Ook

You could try (*s).begin() , MSVC6 had problems with s-> syntax
sometimes, it did not correctly implement templates for obvious reasons.

ismo

Interesting enough, that fixed another problem I had. I was doing
this:

Item* bb = *iterator;

And using bb because I couldn't figure out how to use the item the
iterator was pointing to, until I figured out I can use (*iterator)
instead. Thanks for the info :)
 
V

Victor Bazarov

Ook said:
I'm using VS2003. I tried it on my desktop, and it compiles fine. My
laptop has VS2003 academic version, and it gives an error - I don't
have it with me now so I can't post the actual error, but I can do
that tonight. WTF? Is anyone aware of any differences between the
academic version and the retail version?

People in 'microsoft.public.vc.language' probably are.

V
 
Z

Zootal

Zootal said:
The following code will compile. I have one line commented out because it
won't compile. Why can I not do this:

//set<string>::iterator iterator = s->begin();
<snip>

So I get home tonight, load the project, and it compiles. I spent an hour
banging my head against the wall, wondering why it would not compile. I'm
loosing it. Microsoft products hate me because I run linux on my server,
that must be it...
 
G

gethostbyname

<snip>

So I get home tonight, load the project, and it compiles. I spent an hour
banging my head against the wall, wondering why it would not compile. I'm
loosing it. Microsoft products hate me because I run linux on my server,
that must be it...

IMHO, if you would like to work with VS, use the 2005 Edition. Or use
gcc on Win :)

gethostbyname
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top