Re: ISO standards
Jeffrey Baker wrote:
Jeffrey Baker wrote:
Why hasn't ISO seen it needed to change the vector library to one that is
i
easy to use like the string library?
With <string> using std:::string; I can create string objects like
"string
st, str;" and work from within the class implementation. The vector
libray
should be the same. Yes, objects are created and they are in the
friendship
form of OOP outside the class where global scope objects are created and
used. This make implementation more difficult for the beginner of vectors
and is a greater abstraction from C style OPP.
Sorry but please explain what you mean in language which I can
understand. I have never had the slightest problem with using
std::vector nor have any of my students or readers.
My problem and confusion is the nature of the stl vector.
I will show you two programs one that is vectors without Object Oriented
Programming -OOP- and one program that tries to use OOP. This is the only
way I know to show the problem.
///////////////////////////////////////////////////////////////////////////////////////
// sort by 10th place.cpp
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?
------------------------------------------------------------------------
#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;
}
};
std::vector<int> randomVector(const unsigned int n) {
srand( (unsigned)time( NULL ) );
std::vector<int> result;
for(unsigned int i=0; i<n; i++) {
result.push_back( rand() );
}
return result;
}
int main() {
const unsigned int numberOfNumbers = 12;
std::vector<int> inum = randomVector(numberOfNumbers);
// write out the original order
std::copy(inum.begin(),
inum.end(),std::ostream_iterator<int>(std::cout," "));
std::cout << std::endl;
Compare compare;
std::sort(inum.begin(), inum.end(), compare);
std::copy(inum.begin(),
inum.end(),std::ostream_iterator<int>(std::cout," "));
}
--------------------------------------------------------------------------
Much snippage follows.
#include <iostream>
using std::cout;
I tend to think that using, even using std::cout; is not good. YMWV.
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.
int main()
{
/* int i;
int inum[1000];
srand( (unsigned)time( NULL ) );
for( i = 0; i < 100;i++ )
inum[i] = rand()/100;*/
vector<int> ij[10];
Why isn't that, std::vector< std::vector<int> > ij(10); ?
vector<int>::iterator z;
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.
{
int tmp = 0;
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.
if(inum[i]/10 == 0)
{
tmp = inum[i];
ij[0].push_back(tmp);
sort(ij[0].begin(), ij[0].end());
}
like,
for(int j = 0; j<sizeof(inum)/sizeof(inum[0]); j++) {
const tmp = inum[j];
for(int i=0; i<10; i++) {
if(tmp/10 == i)
ij[i].push_back(tmp);
// might want to break when this condition is met
}
}
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());
}
////////////////////////////////////////////////////////////////////////////////////////
It doesn't integrate well.
//////////////////////////
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.
int main()
{
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};
Visort ob(Visort &bt,inum[],31);
return 0;
}
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?
LR
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]