Re: ISO standards
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
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
#include <vector>
using std::vector;
#include <algorithm>
using std::sort;
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 main()
{
/* int i;
int inum[1000];
srand( (unsigned)time( NULL ) );
for( i = 0; i < 100;i++ )
inum[i] = rand()/100;*/
vector<int> ij[10];
vector<int>::iterator z;
int i = 0;
while(inum[i] > 0)
{
int tmp = 0;
if(inum[i]/10 == 0)
{
tmp = inum[i];
ij[0].push_back(tmp);
sort(ij[0].begin(), ij[0].end());
}
if(inum[i]/10 == 1)
{
tmp = inum[i];
ij[1].push_back(tmp);
sort(ij[1].begin(), ij[1].end());
}
if(inum[i]/10 == 2)
{
tmp =inum[i];
ij[2].push_back(tmp);
sort(ij[2].begin(), ij[2].end());
}
if(inum[i]/10 == 3)
{
tmp = inum[i];
ij[3].push_back(tmp);
sort(ij[3].begin(), ij[3].end());
}
if(inum[i]/10 == 4)
{
tmp = inum[i];
ij[4].push_back(tmp);
sort(ij[4].begin(), ij[4].end());
}
if(inum[i]/10 == 5)
{
tmp = inum[i];
ij[5].push_back(tmp);
sort(ij[5].begin(), ij[5].end());
}
if(inum[i]/10 == 6)
{
tmp = inum[i];
ij[6].push_back(tmp);
sort(ij[6].begin(), ij[6].end());
}if(inum[i]/10 == 7)
{
tmp = inum[i];
ij[7].push_back(tmp);
sort(ij[7].begin(), ij[7].end());
}
if(inum[i]/10 == 8)
{
tmp = inum[i];
ij[8].push_back(tmp);
sort(ij[8].begin(), ij[8].end());
}
if(inum[i]/10 == 9)
{
tmp = inum[i];
ij[9].push_back(tmp);
sort(ij[9].begin(), ij[9].end());
}
i++;
}
for( i = 0;i < 10; i++)
{
for( z = ij[i].begin(); z != ij[i].end(); z++)
{
cout << *z << ' ';
}
cout << endl;
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////
SECOND PROGRAM
The string st;
st = "Hello World!" is easily integrated in the OOPs structure. I didn't
add to the programs.
It doesn't integrate well.
////////////////////////////////////////////////////////////////////////////////////////
// vsort.h
#ifndef VSORT_H
#define VSORT_H
#include <iostream>
using std::cout;
using std::endl;
#include <vector>
using std::vector;
#include <algorithm>
using std::sort;
vector<int> ij[10];
vector<int>::iterator z;
class Visort
{
friend void Filtervisort(Visort& , const int);
public:
Visort();
Visort(Visort &, int[], int);
void Filtervisort(Visort&, const int);
private:
int ToSort[31];
};
void Filtervisort(Visort &bm,const int index)
{
const int am = index;
int ToSort[31];
for(int g = 0; g < am; g++)
ToSort[g] = bm.ToSort[g];
int i = 0;
while(ToSort[i] > 0)
{
int tmp = 0;
if(ToSort[i]/10 == 0)
{
tmp = ToSort[i];
ij[0].push_back(tmp);
sort(ij[0].begin(), ij[0].end());
}
if(ToSort[i]/10 == 1)
{
tmp = ToSort[i];
ij[1].push_back(tmp);
sort(ij[1].begin(), ij[1].end());
}
if(ToSort[i]/10 == 2)
{
tmp =ToSort[i];
ij[2].push_back(tmp);
sort(ij[2].begin(), ij[2].end());
}
if(ToSort[i]/10 == 3)
{
tmp = ToSort[i];
ij[3].push_back(tmp);
sort(ij[3].begin(), ij[3].end());
}
if(ToSort[i]/10 == 4)
{
tmp = ToSort[i];
ij[4].push_back(tmp);
sort(ij[4].begin(), ij[4].end());
}
if(ToSort[i]/10 == 5)
{
tmp = ToSort[i];
ij[5].push_back(tmp);
sort(ij[5].begin(), ij[5].end());
}
if(ToSort[i]/10 == 6)
{
tmp = ToSort[i];
ij[6].push_back(tmp);
sort(ij[6].begin(), ij[6].end());
}if(ToSort[i]/10 == 7)
{
tmp = ToSort[i];
ij[7].push_back(tmp);
sort(ij[7].begin(), ij[7].end());
}
if(ToSort[i]/10 == 8)
{
tmp = ToSort[i];
ij[8].push_back(tmp);
sort(ij[8].begin(), ij[8].end());
}
if(ToSort[i]/10 == 9)
{
tmp = ToSort[i];
ij[9].push_back(tmp);
sort(ij[9].begin(), ij[9].end());
}
i++;
}
for( i = 0;i < 10; i++)
{
for( z = ij[i].begin(); z != ij[i].end(); z++)
{
cout << *z << ' ';
}
cout << endl;
}
}
#endif
///////////////////////////////////////////////////////////////
// Implementvsort.cpp
#include "vsort.h"
Visort::Visort(Visort &ob,int vs[], int v)
{
for(int j = 0; j < v; j++)
{
ToSort[j] = vs[j];
cout << ToSort[j] << ' ';
}
Filtervisort(Visort &ob, 31);
}
///////////////////////////////////////////////////////////////
// mainsort.cpp
#include "vsort.h"
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;
}
Best regards,
Jeff
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]