CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Dec 2010
    Posts
    907

    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

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    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

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  4. #4
    Join Date
    Dec 2010
    Posts
    907

    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.

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Java container types counterparts in C++

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

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Dec 2010
    Posts
    907

    Re: Java container types counterparts in C++

    In other words, do I need to write extra code to keep it sorted?
    Thanks
    Jack

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Java container types counterparts in C++

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Java container types counterparts in C++

    Quote Originally Posted by lucky6969b View Post
    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.

  9. #9
    Join Date
    Dec 2010
    Posts
    907

    Re: Java container types counterparts in C++

    Thanks all of you. I think std::map<int, int, comparator> will do the job

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

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

  11. #11
    Join Date
    Dec 2010
    Posts
    907

    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

  12. #12
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  13. #13
    Join Date
    Dec 2010
    Posts
    907

    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.

  14. #14
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Java container types counterparts in C++

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

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  15. #15
    Join Date
    Dec 2010
    Posts
    907

    Re: Java container types counterparts in C++

    You mean str1 < str2;?

    Thanks
    Jack

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured