CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 23 of 23
  1. #16
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    It took me too long to post ...

  2. #17
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    another point : you have a number of loops which are basically like this :
    Code:
    std::map<int,CGroup>::iterator g_pos = m_logData.m_groups.begin();
    
    while (g_pos != m_logData.m_groups.end())
    {
       if (g_pos->first == g)
       {
       }
       g_pos++;
    }
    You are looping thru the map looking for a particular key. This
    is O(N). std::map has a find function that is O(LOG(N)) :

    Code:
    std::map<int,CGroup>::iterator g_pos = m_logData.m_groups.find(g);
    
    if (g_pos != m_logData.m_groups.end())
    {
       // found
    }
    else
    {
       // not found
    }

  3. #18
    Join Date
    Jan 2004
    Location
    Netherlands
    Posts
    24
    Well, thanks anyway for the reply. It teaches me alot about the inner working of maps. By the way, that first solution you suggested does not seem to work in my case, that was the first thing I tried after I Deciphered those cryptic compiler messages. And another thing, "Effective STL". is that a book or an online guide or something? I would love to have some additional documentation on STL. Currently I only have the "Standard Template Library Programmer's Guide" but that's a bit too abstract for me.

  4. #19
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    The Meyers book is not available online. The first book I would
    recommend is "The C++ Standard Library : A Tutorial and
    Reference" by Nicolai Josuttis. (I also recommend the Meyers
    book, but I would get Josuttis first)

    Besides being a good reference, the "Tutorial" in the name
    is accurate. You can see the types of code presented
    in the book at :

    http://www.josuttis.com/libbook/toc.html

    Note: I don't know if my post concerning using map::find()
    instead of loop thru crossed paths with your post. If not,
    take a look at it.

  5. #20
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496
    A lot of people is asking aout STL and I for one only know what I actually worked with. maybe it would be a good ideea to have a forum dedicated to STL exclusivelly.
    Har Har

  6. #21
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    STL is an official part of the C++ language, so this forum is the perfect place for discussing STL.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  7. #22
    Join Date
    Jan 2004
    Location
    Netherlands
    Posts
    24
    Originally posted by Philip Nicoletti
    The Meyers book is not available online. The first book I would
    recommend is "The C++ Standard Library : A Tutorial and
    Reference" by Nicolai Josuttis. (I also recommend the Meyers
    book, but I would get Josuttis first)

    Besides being a good reference, the "Tutorial" in the name
    is accurate. You can see the types of code presented
    in the book at :

    http://www.josuttis.com/libbook/toc.html
    Looks good to me. I'm getting it ASAP.
    Note: I don't know if my post concerning using map::find()
    instead of loop thru crossed paths with your post. If not,
    take a look at it.
    Yes, it is much better. Not only faster but also more logical en better to understand.

  8. #23
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    Originally posted by Mza
    But this raises another question. How to directly acces the vector's elements (with O(1), like in an array). I thought Vectors should be able to do that but I saw it only with an integer or string, not with a class. The integer value that every class has could act as a key.
    I just read from the book, Effective STL, which shown that if the vector is sorted, we can use binary search to locate the required element in O(log n). The caveat is that using a sorted vector is most appropriate only when once the vector is initialized, there is very little chance of adding/removing element during lookup phase.

Page 2 of 2 FirstFirst 12

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