-
January 30th, 2012, 09:30 AM
#1
Java container types counterparts in C++
Is boost the best bet that i can make use of containers something similar in java
like
SortedMap ArrayList...etc
Thanks
Jack
-
January 30th, 2012, 10:05 AM
#2
Re: Java container types counterparts in C++
Boost contains dozens of libraries with widely varying purposes.
The first place to look for containers in C++ is the STL library, which is part of standard C++. They are nothing like Java containers AFAIK, which is great, because C++ is nothing like Java.
Last edited by D_Drmmr; January 30th, 2012 at 10:08 AM.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
January 30th, 2012, 11:06 AM
#3
Re: Java container types counterparts in C++
STL containers and their approximate Java equivalents:
ArrayList -> std::vector
LinkedList -> std::list
TreeMap -> std::map/std::set/std::multimap/std::multiset
HashMap -> std::unordered_map etc (or boost::unordered_map on older compilers)
C++ also has std:eque, which looks similar to std::vector but is different internally, and some adapters std::stack, std::queue, and std:riority_queue which are not containers but can be used to modify the interface of standard containers if desired.
Boost has some additional containers.
Last edited by Lindley; January 30th, 2012 at 11:09 AM.
-
January 30th, 2012, 11:50 AM
#4
Re: Java container types counterparts in C++
std::map == SortedMap?
are they equivalent?
besides, what is the equivalent for stdext::hash_map in Java?
Thanks
Jack
Last edited by lucky6969b; January 30th, 2012 at 12:20 PM.
-
January 30th, 2012, 12:24 PM
#5
Re: Java container types counterparts in C++
Originally Posted by lucky6969b
std::map == SortedMap?
are they equivalent?
Yes and no. Yes, in the sense that the typically expected usage of a std::map would be identical to that of a SortedMap. No, because the details of their interfaces are different, and in fact std::map is a concrete class template whereas SortedMap is an interface construct.
Originally Posted by lucky6969b
what is the equivalence for stdext::hash_map in Java?
If the name is any clue, then stdext::hash_map's rough equivalent in Java is obviously HashMap.
-
January 30th, 2012, 12:28 PM
#6
Re: Java container types counterparts in C++
In other words, do I need to write extra code to keep it sorted?
Thanks
Jack
-
January 30th, 2012, 12:37 PM
#7
Re: Java container types counterparts in C++
Originally Posted by lucky6969b
In other words, do I need to write extra code to keep it sorted?
No.
I suggest that you read Josuttis' The C++ Standard Library - A Tutorial and Reference. It is outdated now that the new edition of the C++ standard has been published, but it is still relevant.
-
January 30th, 2012, 12:38 PM
#8
Re: Java container types counterparts in C++
Originally Posted by lucky6969b
In other words, do I need to write extra code to keep it sorted?
Thanks
Jack
You need to provide the key class with an operator<, OR you need to explicitly provide a comparison function defining a strict weak ordering when the map is constructed.
This sort of approach (either explicitly provide a comparator, or use the type's natural operators by default) is used throughout the STL.
-
January 30th, 2012, 12:41 PM
#9
Re: Java container types counterparts in C++
Thanks all of you. I think std::map<int, int, comparator> will do the job
-
January 30th, 2012, 12:44 PM
#10
Re: Java container types counterparts in C++
If the key is a primitive such as an int, and the natural notion of ordering is sufficient, then there is no need to do anything special to keep the container sorted.
One thing to watch out for: If you are using a string as the key, it must be a std::string or other string class defining operator< lexicographically. C-style strings, which are essentially just char arrays, will *not* provide the expected behavior when used as map keys (only the pointer values will be compared).
-
January 31st, 2012, 12:07 AM
#11
Re: Java container types counterparts in C++
I wonder how to write a comparator with std::string as the key
Code:
struct Comparator
{
int operator() (const std::string& str1, const std::string& str2)
{
// Any ideas?
}
};
std::map<std::string, int, Comparator> m_SortedMap;
Thanks
Jack
-
January 31st, 2012, 12:18 AM
#12
Re: Java container types counterparts in C++
Since you presumably do not want to use the default "less than" comparison for std::string as provided by the overloaded operator<, how you do want to compare the strings? That is central to implementing this "less than" comparison function.
-
January 31st, 2012, 12:57 AM
#13
Re: Java container types counterparts in C++
I'd like to have it in the ascending order.
BTW how do we get the first item out of an ordered std::set?
I tried m_Set[0] and we can't use begin() since it returns an iterator
Thanks a lot
Jack
Last edited by lucky6969b; January 31st, 2012 at 12:59 AM.
-
January 31st, 2012, 01:23 AM
#14
Re: Java container types counterparts in C++
Originally Posted by lucky6969b
I'd like to have it in the ascending order.
Then you don't need a special comparator since operator< works.
Originally Posted by lucky6969b
BTW how do we get the first item out of an ordered std::set?
I tried m_Set[0] and we can't use begin() since it returns an iterator
begin() returns an iterator to the first element, if it exists.
-
January 31st, 2012, 01:30 AM
#15
Re: Java container types counterparts in C++
You mean str1 < str2;?
Thanks
Jack
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|