By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,159 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

iterating through a set<string> when passed by reference

P: n/a
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<stringStringSet;

void zoot(set<string* s);

int main()
{
set<strings;
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<strings1 = *s;
for( set<string>::iterator iterator1 = s1.begin();iterator1 !=
s1.end();iterator1++)
{
cout << *iterator1;
}
}
Mar 13 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Zootal wrote:
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<stringStringSet;

void zoot(set<string* s);

int main()
{
set<strings;
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<strings1 = *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
Mar 13 '07 #2

P: n/a
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 )


On 3月13日, 下午12时05分, "Zootal" <nousenets...@zootal.nospam.comwrote:
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<stringStringSet;

void zoot(set<string* s);

int main()
{
set<strings;
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<strings1 = *s;
for( set<string>::iterator iterator1 = s1.begin();iterator1 !=
s1.end();iterator1++)
{
cout << *iterator1;
}

}- 隐藏被引用文字 -

- 显示引用的文字 -

Mar 13 '07 #3

P: n/a
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;
}
}

Mar 13 '07 #4

P: n/a
On 13 mar, 01:05, "Zootal" <nousenets...@zootal.nospam.comwrote:
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<stringStringSet;

void zoot(set<string* s);

int main()
{
set<strings;
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<strings1 = *s;
for( set<string>::iterator iterator1 = s1.begin();iterator1 !=
s1.end();iterator1++)
{
cout << *iterator1;
}

}
It compiles here, VS 2005.

gethostbyname

Mar 13 '07 #5

P: n/a
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?

Mar 13 '07 #6

P: n/a
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 :)

Mar 13 '07 #7

P: n/a
Ook wrote:
>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?
People in 'microsoft.public.vc.language' probably are.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Mar 13 '07 #8

P: n/a

"Zootal" <no**********@zootal.nospam.comwrote in message
news:AZ******************************@giganews.com ...
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...
Mar 14 '07 #9

P: n/a
On 13 mar, 22:11, "Zootal" <nousenets...@zootal.nospam.comwrote:
"Zootal" <nousenets...@zootal.nospam.comwrote in message

news:AZ******************************@giganews.com ...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...
IMHO, if you would like to work with VS, use the 2005 Edition. Or use
gcc on Win :-)

gethostbyname

Mar 14 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.