Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Advanced features

Reply
Thread Tools

Advanced features

 
 
Krice
Guest
Posts: n/a
 
      09-11-2009
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.
 
Reply With Quote
 
 
 
 
Francesco
Guest
Posts: n/a
 
      09-11-2009
On 11 Set, 13:40, Krice <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      09-11-2009
* 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
 
Reply With Quote
 
Marcel Müller
Guest
Posts: n/a
 
      09-11-2009
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
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      09-11-2009
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.
 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      09-11-2009
In article <pszqm.290$(E-Mail Removed)>, http://www.velocityreviews.com/forums/(E-Mail Removed)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.
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      09-12-2009
Jerry Coffin wrote:
> In article <pszqm.290$(E-Mail Removed)>, (E-Mail Removed)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).
 
Reply With Quote
 
Ron
Guest
Posts: n/a
 
      09-12-2009
On Sep 11, 5:39*pm, Juha Nieminen <(E-Mail Removed)> 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.

 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      09-12-2009
Ron wrote:
> On Sep 11, 5:39 pm, Juha Nieminen <(E-Mail Removed)> 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.
 
Reply With Quote
 
Brian Wood
Guest
Posts: n/a
 
      09-12-2009
On Sep 12, 6:46*am, Ron <(E-Mail Removed)> wrote:
> On Sep 11, 5:39*pm, Juha Nieminen <(E-Mail Removed)> 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...intrusive.html


Brian Wood
http://webEbenezer.net
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Some advanced analytics features that Google Analytics is lacking Dan Smith HTML 0 01-09-2013 05:30 PM
Learning Python advanced features jvpic Python 6 08-28-2009 11:48 AM
Are c++ features a subset of java features? BillJosephson C++ 148 01-27-2007 11:40 AM
Two favorite features of C++, and over-rated, and misued features Jonathan Mcdougall C++ 2 11-03-2005 06:22 PM
about python advanced/new features documentation Kl Python 1 12-04-2004 07:48 PM



Advertisments