473,396 Members | 1,917 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,396 software developers and data experts.

a basic question about STL

o.k., I'm totally new to the Standard Template Library and am having
difficulty figuring it out.

I want to insert a user-defined structure into a STL set, and then do
various compare and delete operations, something like this:

#include <set>

struct FU
{
...
};
set<FU> things;

void main ()
{
struct FU rec;
things.insert(rec);
}

The Microsoft Visual Studio 6.0 C++ compiler flags a bunch of errors, but
not in this code, rather, in
c:\program files\microsoft visual studio\vc98\include\functional. What is
the correct way to do this?
Jul 22 '05 #1
7 1289
Robert Rotstein wrote:

The Microsoft Visual Studio 6.0 C++ compiler flags a bunch of errors, but
not in this code, rather, in
c:\program files\microsoft visual studio\vc98\include\functional. What is
the correct way to do this?


What are the first few error messages?

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #2

"Robert Rotstein" <rr*******@verizon.net> wrote in message
news:t1Snd.1664$356.1233@trndny04...
o.k., I'm totally new to the Standard Template Library and am having
difficulty figuring it out.

I want to insert a user-defined structure into a STL set, and then do
various compare and delete operations, something like this:

#include <set>

struct FU
{
...
};
set<FU> things;

void main ()
{
struct FU rec;
things.insert(rec);
}

The Microsoft Visual Studio 6.0 C++ compiler flags a bunch of errors, but
not in this code, rather, in
c:\program files\microsoft visual studio\vc98\include\functional. What is
the correct way to do this?


#ifdef _MSC_VER
#pragma warning (disable : 4786)
#endif

#include <iostream>
#include <ostream>
#include <set>

struct FU
{
int i;

public:
FU(int arg = 0) : i(arg)
{
}

bool operator<(const FU& other) const
{
return i < other.i;
}

int value() const
{
return i;
}

};

std::ostream& operator<<(std::ostream& os, const FU& f)
{
return os << f.value();
}
int main ()
{
std::set<FU> things;

things.insert(FU(3));
things.insert(FU(4));
things.insert(FU(1));
things.insert(FU(5));
things.insert(FU(2));

std::set<FU>::const_iterator it(things.begin());
std::set<FU>::const_iterator en(things.end());

while(it != en)
std::cout << *it++ << '\n';

return 0;
}

-Mike
Jul 22 '05 #3

"Robert Rotstein" <rr*******@verizon.net> skrev i en meddelelse
news:t1Snd.1664$356.1233@trndny04...
o.k., I'm totally new to the Standard Template Library and am having
difficulty figuring it out.

I want to insert a user-defined structure into a STL set, and then do
various compare and delete operations, something like this:

#include <set>

struct FU
{
...
};
set<FU> things;

void main ()
{
struct FU rec;
things.insert(rec);
}

The Microsoft Visual Studio 6.0 C++ compiler flags a bunch of errors, but
not in this code, rather, in
c:\program files\microsoft visual studio\vc98\include\functional. What is
the correct way to do this?

The problem most likely is that you must be able to compare two FUs. It is
this comparison that enables each object to be placed correctly into the
set. Thus you need a

bool operator<(FU const& lhs,FU const& rhs);

Kind regards
Peter
Jul 22 '05 #4
Robert Rotstein wrote:
I want to insert a user-defined structure into a STL set, and then do
various compare and delete operations, something like this:
That's the problem. You want to do comparison of FU objects. And how does
such a comparison look like? The compiler doesn't know, so you have to tell
him by overloading operator== for FU objects.
set<FU> things;
Please anyone correct me if I'm wong here, but my impression is that it
could be a bad idea to store actual objects in the container. I'd rather
store pointers or references, for performance reasons.
Again, tell me if I'm wrong here, but I think if you're storing objects,
each time you get() an object from your container, you're in fact getting a
copy of that object (means, the copy contructor is called). Depending on
how complex your FU class is, this can be expensive.

set<FU*> things; // now you're only storing 4-byte objects and copying is
cheap :)
void main ()
{
struct FU rec;
things.insert(rec);
}


Just a note: "void main()" is illegal, this should be "int main()",
conforming to the standard. If your compiler allows this, dump it and get a
better one.

Furthermore "struct FU rec;" to construct a FU object is C syntax. If you're
programming C++, don't do that. "FU rec;" or "FU rec();" is sufficient.
Keep in mind, that in C++ the semantics of a struct is NOT the same as in C.
In C++, a struct is merely a class which is public by default.
Thus:

struct Bar { int x; };

and

class Bar { public: int x; };

are equivalent.

Hope that helps,
Matthias
Jul 22 '05 #5
Matthias Käppler <no****@digitalraid.com> wrote in message news:<cn*************@news.t-online.com>...
Robert Rotstein wrote:
I want to insert a user-defined structure into a STL set, and then do
various compare and delete operations, something like this:
That's the problem. You want to do comparison of FU objects. And how does
such a comparison look like? The compiler doesn't know, so you have to tell
him by overloading operator== for FU objects.


No.
Technically, you need std::less<FU> although the compiler will happily
use the default std::less<T> if you don't provide one. The default
std::less<T>(T t1, T t2) simply tries return t1<t2, so you should
provide an operator<

The reason is that std::set sorts its elements in increasing order,
and operator== doesn't give you an order.
set<FU> things;


Please anyone correct me if I'm wong here, but my impression is that it
could be a bad idea to store actual objects in the container.


Actually, the STL pretty much assumes you will store values.
I'd rather store pointers or references, for performance reasons.
In fact, you can't even store references, and pointers may cause
memory leaks. You should use a smart pointer, but std::auto_ptr
can't be used either. boost::shared_ptr<FU> does work.
Again, tell me if I'm wrong here, but I think if you're storing objects,
each time you get() an object from your container, you're in fact getting a
copy of that object (means, the copy contructor is called). Depending on
how complex your FU class is, this can be expensive.
There's no get(), but the iterators return references. (24.1.3)
set<FU*> things; // now you're only storing 4-byte objects and copying is
cheap :)


Yes, but who deletes the pointers in things? Do they need to be
deleted? Besides, you now store a FU* and the FU object it points to,
so it may even cause increased memory consumption :|

Regards,
Michiel Salters
Jul 22 '05 #6
Michiel Salters wrote:
Technically, you need std::less<FU> although the compiler will happily
use the default std::less<T> if you don't provide one. The default
std::less<T>(T t1, T t2) simply tries return t1<t2, so you should
provide an operator<
Oh okay, but it boils down to a missing comparison operator, which was the
whole point. I'm not an STL expert, I told you I might be wrong :)
Yes, but who deletes the pointers in things? Do they need to be
deleted? Besides, you now store a FU* and the FU object it points to,
so it may even cause increased memory consumption :|


Well, the increase is minimal.

But I have a question to the copying thing:
Suppose I have a class, which objects are say 800 byte each. You're storing
1000 of them in an std::list. You say working with this list is not slower
than working with 1000 4-byte pointers?

Regards,
Matthias

Jul 22 '05 #7
In message <cn*************@news.t-online.com>, Matthias Käppler
<no****@digitalraid.com> writes
Michiel Salters wrote:
Technically, you need std::less<FU> although the compiler will happily
use the default std::less<T> if you don't provide one. The default
std::less<T>(T t1, T t2) simply tries return t1<t2, so you should
provide an operator<


Oh okay, but it boils down to a missing comparison operator, which was the
whole point. I'm not an STL expert, I told you I might be wrong :)
Yes, but who deletes the pointers in things? Do they need to be
deleted? Besides, you now store a FU* and the FU object it points to,
so it may even cause increased memory consumption :|


Well, the increase is minimal.

But I have a question to the copying thing:
Suppose I have a class, which objects are say 800 byte each. You're storing
1000 of them in an std::list. You say working with this list is not slower
than working with 1000 4-byte pointers?


Depends what you do with the list. Constructing the list requires (in
principle - a good compiler may optimize them away) 1000 copy
operations, but thereafter, if you use the STL iterator paradigm
properly, you'll be using references to the objects, and you may not
need to make any further copies. Unless populating the list makes your
program *significantly* slower - and the only way to determine that is
to profile it - the gain in simplicity may well outweigh the slight
increase in time.

If you do find it necessary to use pointers, you need to determine
_before starting to code_ whether pointers represent ownership, and if
so, decide on an ownership model and use smart pointers to enforce it.
--
Richard Herring
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Leif K-Brooks | last post by:
Has anyone ever tried implementing a simple unstructured BASIC dialect in Python? I'm getting interested in language implementation, and looking at a reasonably simple example like that could be...
7
by: Michael Foord | last post by:
#!/usr/bin/python -u # 15-09-04 # v1.0.0 # auth_example.py # A simple script manually demonstrating basic authentication. # Copyright Michael Foord # Free to use, modify and relicense. #...
54
by: seberino | last post by:
Many people I know ask why Python does slicing the way it does..... Can anyone /please/ give me a good defense/justification??? I'm referring to why mystring gives me elements 0, 1, 2 and 3...
13
by: Pete | last post by:
I'm cross posting from mscom.webservices.general as I have received no answer there: There has been a number of recent posts requesting how to satisfactorily enable BASIC authorization at the...
13
by: usenet | last post by:
How and where can one find out about the basics of VB/Access2003 syntax? I am a died in the wool C/C++/Java Linux/Unix programmer and I am finding it difficult to understand the program format...
97
by: Master Programmer | last post by:
An friend insider told me that VB is to be killled off within 18 months. I guess this makes sence now that C# is here. I believe it and am actualy surprised they ever even included it in VS 2003 in...
6
by: Simon Walsh | last post by:
I'm an Electronics student in college and I'm currently working on a project. I was given a circuit diagram for my project, from which I had to design a printed circuit board to be sent off and...
28
by: Randy Reimers | last post by:
(Hope I'm posting this correctly, otherwise - sorry!, don't know what else to do) I wrote a set of programs "many" years ago, running in a type of basic, called "Thoroughbred Basic", a type of...
43
by: Bill H | last post by:
25 years ago every computer came with some form of Basic interpreter so you could use yoru computer without having to buy more software. Is Javascript (teamed with HTML) set to become the new...
1
by: steve | last post by:
A series of articles examining some basic concepts in Sql Server. Basic Anatomy of Sql Server, part I What is a stored procedure?...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.