Advanced features

K

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

Francesco

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
 
A

Alf P. Steinbach

* 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
 
M

Marcel Müller

Alf said:
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
 
J

Juha Nieminen

Krice said:
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.
 
J

Jerry Coffin

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

Juha Nieminen

Jerry said:
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).
 
R

Ron

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

Juha Nieminen

Ron said:
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.
 
R

Ramesh

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
 
K

Krice

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?
 
J

Juha Nieminen

Krice said:
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.
 
B

Balog Pal

Juha Nieminen said:
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.
 
J

Juha Nieminen

Balog said:
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.
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.
 
J

Jerry Coffin

[ ... ]
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) {
// ...
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top