Re: ISO standards
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. 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.
Which is better and where is one better used or is it just how the
programmer
feels?
------------------------------------------------------------------------
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <ctime>
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::cout << std::endl;
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.
Much snippage follows.
#include <iostream>
using std::cout;
I tend to think that using, even using std::cout; is not good. YMWV.
It preference. I like define them in the header file.
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};
It's not clear to me why inum isn't vector.
It can be. But you can think of this as a C string that gets sorted in a
vector. It can be a conversion.
A vector as a utility program.
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.
What I have that is new elimates the while and used a set of for/loops.
Run the code or the first program and will see that is what it does.
Couldn't all of these just be in a loop? It's also not clear to me why
you're sorting after each push_back.
code doesn't like to do what you want. You got to do what it wants.
Your code below isn't right for the code below. It uses vectors, but
your results doesn't take the form the code is to generate.
and then when you're done going through all the numbers, just sort each
vector once? Watch out for 10 being a magic number
for(unsigned int i=0; i<10; i++) {
std::sort(ij[0].begin(), ij[0].end());
}
Ten is the magic number! Any array from 0-99 will be separated from
0-9,10-19,20-29....99. That is why it has its form. The below code is
really the same. It does the same.
The rest of this was less clear to me, so I didn't read it all that
carefully. Besides, I doubt that it would compile.
It all compiles and functions in the first program. The second was my
confusion of vectors.
It is resolve below.
The one below shows better what its suppose to do with the 'friend' keyword.
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.
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?
// sort by 10th place.cpp
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
#include <vector>
using std::vector;
#include <algorithm>
using std::sort;
using std::copy;
using std::ostream_iterator;
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?
};
void fx(int inum[], vector<int>ij[])
{
int tmp = 0;
for (int ii = 0; ii < 10; ii++)
{
for(int i = 0; i < 31; i++)
{
if(inum[i]/10 == ii)
{
tmp = inum[i];
ij[ii].push_back(tmp);
sort(ij[ii].begin(), ij[ii].end());
i++;
}
}
}// end for loop
for(int ii = 0; ii < 10;ii++)
{
copy(ij[ii].begin(), ij[ii].end(),ostream_iterator<int>(cout," "));
cout << endl;
}
}
int main()
{
int inum[] =
{1,50,98,9,76,2,88,22,49,52,0,89,54,25,4,56,97,5,40,44,17,30,75,6,33,7,36,10,58,38,65};
vector<int> ij[10]; // each ij object can only hold 10 numbers per
object. So
fx( inum,ij); // you have only 100 numbers from 0 to 99 to
process here.
return 0;
}
This program is completely functional. The array can be changed to any set
of numbers from 0-99.
Best regards,
Jeff
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]