C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

3rd set.insert overload question

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
John Calcote
Guest





PostPosted: Thu Oct 30, 2003 7:14 pm    Post subject: 3rd set.insert overload question Reply with quote



Okay everyone, I've got two questions here, one very specific and one
much more general:

1. specific: what is the proper way to use the

void insert(InputIterator first, InputIterator last);

method of the set class? This is what I tried:

using namespace std;

set<string> set1;
set<string> set2;

set2.insert(set1.begin(), set2.end());

But this give me an error indicating the compiler (msvc6) was trying
to coerce my function call into a call to the second insert function,
eg.:

iterator insert(iterator where, const value_type & val);

Now I can see the clue that the MSDN docs are trying to give me by
naming true set<string> iterators "iterator" and the other thing I'm
trying to use "InputIterator", and the <set> header file tells me the
types are logically different, but I guess I'm a loser, because I
can't seem to discover the true nature of "InputIterator" in this
context. No doubt it's something related to std::inserter... (see the
next question, please).

Any help would be appreciated.

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

2. general: Why, oh why does every single book on the STL presuppose
that you already understand the STL? Here's why I think this is the
case. I'm reading through Josuttis's book and I find an example of the
use of the set_intersection algorithm that looks like this:

set<int> c1, c2;
set_intersection(c1, c1+num1,
c2, c2+num2, // fine up to this point...
ostream_iterator<int>(cout, " "));

Now, why in the world would anyone want to know how to get this
algorithm to output to cout?! Everyone can guess what anyone will
really try to do with this algorithm - they'll want to create a third
set that contains the intersection of the first two sets. So
obviously, we would do this, right?

set<int> c1, c2, c3;
set_intersection(c1, c1+num1,
c2, c2+num2,
c3.begin());

Of course this doesn't work, but I had to come to this newsgroup and
find a thread that already contained the information I wanted. In each
of these threads, I find snide remarks like, "Why don't you use an
inserter? That's what their for, after all...". Well, yes, this is
true - that is what they are for, but we don't all know everything,
and in this case, we can't even look it up because it's not
cross-referenced correctly (in any book I own - and I own many of
them). The fact is, I had to discover the true technique first, and
then go the index in my book (Josuttis again) and discover by reading
up on inserters that, in fact, this is what they are for.

Can't someone write a book that explains the STL in terms of what
people are really going to do with it, instead of using examples that
show how clever the book's author is? I write system-level software,
and I NEVER write to cout.

John

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Matthew Collett
Guest





PostPosted: Fri Oct 31, 2003 12:18 pm    Post subject: Re: 3rd set.insert overload question Reply with quote



In article <abb28ce2.0310292227.a204e20 (AT) posting (DOT) google.com>,
[email]jcalcote (AT) novell (DOT) com[/email] (John Calcote) wrote:

Quote:
1. specific: what is the proper way to use the

void insert(InputIterator first, InputIterator last);

method of the set class?

This is what I tried:

using namespace std;

set<string> set1;
set<string> set2;

set2.insert(set1.begin(), set2.end());

But this give me an error

Then you are lucky: it could easily have compiled happily and then
crashed horribly, since specifying an iterator range using two unrelated
iterators gives undefined behaviour.

What are you actually trying to do here? If the aim is to copy set1
into set2 then

set<string> set1;
//Populate set1
set1.insert("A string");
set1.insert("Another string");

set<string> set2;
set2.insert(set1.begin(), set1.end());

will result in set2 containing the same elements as set1.

Of course, in this particular case, just saying

set2 = set1;

does the same job more simply, but the range insertion is more general.

Quote:
I
can't seem to discover the true nature of "InputIterator" in this
context.

This is a template function, so InputIterator may be any type that acts
as an iterator; but first and last must be of the same type (and last
must be reachable by incrementing first).

Quote:
No doubt it's something related to std::inserter...

Not needed, since you are explicitly 'insert'ing here.

Quote:
2. general: Why, oh why does every single book on the STL presuppose
that you already understand the STL?

I presume that besides Josuttis you have read the standary library
section of C++PL and Meyers' "Effective STL"? Sutter's "Exceptional
C++" and "More Exceptional C++" also start with some discussion of STL
problems, though one would hardly call them books "on the STL".

Quote:
So
obviously, we would do this, right?

set<int> c1, c2, c3;
set_intersection(c1, c1+num1,
c2, c2+num2,
c3.begin());

Of course this doesn't work [...] but we don't all know everything,
and in this case, we can't even look it up because it's not
cross-referenced correctly (in any book I own - and I own many of
them).

Look up 'set_intersection' in the index of Josuttis. That takes you
directly to the relevant section on p.419. One of the bullet points
there says "The caller must ensure that the destination range is big
enough or that insert iterators are used." If that's not clear because
you don't know what an insert iterator is, look up 'insert iterator' in
the index, which directs you to p.271, where you will find an extensive
discussion of the topic. What more do you want?

Best wishes,
Matthew Collett

--
Those who assert that the mathematical sciences have nothing to say
about the good or the beautiful are mistaken. -- Aristotle


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ben Hutchings
Guest





PostPosted: Fri Oct 31, 2003 5:21 pm    Post subject: Re: 3rd set.insert overload question Reply with quote



John Calcote wrote:
Quote:
Okay everyone, I've got two questions here, one very specific and one
much more general:

1. specific: what is the proper way to use the

void insert(InputIterator first, InputIterator last);

method of the set class?

This is actually supposed to be a template member function rather
than a single member function.

Quote:
This is what I tried:

using namespace std;

set<string> set1;
set<string> set2;

set2.insert(set1.begin(), set2.end());

That is correct, but...

Quote:
But this give me an error indicating the compiler (msvc6) was trying
to coerce my function call into a call to the second insert function,
eg.:

iterator insert(iterator where, const value_type & val);

....VC++ 6 doesn't have a sufficiently complete implementation of
template functions to support a correct implementation of insert().

Quote:
Now I can see the clue that the MSDN docs are trying to give me by
naming true set<string> iterators "iterator" and the other thing I'm
trying to use "InputIterator", and the <set> header file tells me the
types are logically different, but I guess I'm a loser, because I
can't seem to discover the true nature of "InputIterator" in this
context. No doubt it's something related to std::inserter... (see the
next question, please).

Any help would be appreciated.

InputIterator is a template type parameter which should normally be
deduced from the arguments to insert().

Quote:
2. general: Why, oh why does every single book on the STL presuppose
that you already understand the STL? Here's why I think this is the
case. I'm reading through Josuttis's book and I find an example of the
use of the set_intersection algorithm that looks like this:

set<int> c1, c2;
set_intersection(c1, c1+num1,
c2, c2+num2, // fine up to this point...
ostream_iterator<int>(cout, " "));

Now, why in the world would anyone want to know how to get this
algorithm to output to cout?!

So that they can try it out and see the results immediately?

<snip>
Quote:
Can't someone write a book that explains the STL in terms of what
people are really going to do with it, instead of using examples that
show how clever the book's author is? I write system-level software,
and I NEVER write to cout.

I think there probably are such books but since C++ is no longer cool
other languages get more shelf space at the bookshop. Try searching
for "STL" at <http://www.accu.org/htdig/search_bk.htm>.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.