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

c++ equivalent of java collections

P: n/a
hi-

where would someone find things like lists, sets, map classes like a
hash map or tree map. not specifically those but in general is there
a book section that deals with it? in java you can use the interface
to these without having to know all of the implementation details.
is there an interface to a library with these kind of methods in c++?

i knew a little c++ from 2 really basic introductory courses.
however, each time we wanted this functionality we had to program
it. an example where this would be cool is if i wanted a data structure
that TCL didn't have. i could extend TCL with C++.

thanks for letting me know what people use.
jim
Jun 19 '07 #1
Share this Question
Share on Google+
16 Replies


P: n/a

i just found a listing point to the STL.
thanks,
later
Jun 19 '07 #2

P: n/a
3rdshiftcoder wrote:
hi-

where would someone find things like lists, sets, map classes like a
hash map or tree map. not specifically those but in general is there
a book section that deals with it?
This is referred to as the "standard template library" (STL) and is a
normative part of the C++ standard. Not only does the STL encompass
containers like you have mentioned but also algorithms like sort or find.

There are many books that discuss these.

The "reference" I used often but it is a little dated is the SGI web
site on the STL (I have not looked at them for a while myself - maybe it
is updated).
http://www.sgi.com/tech/stl/

SGI has implemented some containers that are not part of the standard so
you need to be a little careful.

The ones you will use almost all the time in modern c++ code are:

basic_string
vector
list
map
sort
The ones I know are not part of the current standard are:

hash_map
rope

.... probably others.

The SGI examples are missing a few namespace specifiers. Usually a
using namespace std; somewhere in the example fixes those probs.
Jun 19 '07 #3

P: n/a
Hi Gianni -

that is a very cool link :-)
i think this is worth checking out !
the first 2 semesters of coding at school were very basic.
this is definitely some of the stuff that is missing.

thanks very much,
jim


"Gianni Mariani" <gi*******@mariani.wswrote in message
news:46***********************@per-qv1-newsreader-01.iinet.net.au...
3rdshiftcoder wrote:
>hi-

where would someone find things like lists, sets, map classes like a
hash map or tree map. not specifically those but in general is there
a book section that deals with it?

This is referred to as the "standard template library" (STL) and is a
normative part of the C++ standard. Not only does the STL encompass
containers like you have mentioned but also algorithms like sort or find.

There are many books that discuss these.

The "reference" I used often but it is a little dated is the SGI web site
on the STL (I have not looked at them for a while myself - maybe it is
updated).
http://www.sgi.com/tech/stl/

SGI has implemented some containers that are not part of the standard so
you need to be a little careful.

The ones you will use almost all the time in modern c++ code are:

basic_string
vector
list
map
sort
The ones I know are not part of the current standard are:

hash_map
rope

... probably others.

The SGI examples are missing a few namespace specifiers. Usually a using
namespace std; somewhere in the example fixes those probs.

Jun 19 '07 #4

P: n/a
On Tue, 19 Jun 2007 00:36:32 -0400, "3rdshiftcoder" wrote:
>where would someone find things like lists, sets, map classes like a
hash map or tree map. not specifically those but in general is there
a book section that deals with it? in java you can use the interface
to these without having to know all of the implementation details.
is there an interface to a library with these kind of methods in c++?
Actually, there is no equivalent for Java collections in Standard C++.
Java collections work only with objects but not with values, Standard
C++ collections (a.k.a. STL) work only with values but not with
objects.
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Jun 19 '07 #5

P: n/a
On 2007-06-19 07:27, 3rdshiftcoder wrote:
"Gianni Mariani" <gi*******@mariani.wswrote in message
news:46***********************@per-qv1-newsreader-01.iinet.net.au...
>3rdshiftcoder wrote:
>>hi-

where would someone find things like lists, sets, map classes like a
hash map or tree map. not specifically those but in general is there
a book section that deals with it?

This is referred to as the "standard template library" (STL) and is a
normative part of the C++ standard. Not only does the STL encompass
containers like you have mentioned but also algorithms like sort or find.

There are many books that discuss these.

The "reference" I used often but it is a little dated is the SGI web site
on the STL (I have not looked at them for a while myself - maybe it is
updated).
http://www.sgi.com/tech/stl/

SGI has implemented some containers that are not part of the standard so
you need to be a little careful.

The ones you will use almost all the time in modern c++ code are:

basic_string
vector
list
map
sort
The ones I know are not part of the current standard are:

hash_map
rope

... probably others.

The SGI examples are missing a few namespace specifiers. Usually a using
namespace std; somewhere in the example fixes those probs.

Hi Gianni -

that is a very cool link :-)
i think this is worth checking out !
the first 2 semesters of coding at school were very basic.
this is definitely some of the stuff that is missing.
Please, try not to top-post (fixed here), it makes it much harder to
read your posts and there are people who will not reply to people who
top-post which means you might miss out on some valuable information.

Another link to check out would be the following: www.cppreference.com,
which I believe is fully standards compliant and quite easy to navigate.

--
Erik Wikström
Jun 19 '07 #6

P: n/a
On Jun 19, 1:37 am, rpbg...@yahoo.com (Roland Pibinger) wrote:
On Tue, 19 Jun 2007 00:36:32 -0400, "3rdshiftcoder" wrote:
where would someone find things like lists, sets, map classes like a
hash map or tree map. not specifically those but in general is there
a book section that deals with it? in java you can use the interface
to these without having to know all of the implementation details.
is there an interface to a library with these kind of methods in c++?

Actually, there is no equivalent for Java collections in Standard C++.
Java collections work only with objects but not with values, Standard
C++ collections (a.k.a. STL) work only with values but not with
objects.

Having worked with both, I can kind of see what you mean. However, I
fundamentally disagree
with what you imply. It is true that c++ standard containers are by
value but there is no
restriction whatsoever regarding the type of that value. The value
can be a class in which
case, the objects themselves will be copied, potentially many times.
You can specify that
the containers carry pointers to objects. Finally, you can specify
that the containers can
carry shared pointers to objects which provide the same basic
advantages as Java's school marm
garbage collection.

regards,

Jon Trauntvein

Jun 19 '07 #7

P: n/a
On Jun 19, 3:37 am, rpbg...@yahoo.com (Roland Pibinger) wrote:
Actually, there is no equivalent for Java collections in Standard C++.
Java collections work only with objects but not with values, Standard
C++ collections (a.k.a. STL) work only with values but not with
objects.
In a frameowrk like COM, where everything already inherits from
IUnknown, tt is easy to use a shared pointer, and have something that
can store references to any object.
Even with such a framework, I agree that it is fairly difficult to
emulate Java Collections using C++. However, I have found the model
useful in some cases, when I want to model a sequence or collection in
an abstract interface.
In other words you have something like:
tempalte <class Tstruct Collection{
Iterator<Tbegin()=0;
Iterator<Tend()=0;
ConstIterator<Tbegin()const=0;
ConstIterator<Tend()const=0;
bool empty()const=0;
std::size_t size()const=0;

//plus all the requred typedefs
};
I have actually gotten this to work for read-only containers that
return const InputIterators. But going further (e.g. Forward
iterators, modifyable containers) was a lot of work for little
payoff.
Jun 19 '07 #8

P: n/a
On Jun 19, 9:37 am, rpbg...@yahoo.com (Roland Pibinger) wrote:
On Tue, 19 Jun 2007 00:36:32 -0400, "3rdshiftcoder" wrote:
where would someone find things like lists, sets, map classes like a
hash map or tree map. not specifically those but in general is there
a book section that deals with it? in java you can use the interface
to these without having to know all of the implementation details.
is there an interface to a library with these kind of methods in c++?
Actually, there is no equivalent for Java collections in Standard C++.
Java collections work only with objects but not with values, Standard
C++ collections (a.k.a. STL) work only with values but not with
objects.
That's to be expected, since C++ as a language works only with
values, where as Java works mainly with pointers. In this
respect, each of the libraries "fits in" with the language; a
C++ library which worked with pointers would be
counter-intuitive.

On the the other hand, the interfaces of the collections are
significantly different---the C++ two iterator idiom makes most
things significantly more verbose, is much more awkward to work
with, and much less flexible. (Of course, the Java iterator
combines access and increment, which has a few drawbacks as
well.)

--
James Kanze (GABI Software, from CAI) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 19 '07 #9

P: n/a
On Tue, 19 Jun 2007 11:16:27 -0000, JH Trauntvein wrote:
>Having worked with both, I can kind of see what you mean. However, I
fundamentally disagree
with what you imply. It is true that c++ standard containers are by
value but there is no
restriction whatsoever regarding the type of that value. The value
can be a class in which
case, the objects themselves will be copied, potentially many times.
You can specify that
the containers carry pointers to objects.
You are right that the container value type can be a value or a
pointer to an object. But nevertheless STL is designed with 'value
semantics' in mind, e.g.

std::vector<T*v;
std::sort (v.begin(), v.end());

compiles but gives false results.
>Finally, you can specify that the containers can
carry shared pointers to objects
pointers in C/C++ are shared by default ;-)
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Jun 19 '07 #10

P: n/a
On Tue, 19 Jun 2007 04:28:52 -0700, Lance Diduck wrote:
>I agree that it is fairly difficult to
emulate Java Collections using C++. However, I have found the model
useful in some cases, when I want to model a sequence or collection in
an abstract interface.
In other words you have something like:
tempalte <class Tstruct Collection{
Iterator<Tbegin()=0;
Iterator<Tend()=0;
ConstIterator<Tbegin()const=0;
ConstIterator<Tend()const=0;
bool empty()const=0;
std::size_t size()const=0;

//plus all the requred typedefs
};
I have actually gotten this to work for read-only containers that
return const InputIterators. But going further (e.g. Forward
iterators, modifyable containers) was a lot of work for little
payoff.
One should distinguish between containers for polymorphic objects and
polymorphic containers (your example). STL has neither.
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Jun 19 '07 #11

P: n/a
On Tue, 19 Jun 2007 11:36:32 -0000, James Kanze wrote:
>That's to be expected, since C++ as a language works only with
values, where as Java works mainly with pointers.
That C++ works only with values is, at least, misleading.
>In this
respect, each of the libraries "fits in" with the language; a
C++ library which worked with pointers would be
counter-intuitive.
RogueWave, Microsoft, Qt, ... successfully produced such
'counter-intuitive' libraries. OTOH, looking at STL-related postings
in newsgroups STL's 'value semantics' isn't intuitive at all for many.
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Jun 19 '07 #12

P: n/a
Gil
Roland Pibinger escribió:
On Tue, 19 Jun 2007 11:36:32 -0000, James Kanze wrote:
>That's to be expected, since C++ as a language works only with
values, where as Java works mainly with pointers.

That C++ works only with values is, at least, misleading.
>In this
respect, each of the libraries "fits in" with the language; a
C++ library which worked with pointers would be
counter-intuitive.

RogueWave, Microsoft, Qt, ... successfully produced such
'counter-intuitive' libraries. OTOH, looking at STL-related postings
in newsgroups STL's 'value semantics' isn't intuitive at all for many.

Anybody knows something like java's Files.list() in C++

Thanks
Jun 19 '07 #13

P: n/a
On 2007-06-19 20:49, Gil wrote:
Roland Pibinger escribió:
>On Tue, 19 Jun 2007 11:36:32 -0000, James Kanze wrote:
>>That's to be expected, since C++ as a language works only with
values, where as Java works mainly with pointers.

That C++ works only with values is, at least, misleading.
>>In this
respect, each of the libraries "fits in" with the language; a
C++ library which worked with pointers would be
counter-intuitive.

RogueWave, Microsoft, Qt, ... successfully produced such
'counter-intuitive' libraries. OTOH, looking at STL-related postings
in newsgroups STL's 'value semantics' isn't intuitive at all for many.

Anybody knows something like java's Files.list() in C++
There are no classes for accessing files and directories in standard
C++, but there are lots of third party libraries and any of the more
complete ones should have something similar.

--
Erik Wikström
Jun 19 '07 #14

P: n/a
On Jun 19, 1:49 pm, Gil <gilberto...@gmail.comwrote:
Roland Pibinger escribió:
Anybody knows something like java's Files.list() in C++
Method of accessing files varies with operating system. On Linux you
do it one way, on Windows you do it another way. Either way, you need
to either find a library that supports unified file access, or you
need to learn the specific method for the operating system you are
trying to support (Hint: In Windows lookup FindFirstFile /
FindNextFile, in Unix lookup opendir / readdir. This is highly
offtopic though, so please continue any further discussion about this
in the appropriate newsgroup.)

Jun 19 '07 #15

P: n/a
On Jun 19, 8:49 pm, Gil <gilberto...@gmail.comwrote:
Anybody knows something like java's Files.list() in C++
I've got one myself, but it's not standard. As of today,
there's no such thing in standard C++. But I believe that
there is something being proposed for the next version of the
standard.

--
James Kanze (GABI Software, from CAI) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 20 '07 #16

P: n/a
On 19 Jun, 19:49, Gil <gilberto...@gmail.comwrote:
Anybody knows something like java's Files.list() in C++
Boost has a Filesystem library.

http://www.boost.org/libs/filesystem/doc/index.htm

Gavin Deane

Jun 20 '07 #17

This discussion thread is closed

Replies have been disabled for this discussion.