Advanced features

Discussion in 'C++' started by Krice, Sep 11, 2009.

  1. Krice

    Krice Guest

    Why do people use advaced features of C++ like std::map? The only
    "advanced" features I'm using are std::vector and list, and string
    sometimes.
    I never felt that I would need anything more than that to produce
    working
    programs.
     
    Krice, Sep 11, 2009
    #1
    1. Advertising

  2. Krice

    Francesco Guest

    On 11 Set, 13:40, Krice <> wrote:
    > Why do people use advaced features of C++ like std::map? The only
    > "advanced" features I'm using are std::vector and list, and string
    > sometimes.
    > I never felt that I would need anything more than that to produce
    > working
    > programs.


    Well, that's fine. You should use only what you need.

    Maybe one day you'll want some container that keeps the elements
    sorted by some key, et voilà, there it comes std::map ;-)

    Cheers,
    Francesco
     
    Francesco, Sep 11, 2009
    #2
    1. Advertising

  3. * Krice:
    > Why do people use advaced features of C++ like std::map? The only
    > "advanced" features I'm using are std::vector and list, and string
    > sometimes.
    > I never felt that I would need anything more than that to produce
    > working
    > programs.


    There's nothing "advanced" about std::map, nor about std::vector, std::list or
    std::string.

    Those are basic types from the standard library.

    People generally use these types because the types meet their needs.

    std::string is an exception since its only redeeming feature is that it's
    standard and "cheap" (not requiring development time or buying a third party
    library); it's like people use Windows instead of Mac for those 2 reasons,
    standard and cheap, not because Windows of itself meets their needs.

    Except for that, use a proper tool for any given job, if you can. :)


    Cheers & hth.,

    - Alf
     
    Alf P. Steinbach, Sep 11, 2009
    #3
  4. Alf P. Steinbach wrote:
    > std::string is an exception since its only redeeming feature is that
    > it's standard and "cheap" (not requiring development time or buying a
    > third party library);


    You do not like std::string, isn't it? :)


    Marcel
     
    Marcel Müller, Sep 11, 2009
    #4
  5. Krice wrote:
    > Why do people use advaced features of C++ like std::map? The only
    > "advanced" features I'm using are std::vector and list, and string
    > sometimes.
    > I never felt that I would need anything more than that to produce
    > working
    > programs.


    You clearly have never programmed anything required advanced
    algorithms and data containers.

    An exercise: Write a function (which is as short and simple, but also
    as fast as possible) which answers the question "has this string been
    seen before?" (When it is asked that question with some string, that
    string is then considered "seen" from that point forward.)

    Yes, you can do that with a vector, but it won't be fast.
     
    Juha Nieminen, Sep 11, 2009
    #5
  6. Krice

    Jerry Coffin Guest

    In article <pszqm.290$>, lid
    says...
    >
    > Krice wrote:
    > > Why do people use advaced features of C++ like std::map? The only
    > > "advanced" features I'm using are std::vector and list, and string
    > > sometimes.
    > > I never felt that I would need anything more than that to produce
    > > working
    > > programs.

    >
    > You clearly have never programmed anything required advanced
    > algorithms and data containers.
    >
    > An exercise: Write a function (which is as short and simple, but also
    > as fast as possible) which answers the question "has this string been
    > seen before?" (When it is asked that question with some string, that
    > string is then considered "seen" from that point forward.)
    >
    > Yes, you can do that with a vector, but it won't be fast.


    Just FWIW, that's not a very good example for using a map though --
    I, at least, would do that with a set instead.

    A better (IMO, anyway) example of using a map would be if you were
    writing an interpreter for a small programming language (e.g. BASIC).
    A map would be a simple, straightforward way of storing the values of
    the variables. When the code being interpreted had something like:
    Let x = 1
    your program would (internally) use something like:
    floats['x'] = 1;
    or more accurately, something like:
    floats[variable_name] = value;

    You'd probably have something like three symbol tables, one each for
    strings, floats and ints (and as-per the rules of most older BASICs
    anyway) you'd figure out the type from the suffix on the name.

    If you wanted an example of a multimap, consider building a
    concordance of words -- e.g. for each word you encounter, you keep
    track of all the places (e.g. filename and line) where that word can
    be found. If you wanted to enhance it a bit, you could use a set
    (about like in the first example above) to hold really common words
    you did NOT want to store in the concordance. When you were building
    the concordance, you'd look each word up in that list, and only store
    positions of words that weren't found in the list.

    There is a multiset, but frankly, I've never seen much use for it. At
    least in most cases, it appears that a multiset<T> can be replaced
    with a map<T, int> pretty easily, and will probably run faster and
    use less storage.

    --
    Later,
    Jerry.
     
    Jerry Coffin, Sep 11, 2009
    #6
  7. Jerry Coffin wrote:
    > In article <pszqm.290$>, lid
    > says...
    >> Krice wrote:
    >>> Why do people use advaced features of C++ like std::map? The only
    >>> "advanced" features I'm using are std::vector and list, and string
    >>> sometimes.
    >>> I never felt that I would need anything more than that to produce
    >>> working
    >>> programs.

    >> You clearly have never programmed anything required advanced
    >> algorithms and data containers.
    >>
    >> An exercise: Write a function (which is as short and simple, but also
    >> as fast as possible) which answers the question "has this string been
    >> seen before?" (When it is asked that question with some string, that
    >> string is then considered "seen" from that point forward.)
    >>
    >> Yes, you can do that with a vector, but it won't be fast.

    >
    > Just FWIW, that's not a very good example for using a map though --
    > I, at least, would do that with a set instead.


    I was more thinking about maps/sets as data containers having O(log n)
    complexity for all operations, which are very well suited for this type
    of problem, especially if the code ought to be simple and clean (and
    when extremely efficient memory usage is not an imperative, as is the
    case in most simple situations).

    If we want to modify the exercise I presented in a way that using a
    map is more natural, then: Write a function which tells how many times a
    string has been "seen" (each time it's asked, it increases this "seen"
    count by one).
     
    Juha Nieminen, Sep 12, 2009
    #7
  8. Krice

    Ron Guest

    On Sep 11, 5:39 pm, Juha Nieminen <> wrote:

    >
    >   An exercise: Write a function (which is as short and simple, but also
    > as fast as possible) which answers the question "has this string been
    > seen before?" (When it is asked that question with some string, that
    > string is then considered "seen" from that point forward.)
    >
    >   Yes, you can do that with a vector, but it won't be fast.


    Yeah, but I wouldn't use map either. Set would be most appropriate
    for that problem.
     
    Ron, Sep 12, 2009
    #8
  9. Ron wrote:
    > On Sep 11, 5:39 pm, Juha Nieminen <> wrote:
    >
    >> An exercise: Write a function (which is as short and simple, but also
    >> as fast as possible) which answers the question "has this string been
    >> seen before?" (When it is asked that question with some string, that
    >> string is then considered "seen" from that point forward.)
    >>
    >> Yes, you can do that with a vector, but it won't be fast.

    >
    > Yeah, but I wouldn't use map either. Set would be most appropriate
    > for that problem.


    I really think this borders on nitpicking. There really isn't much
    difference between a map and a set. I bet that in most implementations
    both data containers use internally the exact same routines, the only
    difference being what is being compared (and a few public methods and
    iterators).

    When I posed that exercise, I was not thinking about std::map in
    particular, but about something which requires a better data container
    than std::vector or std::list, which is what the original poster said
    had sufficed for him for everything he has done.
     
    Juha Nieminen, Sep 12, 2009
    #9
  10. Krice

    Brian Wood Guest

    On Sep 12, 6:46 am, Ron <> wrote:
    > On Sep 11, 5:39 pm, Juha Nieminen <> wrote:
    >
    >
    >
    > >   An exercise: Write a function (which is as short and simple, but also
    > > as fast as possible) which answers the question "has this string been
    > > seen before?" (When it is asked that question with some string, that
    > > string is then considered "seen" from that point forward.)

    >
    > >   Yes, you can do that with a vector, but it won't be fast.

    >
    > Yeah, but I wouldn't use map either.   Set would be most appropriate
    > for that problem.



    I'd consider using an rbtree
    http://www.boost.org/doc/libs/1_40_0/doc/html/intrusive.html


    Brian Wood
    http://webEbenezer.net
     
    Brian Wood, Sep 12, 2009
    #10
  11. Krice

    Ramesh Guest

    On Sep 11, 4:40 am, Krice <> wrote:
    > Why do people use advaced features of C++ like std::map? The only
    > "advanced" features I'm using are std::vector and list, and string
    > sometimes.
    > I never felt that I would need anything more than that to produce
    > working
    > programs.


    You are definitely gonna use the data structures if you are gonna keep
    programming more and more. If you say, you will never, and you are
    provided with all you want; All you are gonna do is, end up recreating
    what was created for your convenience. That is good, if you are young
    and hungry, you may invent a new data structure with quite some
    amazing performance. :)

    Ramesh
     
    Ramesh, Sep 12, 2009
    #11
  12. Juha Nieminen, Sep 12, 2009
    #12
  13. Krice

    Krice Guest

    On 12 syys, 22:50, Ramesh <> wrote:
    > You are definitely gonna use the data structures if you are gonna keep
    > programming more and more.


    Maybe I expressed myself badly. I was talking about using
    advanced features for simple tasks. Like using templates
    everywhere. What's wrong with simple types and design?
     
    Krice, Sep 13, 2009
    #13
  14. Krice wrote:
    > Maybe I expressed myself badly. I was talking about using
    > advanced features for simple tasks. Like using templates
    > everywhere. What's wrong with simple types and design?


    You contrasted std::map with std::vector and std::list in your
    original post. You didn't mention "templates vs. non-templates".

    Who uses "templates everywhere" in simple programs?

    Besides, even if someone did, what's wrong with that? Templates don't
    have to make the program more complicated.
     
    Juha Nieminen, Sep 13, 2009
    #14
  15. Krice

    Balog Pal Guest

    "Juha Nieminen" <>
    > Krice wrote:
    >> Maybe I expressed myself badly. I was talking about using
    >> advanced features for simple tasks. Like using templates
    >> everywhere. What's wrong with simple types and design?

    >
    > You contrasted std::map with std::vector and std::list in your
    > original post. You didn't mention "templates vs. non-templates".
    >
    > Who uses "templates everywhere" in simple programs?


    Whoever uses std::string, std::vector and the rest of STL. And most other
    libs use templates too.

    > Besides, even if someone did, what's wrong with that? Templates don't
    > have to make the program more complicated.


    Yeah, what would make using templates 'more complicated' than say
    inheritance and virtual functions? Or exceptions? Or the other features in
    the language?

    Simple types (whatever that means) are okay, but has little interaction wit
    'simple design', and if you want to solve a real problem restricting toolset
    to whatever predetermined subset will hardly be simple.

    Was design and implementation of anything simpler in C, that have only those
    'simple' types? No, it is not, by far.
     
    Balog Pal, Sep 13, 2009
    #15
  16. Balog Pal wrote:
    >> Who uses "templates everywhere" in simple programs?

    >
    > Whoever uses std::string, std::vector and the rest of STL. And most other
    > libs use templates too.


    One could distinguish between two "uses" of templates, and the
    distinguishing feature is whether you explicitly write the keyword
    'template' yourself or not.

    Just using someone else's (eg. the standard library) template code
    does not necessarily entail the same level of design complexity as
    writing your own templates.

    I was more thinking of the latter when I wrote that sentence you quoted.

    >> Besides, even if someone did, what's wrong with that? Templates don't
    >> have to make the program more complicated.

    >
    > Yeah, what would make using templates 'more complicated' than say
    > inheritance and virtual functions? Or exceptions? Or the other features in
    > the language?


    Templates can also often be used to avoid code repetition, and are
    often a much cleaner way to do that than other alternatives, in which
    case using templates (as in the second meaning above) actually makes
    your code simpler, not more complicated.
     
    Juha Nieminen, Sep 13, 2009
    #16
  17. Krice

    Jerry Coffin Guest

    In article <sf5rm.63$>, lid
    says...

    [ ... ]

    > Just using someone else's (eg. the standard library) template
    > code does not necessarily entail the same level of design
    > complexity as writing your own templates.


    I'd state things more directly: it often entails a great deal less
    complexity.

    Perhaps I'm warped, but I find that in many cases I can simplify
    things by using a template. Just for example, consider something
    like:

    std::map<std::string, int>::value_type myfunc(
    std::map<std::string, int>::value_type> a,
    std::Map<std::string, int>::value_type> b)
    {
    // ...
    }

    What we have is fundamentally quite simple, but that simplicity has
    been lost in the simple length and repetition of the name of a type.
    You can cut down a bit on the typing, by using a typedef:

    typedef std::map<std::string, int>::value_type x;

    x myfunc(x a, x b) {
    // ...
    }

    This cuts down on the typing, and makes it apparent that myfunc just
    takes two things of the same kind, combines them somehow or other,
    and returns another object of that same type. Unfortunately, to do
    that, it pollutes the namespace with an otherwise useless name ('x'
    in this case, though you'd undoubtedly want to use something more
    meaningful in real code). While we only have to type out that type
    name once instead of three times, we do still have to type it out in
    full once. A template lets us eliminate typing it even one time,
    while avoiding introducing a new name for the type (except inside the
    template itself):

    template <class T>
    T myfunc(T a, T b) {
    // ...
    }

    --
    Later,
    Jerry.
     
    Jerry Coffin, Sep 13, 2009
    #17
    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. Kl
    Replies:
    1
    Views:
    319
    Kent Johnson
    Dec 4, 2004
  2. Jonathan Mcdougall
    Replies:
    2
    Views:
    517
    Kaz Kylheku
    Nov 3, 2005
  3. BillJosephson
    Replies:
    148
    Views:
    2,725
    peter koch
    Jan 27, 2007
  4. jvpic
    Replies:
    6
    Views:
    482
    Bruno Desthuilliers
    Aug 28, 2009
  5. Dan Smith
    Replies:
    0
    Views:
    376
    Dan Smith
    Jan 9, 2013
Loading...

Share This Page