Re: sorting the input
On Sep 10, 3:13 pm, ??? <lostgold...@163.com> wrote:
arnuld ??:
A program that asks user to input words and then prints them
in alphabetical order. I have used vectors to accomplish
task. That left me wondering with 2 questions:
1) whether list will be a good idea. I am basically
concerned about CPU efficiency.
It might be better to worry about whether the code works or not.
2) Is the program is a C++ program or C program written in
C++.
you wrote nice code of STL style, but i think use typedef to
redefine some iterator type is better to read.
Question of taste. I find that a lot of such typedef's actually
make the code harder to read. I know exactly what an
std::vector<>::iterator is and does; I don't know what a
VectIter is or does.
in my point of view,the std::sort works better with
std::vector than std::list,
That's putting it mildly. Calling std::sort with iterators from
an std::list is undefined behavior, and I suspect that it will
fail to compile with most compilers. std::list does have a sort
member function, however.
but, the std::vector<typename T>::push_back is less efficient
than std::list<>::insert.
Are you sure about that? I'm not. In the few times I've
actually measured, std::vector<>push_back has turned out to be
faster than std::list<>::push_back.
About the only time you would want to use std::list<> is when
you need to insert and/or erase somewhere in the middle of the
sequence. And even then, only if the sequence is long, and the
objects expensive to copy.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34