Re: ISO standards
Jeffrey Baker wrote:
It's not quite clear to me what it is you're trying to show here. I
suspect that you want to sort by the tens digit and sort each set of
numbers that are grouped by their tens digit. It's not clear to me why
you need ten vectors to do that. Is this what you wanted to do? Or at
least close to it?
It is mainly to learn vectors and become comfortable with its nature with
OOPs.
Jeffrey, OOP is a tool. There are cases where it is a useful tool and others
where it is not. The STL[1] is all about the separation between algorithms,
containers and iterators, there is no OOP in it and there is also no need
for it there. Your problems don't stem from the STL but rather from your
expectations that something must be solved using OOP.
To me it is still a new challenge and a litgitimate problem to
solve. I finally figured the best form of vector in relation to OOPs.
There are three types of arrays. The C style, the link list and the STL
form.
Calling a linked list an array is slightly misleading. Otherwise, there are
more containers in the STL than just list<> and vector<>.
Which is better and where is one better used or is it just how the
programmer feels?
Different containers all have their advantages and disadvantages.
class Compare { // yes, that's a horrible name for a class.
static int extractTensDigit(const int &i) {
const int result = (i/10)%10;
return result;
}
public:
bool operator()(const int &i1, const int &i2) const {
const int e1 = extractTensDigit(i1);
const int e2 = extractTensDigit(i2);
const bool result =
e1 < e2 ? true :
e2 < e1 ? false :
i1 < i2;
return result;
}
I don't know what you were try here. The STL std::sort does the task.
Why use this?
std::sort uses std::less by default to sort the elements. Using a custom
comparator allows you to sort a sequence by different criteria like e.g.
the value of the second digit. This should be explained in any better book
covering the STL-descendent parts of the C++ stdlib, in case you don't have
one yet, take a look at the reviews at ACCU's website before buying one.
Compare compare;
std::sort(inum.begin(), inum.end(), compare);
The'compare' here doesn't break down the intergers into ordered sets.
Take 'compare' out and you get sorted set of numbers.
Well, if you want to sort into different containers depending on a
criterium, std::sort is not the right tool. std::sort is only for sorting
elements of the same sequence. Otherwise, the code above does group the
numbers inside the vector according to the tens digit, only that they
remain in the same vector.
int inum[] => {1,50,98,9,76,2,88,22,49,52,3,89,54,25,4,
56,97,5,40,44,17,30,75,6,33,7,36,10,58,38,65};
[...]
int i = 0;
while(inum[i] > 0)
Did you mean to tack a zero onto the end of inum above? Sorry, maybe
I'm having trouble following, but this looks like it might not terminate
correctly.
It terminated just fine like it would in a link list.while(current != 0)
while(inum[i] != 0) is the same and works too.
Ahem, no. There is no element that is less than or equal to zero in 'inum',
so if you find something less than or equal to zero it is not in that
array, which means that your program is broken because it accessed elements
beyond the end of an array. Note that C++ other than e.g. Java does
explicitly not check every access but puts it on the programmer to check
them.
I'm still not clear on what your objection is. I also think I may not be
clear on what task you want to accomplish. Perhaps you could clarify
both of these for me?
I am not objecting to it. I want to use STL, but I want to see where it is
best used.
The program I have below uses 'friend' to make it class worthy.
What is 'class worthy'? If you think every code should belong to a class
because that is good programming style you are mistaken. Remember, OOP is a
means not a goal!
Now the program works with the 'friend ' keyword and without
How is this safe code? How is 'friend' protecting the code, like
member function keep code safe? Any simularity?
[...]
class Tenth
{
void friend fx(int , vector<int>,vector<int>);
// take friend ou and leave the rest
// what would be the difference?
public:
// How strong is friend in OOPs?
};
Since 'Tenth' doesn't have any private parts, there is not much
which 'friend' would change. 'friend' is used to allow a free/unbound
function the same access as a memberfunction. This is particularly useful
for operator overloading and similar functions that should not be made part
of the class itself even though they logically belong to the class.
Further, since 'Tenth' is unused, any changes there are pretty meaningless
as argument, don't you think so? Also, what does that have to do with
usefulness of vector? Please, if you have a separate question, do also post
that in a separate thread.
Uli
[1] Note: the STL is a library which was mostly integrated in the C++
standard. I'm referring to only the parts of the C++ standard that came
from the STL or are written in the spirit of it.
--
Sator Laser GmbH
Gesch?ftsf?hrer: Michael W?hrmann, Amtsgericht Hamburg HR B62 932
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]