Should I be using something other than raw pointers as a helper indexinto a collection?

Discussion in 'C++' started by K. Frank, Jun 1, 2013.

  1. K. Frank

    K. Frank Guest

    Hello Group!

    I have a collection.

    (It happens to be an unordered_map, hence my earlier question
    about possible pointer invalidation, but for the purposes of
    this question it could be a map or set, etc.)

    In addition to element access through the built-in features
    of the collection class, I also want quick access to the first
    few elements according to some independent sorting criterion.

    Therefore my wrapper class contains not only the underlying
    collection, but a helper index that is a (sorted) set of
    pointers to values in the collection. I am using raw pointers
    for this, basically because I don't see any need to use some
    sort of smart pointer.

    I do know the various smart pointers have been getting better
    designed over the years and are now part of the standard.

    Is there a good reason I should be using some sort of smart
    pointer? Is there a compelling reason?

    Some details: In my particular use case, items are never
    removed form the main collection. If they were, of course,
    my wrapper class would need to keep the helper index in
    sync by also removing the corresponding index -- just as
    it keeps the helper index in sync when items are added.
    Also, the helper index is used only to access the items,
    not to modify or remove them.

    Thanks for an smart-pointer wisdom.


    K. Frank
     
    K. Frank, Jun 1, 2013
    #1
    1. Advertising

  2. Re: Should I be using something other than raw pointers as a helper index into a collection?

    K. Frank wrote:

    > Hello Group!
    >
    > I have a collection.
    >
    > (It happens to be an unordered_map, hence my earlier question
    > about possible pointer invalidation, but for the purposes of
    > this question it could be a map or set, etc.)
    >
    > In addition to element access through the built-in features
    > of the collection class, I also want quick access to the first
    > few elements according to some independent sorting criterion.
    >
    > Therefore my wrapper class contains not only the underlying
    > collection, but a helper index that is a (sorted) set of
    > pointers to values in the collection. I am using raw pointers
    > for this, basically because I don't see any need to use some
    > sort of smart pointer.
    >
    > I do know the various smart pointers have been getting better
    > designed over the years and are now part of the standard.
    >
    > Is there a good reason I should be using some sort of smart
    > pointer? Is there a compelling reason?
    >
    > Some details: In my particular use case, items are never
    > removed form the main collection. If they were, of course,
    > my wrapper class would need to keep the helper index in
    > sync by also removing the corresponding index -- just as
    > it keeps the helper index in sync when items are added.
    > Also, the helper index is used only to access the items,
    > not to modify or remove them.
    >
    > Thanks for an smart-pointer wisdom.


    The main use of smart pointers is for (automatic) lifetime management of
    the pointee. In your case, the lifetime is managed by your main
    collection, so there is no need to use smart pointers in your index
    collection.

    Gerhard
     
    Gerhard Fiedler, Jun 1, 2013
    #2
    1. Advertising

  3. K. Frank

    K. Frank Guest

    Re: Should I be using something other than raw pointers as a helperindex into a collection?

    Hi Gerhard!

    On Jun 1, 10:28 am, Gerhard Fiedler <> wrote:
    > K. Frank wrote:
    > > Hello Group!

    >
    > > I have a collection.
    > > ...
    > > Therefore my wrapper class contains not only the underlying
    > > collection, but a helper index that is a (sorted) set of
    > > pointers to values in the collection.  I am using raw pointers
    > > for this, basically because I don't see any need to use some
    > > sort of smart pointer.
    > > ...
    > > Is there a good reason I should be using some sort of smart
    > > pointer?  Is there a compelling reason?
    > > ...

    >
    > The main use of smart pointers is for (automatic) lifetime management of
    > the pointee. In your case, the lifetime is managed by your main
    > collection, so there is no need to use smart pointers in your index
    > collection.


    Thank you. That agrees with my basic thinking, although I wouldn't
    have been able to express it as cleanly as you did.

    > Gerhard


    Best.


    K. Frank
     
    K. Frank, Jun 1, 2013
    #3
  4. K. Frank

    Öö Tiib Guest

    Re: Should I be using something other than raw pointers as a helperindex into a collection?

    On Saturday, 1 June 2013 16:33:49 UTC+3, K. Frank wrote:
    > I have a collection.
    >
    > (It happens to be an unordered_map, hence my earlier question
    > about possible pointer invalidation, but for the purposes of
    > this question it could be a map or set, etc.)
    >
    > In addition to element access through the built-in features
    > of the collection class, I also want quick access to the first
    > few elements according to some independent sorting criterion.
    >
    > Therefore my wrapper class contains not only the underlying
    > collection, but a helper index that is a (sorted) set of
    > pointers to values in the collection. I am using raw pointers
    > for this, basically because I don't see any need to use some
    > sort of smart pointer.
    >
    > I do know the various smart pointers have been getting better
    > designed over the years and are now part of the standard.
    >
    > Is there a good reason I should be using some sort of smart
    > pointer? Is there a compelling reason?
    >
    > Some details: In my particular use case, items are never
    > removed form the main collection. If they were, of course,
    > my wrapper class would need to keep the helper index in
    > sync by also removing the corresponding index -- just as
    > it keeps the helper index in sync when items are added.
    > Also, the helper index is used only to access the items,
    > not to modify or remove them.


    You might be interested in Boost.Multi-index. Or not. See:

    http://www.boost.org/doc/libs/1_53_0/libs/multi_index/doc/index.html

    It is the sort of thing that one may actually need when simple sets
    and maps are not enough. It all depends what you do with it.
     
    Öö Tiib, Jun 1, 2013
    #4
  5. K. Frank

    K. Frank Guest

    Re: Should I be using something other than raw pointers as a helperindex into a collection?

    Hi Öö!

    On Jun 1, 12:06 pm, Öö Tiib <> wrote:
    > On Saturday, 1 June 2013 16:33:49 UTC+3, K. Frank  wrote:
    > > I have a collection.
    > > ...
    > > In addition to element access through the built-in features
    > > of the collection class, I also want quick access to the first
    > > few elements according to some independent sorting criterion.

    >
    > > Therefore my wrapper class contains not only the underlying
    > > collection, but a helper index that is a (sorted) set of
    > > pointers to values in the collection.  I am using raw pointers
    > > for this, basically because I don't see any need to use some
    > > sort of smart pointer.
    > > ...

    >
    > You might be interested in Boost.Multi-index. Or not. See:
    >
    > http://www.boost.org/doc/libs/1_53_0/libs/multi_index/doc/index.html
    >
    > It is the sort of thing that one may actually need when simple sets
    > and maps are not enough. It all depends what you do with it.


    Yes, this is a very good recommendation. I've looked at
    Boost.MultiIndex
    for this use case. It looks like what I'm doing is a baby version of
    the more full-featured functionality that Boost.MultiIndex provides.

    However (unlike many participants in this group) I tend to stay away
    from boost as it is large and unwieldy. I generally avoid large and
    unwieldy packages unless they offer nearly irreplaceable
    functionality.
    (For example, I do invest the time and effort in building and
    upgrading
    Qt, a GUI / application framework. Qt is large and unwieldy, but if
    you want to program GUI's, you need something like Qt.)

    For example, boost offers various smart pointers (now subsumed, I
    suppose,
    into C++11). But the idea that I should download and deal with the
    boost state-machine framework or spirit parser generator (both
    implemented
    with template meta-programming, if I'm not mistaken), just to get
    smart
    pointers, seems excessive to me. The notion that I should download
    the
    whole boost ball of wax, and then run various third-party scripts on
    it to strip away all of the unneeded non-dependencies, just to get a
    clean set of smart pointers -- well, I just don't get it.

    But I'd love to learn more about Boost.MultiIndex -- it seems like a
    good idea.

    Thanks.


    K. Frank
     
    K. Frank, Jun 1, 2013
    #5
  6. K. Frank

    Öö Tiib Guest

    Re: Should I be using something other than raw pointers as a helperindex into a collection?

    On Saturday, 1 June 2013 19:38:54 UTC+3, K. Frank wrote:
    > On Jun 1, 12:06 pm, Öö Tiib <> wrote:
    > > On Saturday, 1 June 2013 16:33:49 UTC+3, K. Frank  wrote:
    > > > I have a collection.
    > > > ...
    > > > In addition to element access through the built-in features
    > > > of the collection class, I also want quick access to the first
    > > > few elements according to some independent sorting criterion.

    > >
    > > > Therefore my wrapper class contains not only the underlying
    > > > collection, but a helper index that is a (sorted) set of
    > > > pointers to values in the collection.  I am using raw pointers
    > > > for this, basically because I don't see any need to use some
    > > > sort of smart pointer.
    > > > ...

    > >
    > > You might be interested in Boost.Multi-index. Or not. See:
    > >
    > > http://www.boost.org/doc/libs/1_53_0/libs/multi_index/doc/index.html
    > >
    > > It is the sort of thing that one may actually need when simple sets
    > > and maps are not enough. It all depends what you do with it.

    >
    > Yes, this is a very good recommendation. I've looked at
    > Boost.MultiIndex
    > for this use case. It looks like what I'm doing is a baby version of
    > the more full-featured functionality that Boost.MultiIndex provides.
    >
    > However (unlike many participants in this group) I tend to stay away
    > from boost as it is large and unwieldy. I generally avoid large and
    > unwieldy packages unless they offer nearly irreplaceable
    > functionality.


    Boost is collection of libraries. There are lot of libraries. Like
    hundred. The libraries are designed by different people and usually
    rather weakly related with each other. The people are usually known
    as good specialists. So what you get is a big pile of libraries
    written by good specialists with one download.

    > (For example, I do invest the time and effort in building and
    > upgrading
    > Qt, a GUI / application framework. Qt is large and unwieldy, but if
    > you want to program GUI's, you need something like Qt.)


    Yes, Qt is application framework. That is designed by one company and
    it is quite heavily interrelated with other parts of itself. I also
    use it sometimes to throw up quick prototype or a GUI for some tool.

    > For example, boost offers various smart pointers (now subsumed, I
    > suppose,
    > into C++11). But the idea that I should download and deal with the
    > boost state-machine framework or spirit parser generator (both
    > implemented
    > with template meta-programming, if I'm not mistaken), just to get
    > smart
    > pointers, seems excessive to me.


    You do not have to deal with all the libraries. Actually I have only
    once looked into several of them and others I have never looked into.
    Neither the performance nor usability nor maintainability of boost
    libraries is guaranteed to be best but it is often better than same
    thing reinvented by average Joe.

    I haven't measured, but lets say whole boost with everything built and
    ready takes 10Gb. 10Gb of hard-drive space costs less than 1 euro. The
    built executable that uses something from boost is about as large as
    built executable that does same thing without using anything from boost.

    > The notion that I should download the
    > whole boost ball of wax, and then run various third-party scripts on
    > it to strip away all of the unneeded non-dependencies, just to get a
    > clean set of smart pointers -- well, I just don't get it.


    Most of boost is header-only. That means if you do not #include it
    then you do not use it. Most of boost are templates. That means if
    you do not instantiate a template in your code then compiler generates
    nothing into your program. You only get what you use.

    > But I'd love to learn more about Boost.MultiIndex -- it seems like a
    > good idea.


    It can be too generic at first to get the thing declared. Once done
    it just works, like any other container.
     
    Öö Tiib, Jun 1, 2013
    #6
  7. K. Frank

    James Kanze Guest

    Re: Should I be using something other than raw pointers as a helperindex into a collection?

    On Saturday, June 1, 2013 2:33:49 PM UTC+1, K. Frank wrote:
    > I have a collection.


    > (It happens to be an unordered_map, hence my earlier question
    > about possible pointer invalidation, but for the purposes of
    > this question it could be a map or set, etc.)


    > In addition to element access through the built-in features
    > of the collection class, I also want quick access to the first
    > few elements according to some independent sorting criterion.


    > Therefore my wrapper class contains not only the underlying
    > collection, but a helper index that is a (sorted) set of
    > pointers to values in the collection. I am using raw pointers
    > for this, basically because I don't see any need to use some
    > sort of smart pointer.


    > I do know the various smart pointers have been getting better
    > designed over the years and are now part of the standard.


    I'm not sure that I'd agree with that statement. Of all of the
    smart pointers I've used (or tried to use), std::shared_ptr
    probably has the worst design of them all.

    > Is there a good reason I should be using some sort of smart
    > pointer? Is there a compelling reason?


    > Some details: In my particular use case, items are never
    > removed form the main collection. If they were, of course,
    > my wrapper class would need to keep the helper index in
    > sync by also removing the corresponding index -- just as
    > it keeps the helper index in sync when items are added.
    > Also, the helper index is used only to access the items,
    > not to modify or remove them.


    I don't see why anyone would want a smart pointer anywhere here.
    If the items are never removed from the main container, using
    smart pointers there is just introducing unnecessary
    complication. And using them in the secondary indexes would
    even be considered obfuscation---you're suggesting something
    about the secondary indexes which isn't true.

    --
    James
     
    James Kanze, Jun 2, 2013
    #7
  8. K. Frank

    Balog Pal Guest

    Re: Should I be using something other than raw pointers as a helperindex into a collection?

    On 6/1/2013 3:33 PM, K. Frank wrote:
    > Is there a good reason I should be using some sort of smart
    > pointer?


    In theory, sure. You use smart pointer for being smart. Because you
    expect the very feature it is smart on. Because it aligns with your demand.

    > Is there a compelling reason?


    That is up to you to decide. What smart behavior do you expect for the
    use case? Make a list. Them match it against the supply.

    If your list is empty to start with, the plain pointer is as smart as it
    gets.
     
    Balog Pal, Jun 2, 2013
    #8
  9. K. Frank

    K. Frank Guest

    Re: Should I be using something other than raw pointers as a helperindex into a collection?

    Hello James (and Balog)!

    On Jun 2, 9:53 am, James Kanze <> wrote:
    > On Saturday, June 1, 2013 2:33:49 PM UTC+1, K. Frank wrote:
    > > I have a collection.
    > > (It happens to be an unordered_map, hence my earlier question
    > > about possible pointer invalidation, but for the purposes of
    > > this question it could be a map or set, etc.)
    > > In addition to element access through the built-in features
    > > of the collection class, I also want quick access to the first
    > > few elements according to some independent sorting criterion.
    > > Therefore my wrapper class contains not only the underlying
    > > collection, but a helper index that is a (sorted) set of
    > > pointers to values in the collection.  I am using raw pointers
    > > for this, basically because I don't see any need to use some
    > > sort of smart pointer.
    > > I do know the various smart pointers have been getting better
    > > designed over the years and are now part of the standard.

    >
    > I'm not sure that I'd agree with that statement.  Of all of the
    > smart pointers I've used (or tried to use), std::shared_ptr
    > probably has the worst design of them all.


    Thank you for the heads-up on that.

    > > Is there a good reason I should be using some sort of smart
    > > pointer?  Is there a compelling reason?
    > > Some details:  In my particular use case, items are never
    > > removed form the main collection.  If they were, of course,
    > > my wrapper class would need to keep the helper index in
    > > sync by also removing the corresponding index -- just as
    > > it keeps the helper index in sync when items are added.
    > > Also, the helper index is used only to access the items,
    > > not to modify or remove them.

    >
    > I don't see why anyone would want a smart pointer anywhere here.
    > If the items are never removed from the main container, using
    > smart pointers there is just introducing unnecessary
    > complication.  And using them in the secondary indexes would
    > even be considered obfuscation---you're suggesting something
    > about the secondary indexes which isn't true.


    That was pretty much my conclusion, as well.

    I think, in Balog's wording, my "list is empty."

    The reason I posted the question in the first place is that I
    haven't used smart pointers much in the past, and I have yet
    to use a c++11 smart pointer. Nonetheless, one sees a lot of
    commentary advocating the use of smart pointers (where appropriate,
    of course), and since I don't have a deep understanding of the
    use cases for c++11 (and other) smart pointers, I wanted to make
    sure I wasn't missing anything.

    > ...
    > James


    Thanks for your advice.


    K. Frank
     
    K. Frank, Jun 2, 2013
    #9
  10. K. Frank

    Guest

    Re: Should I be using something other than raw pointers as a helperindex into a collection?

    On Saturday, June 1, 2013 7:01:03 PM UTC, Öö Tiib wrote:
    >
    > I haven't measured, but lets say whole boost with everything built and
    > ready takes 10Gb. 10Gb of hard-drive space costs less than 1 euro. The
    > built executable that uses something from boost is about as large as
    > built executable that does same thing without using anything from boost.
    >


    There are differences in the size of executables --
    http://webEbenezer.net/comparison.html
    One time the Boost version is 2.6 times larger and
    another time it's 4 times larger than Ebenezer versions.

    I think it makes sense to "chew the meat and spit the
    bones" with Boost. Some of the libraries are excellent,
    but not all of them. The intrusive containers
    library and some of the other containers in Boost
    are examples of what I think are great libraries.

    Brian
    Ebenezer Enterprises - John 3:16.
    http://webEbenezer.net
     
    , Jun 2, 2013
    #10
  11. K. Frank

    Öö Tiib Guest

    Re: Should I be using something other than raw pointers as a helperindex into a collection?

    On Sunday, 2 June 2013 23:54:34 UTC+3, wrote:
    > On Saturday, June 1, 2013 7:01:03 PM UTC, Öö Tiib wrote:
    > >
    > > I haven't measured, but lets say whole boost with everything built and
    > > ready takes 10Gb. 10Gb of hard-drive space costs less than 1 euro. The
    > > built executable that uses something from boost is about as large as
    > > built executable that does same thing without using anything from boost..
    > >

    > There are differences in the size of executables --
    > http://webEbenezer.net/comparison.html
    > One time the Boost version is 2.6 times larger and
    > another time it's 4 times larger than Ebenezer versions.


    Probably that boost serialize does more than than your software.
    Can the Ebenezer serialize that Boost.MultiIndex that I suggested
    to OP? As XML? Actually I dislike Boost.Serialize since it feels
    complicated.

    > I think it makes sense to "chew the meat and spit the
    > bones" with Boost. Some of the libraries are excellent,
    > but not all of them. The intrusive containers
    > library and some of the other containers in Boost
    > are examples of what I think are great libraries.


    When I feel that I do not understand how it does what it
    does then I avoid using it. I generally avoid using source
    code that appears too complex for me to maintain.
    Performance is of secondary importance. If I can
    maintain it then I can profile it too if needed.
     
    Öö Tiib, Jun 2, 2013
    #11
  12. K. Frank

    Guest

    Re: Should I be using something other than raw pointers as a helperindex into a collection?

    On Sunday, June 2, 2013 9:34:36 PM UTC, Öö Tiib wrote:
    > On Sunday, 2 June 2013 23:54:34 UTC+3, wrote:
    >
    > > There are differences in the size of executables --
    > > http://webEbenezer.net/comparison.html
    > > One time the Boost version is 2.6 times larger and
    > > another time it's 4 times larger than Ebenezer versions.

    >
    > Probably that boost serialize does more than than your software.
    > Can the Ebenezer serialize that Boost.MultiIndex that I suggested
    > to OP? As XML?


    "Yes" to the first question and "no" to the second.


    > Actually I dislike Boost.Serialize since it feels
    > complicated.


    Sometimes I think that an advantage to on line code
    generation, an unusual approach in 2002, is that
    people have been slow to jump on the bandwagon. I
    would have liked to have more users, but on the other
    hand it makes it easier to make interface breaking
    changes while there aren't many that are affected.
    A lot has been learned about interface design over
    the past ten years so I hope we're able to incorpor-
    ate more of that sooner than later.

    >
    > > I think it makes sense to "chew the meat and spit the
    > > bones" with Boost. Some of the libraries are excellent,
    > > but not all of them. The intrusive containers
    > > library and some of the other containers in Boost
    > > are examples of what I think are great libraries.

    >
    > When I feel that I do not understand how it does what it
    > does then I avoid using it. I generally avoid using source
    > code that appears too complex for me to maintain.
    > Performance is of secondary importance. If I can
    > maintain it then I can profile it too if needed.


    The compression library I use is only 991 lines, but I
    don't understand much of it. Probably it would take me
    weeks to get some idea of how it works. I'd communicate
    with the author though if there is a problem with the
    library. He's answered some questions I've had so
    hopefully he'll be available if there's a problem with it.


    Brian
    Ebenezer Enterprises - So far G-d has helped us.
    http://webEbenezer.net
     
    , Jun 3, 2013
    #12
  13. K. Frank

    Öö Tiib Guest

    Re: Should I be using something other than raw pointers as a helperindex into a collection?

    On Monday, 3 June 2013 05:23:14 UTC+3, wrote:
    > On Sunday, June 2, 2013 9:34:36 PM UTC, Öö Tiib wrote:
    > > When I feel that I do not understand how it does what it
    > > does then I avoid using it. I generally avoid using source
    > > code that appears too complex for me to maintain.
    > > Performance is of secondary importance. If I can
    > > maintain it then I can profile it too if needed.

    >
    > The compression library I use is only 991 lines, but I
    > don't understand much of it. Probably it would take me
    > weeks to get some idea of how it works. I'd communicate
    > with the author though if there is a problem with the
    > library. He's answered some questions I've had so
    > hopefully he'll be available if there's a problem with it.


    Sure, perhaps my view of things is because I just like to
    understand complex algorithms. Be it compression or linear algebra I
    am not happy when using it without feel that I will understand it
    if needed. It is written by a human like me and so it certainly
    contains defects. Take for example binary search. It is so simple?

    Jon Bentley: "While the first binary search was published in 1946,
    the first binary search that works correctly for all
    values of n did not appear until 1962."
    http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html

    I do not like needless complexity that is caused by usage of
    unsuitable tools. C++ template meta-programming was not designed
    into language and so it usually looks like the hack it is. Your
    idea to generate code is lot better at the moment.
     
    Öö Tiib, Jun 3, 2013
    #13
  14. K. Frank

    James Kanze Guest

    Re: Should I be using something other than raw pointers as a helperindex into a collection?

    On Sunday, June 2, 2013 6:12:15 PM UTC+1, K. Frank wrote:
    > Hello James (and Balog)!


    > The reason I posted the question in the first place is that I
    > haven't used smart pointers much in the past, and I have yet
    > to use a c++11 smart pointer. Nonetheless, one sees a lot of
    > commentary advocating the use of smart pointers (where appropriate,
    > of course), and since I don't have a deep understanding of the
    > use cases for c++11 (and other) smart pointers, I wanted to make
    > sure I wasn't missing anything.


    Smart pointers are the current silver bullet. It's a lot easier
    for commentators to recommend using a smart pointer everywhere
    than for them to recommend actually doing some design. There
    are places where smart pointers are an appropriate solution; my
    first smart pointers go back some twenty years or more. But
    there is no silver bullet, and I've found very few, if any cases
    where smart pointers were appropriate in a container.

    --
    James
     
    James Kanze, Jun 3, 2013
    #14
  15. K. Frank

    Guest

    Re: Should I be using something other than raw pointers as a helperindex into a collection?

    On Monday, June 3, 2013 2:23:14 AM UTC, wrote:
    > On Sunday, June 2, 2013 9:34:36 PM UTC, Öö Tiib wrote:
    >
    > > Probably that boost serialize does more than than your software.
    > > Can the Ebenezer serialize that Boost.MultiIndex that I suggested
    > > to OP? As XML?

    >
    > "Yes" to the first question and "no" to the second.
    >


    There are some limitations on our support of multi_index_container:
    1. At most one sequenced index.
    2. If a sequenced index is used, it must be the
    first index listed in the "IndexSpecifierList."
    3. Transmission of a multi_index_container will probably not
    preserve the relative order of elements that are
    considered equal under an ordered_non_unique index.


    These limitations permit a process which uses a
    multi_index_container to serialize it's data and
    then be used in another process that is using a
    "single index container" like vector or deque.

    I guess the first and third limitations are what are
    needed for that. The second one is due to an
    implementation difficulty.


    Brian
    Ebenezer Enterprises
    http://webEbenezer.net
     
    , Jun 3, 2013
    #15
  16. K. Frank

    Luca Risolia Guest

    Re: Should I be using something other than raw pointers as a helper index into a collection?

    K. Frank wrote:

    > Hello Group!
    >
    > I have a collection.
    >
    > (It happens to be an unordered_map, hence my earlier question
    > about possible pointer invalidation, but for the purposes of
    > this question it could be a map or set, etc.)
    >
    > In addition to element access through the built-in features
    > of the collection class, I also want quick access to the first
    > few elements according to some independent sorting criterion.
    >
    > Therefore my wrapper class contains not only the underlying
    > collection, but a helper index that is a (sorted) set of
    > pointers to values in the collection. I am using raw pointers
    > for this, basically because I don't see any need to use some
    > sort of smart pointer.


    Reference wrappers might be a convenient alternative to raw pointers.

    For example:

    using namespace std;

    unordered_map<int, int> m{{3, 1}, {2, 2}, {1, 3}};
    vector<reference_wrapper<decltype(m)::value_type>> v{begin(m), end(m)};
    sort(begin(v), end(v), less<decltype(m)::value_type>{});
     
    Luca Risolia, Jun 3, 2013
    #16
  17. K. Frank

    Balog Pal Guest

    Re: Should I be using something other than raw pointers as a helperindex into a collection?

    On 6/3/2013 11:26 PM, Luca Risolia wrote:
    > Reference wrappers might be a convenient alternative to raw pointers.
    >
    > For example:
    >
    > using namespace std;
    >
    > unordered_map<int, int> m{{3, 1}, {2, 2}, {1, 3}};
    > vector<reference_wrapper<decltype(m)::value_type>> v{begin(m), end(m)};


    Hm, is it worth?

    > sort(begin(v), end(v), less<decltype(m)::value_type>{});


    I have a small suite of function adapters for that case from the old
    times. Nowadays we have lambda for solo cases too.
     
    Balog Pal, Jun 4, 2013
    #17
  18. K. Frank

    Jorgen Grahn Guest

    Re: Should I be using something other than raw pointers as a helperindex into a collection?

    On Sat, 2013-06-01, Paavo Helde wrote:
    > "K. Frank" <> wrote in news:f302bee4-aa53-48e3-a3f7-
    > :
    >>
    >> However (unlike many participants in this group) I tend to stay away
    >> from boost as it is large and unwieldy. I generally avoid large and
    >> unwieldy packages unless they offer nearly irreplaceable
    >> functionality.

    ....
    > It is true that it is large but this only affects the initial download
    > time.


    It depends on your environment. (Parts of) Boost is a standard part
    of Debian Linux, and there it's split into >20 packages.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Jun 4, 2013
    #18
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Fish
    Replies:
    2
    Views:
    3,110
    Andy Fish
    Aug 11, 2004
  2. darrel
    Replies:
    5
    Views:
    3,704
    Lucas Tam
    May 6, 2005
  3. Øyvind Isaksen
    Replies:
    1
    Views:
    1,036
    Øyvind Isaksen
    May 18, 2007
  4. Piotr Kobzda
    Replies:
    3
    Views:
    2,740
    Daniel Pitts
    Jul 25, 2007
  5. kevin
    Replies:
    1
    Views:
    394
    Kairi Zikpin
    Jul 21, 2006
Loading...

Share This Page