473,394 Members | 2,031 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

iterating through a set<string> when passed by reference

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
9 2672
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
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
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
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
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
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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: aquanutz | last post by:
Ok, I have a list of strings (list<string> stringList) that I want to sort alphabetcially, only "sort(stringList.begin(), stringList.end()); ) does not work. Any insight would be helpful. Thanks!
8
by: Steve Edwards | last post by:
Hi, 1) I'm making a container for string pointers... set<string*> strSet; strSet.insert(...); // insert some strings ....
5
by: KL | last post by:
I have a problem trying to write to a binary file that I want to name after a particular name from a set. My code for the function follows, please excuse the horrible mistakes you may see, I am a...
6
by: buzzweetman | last post by:
Many times I have a Dictionary<string, SomeTypeand need to get the list of keys out of it as a List<string>, to pass to a another method that expects a List<string>. I often do the following: ...
5
by: Martin Jørgensen | last post by:
Hi, The piece of code I'm struggling with is so simple, that I hope nobody wants a complete example for answering the question: -------- string color_line; int data_type = 0; for(...
12
by: Mark S. | last post by:
Hello, The app in question is lives on a Windows 2003 server with .NET 2.0 running IIS 6. The page of the app in question processes 2000 get requests a second during peak loads. The app uses...
4
by: parez | last post by:
Hi, I am trying to serialize List<List<string>. With the following code public List<List<string>DataRows { get; set; }
42
by: barcaroller | last post by:
In the boost::program_options tutorial, the author included the following code: cout << "Input files are: " << vm.as< vector<string() << "\n"; Basically, he is trying to print a vector...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.