c++ equivalent of java collections

3

3rdshiftcoder

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
 
G

Gianni Mariani

3rdshiftcoder said:
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.
 
3

3rdshiftcoder

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
 
R

Roland Pibinger

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.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

>
> 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.
 
J

JH Trauntvein

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
 
L

Lance Diduck

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 T> struct Collection{
Iterator<T> begin()=0;
Iterator<T> end()=0;
ConstIterator<T> begin()const=0;
ConstIterator<T> end()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.
 
J

James Kanze

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.)
 
R

Roland Pibinger

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 ;-)
 
R

Roland Pibinger

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 T> struct Collection{
Iterator<T> begin()=0;
Iterator<T> end()=0;
ConstIterator<T> begin()const=0;
ConstIterator<T> end()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.
 
R

Roland Pibinger

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.
 
G

Gil

Roland Pibinger escribió:
That C++ works only with values is, at least, misleading.


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
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Roland Pibinger escribió:
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.
 
Z

Zachary Turner

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.)
 
J

James Kanze

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top