No, but I sorting function is a good idea for the myVector class (although the STL sort would probably work). I was just doing the algorithm for kicks and just started myVector yesterday. I got it pretty much finished:Matthew wrote:Is this why you wanted to know a sorting algorithm?
myVector.h
Code: Select all
class myVector
{
public:
myVector(){itsLength = 0; pVector = new int[itsLength];} //Constructor sets length and creates a vector
~myVector(){clean();} //deletes pointers
int& operator [] (int location) //allows for the [] subscript
{
if (itsLength > 0)
return pVector[location];
}
void operator = (myVector& myVec) //allows to copy vectors to each other
{
itsLength = myVec.len();
delete pVector;
pVector = new int[itsLength];
for (int i = 0; i < itsLength; i++)
pVector[i] = myVec[i];
}
void add_on (int newElement) //adds an element to the end
{
tempVector = new int[itsLength];
for (int i = 0; i < itsLength; i++)
tempVector[i] = pVector[i];
delete pVector;
itsLength++;
pVector = new int[itsLength];
for (int i = 0; i < itsLength; i++)
pVector[i] = tempVector[i];
pVector[itsLength-1] = newElement;
delete tempVector;
}
void add_on (myVector& myVec) //adds another myVector to the end
{
tempVector = new int[itsLength+myVec.itsLength];
for (int i = 0; i < itsLength; i++)
tempVector[i] = pVector[i];
for (int i = itsLength, j = 0; i < itsLength+myVec.itsLength; i++, j++)
tempVector[i] = myVec[j];
itsLength += myVec.len();
pVector = new int[itsLength];
for (int i = 0; i < itsLength; i++)
pVector[i] = tempVector[i];
delete tempVector;
}
void take_off(int location) //takes off the element at int location
{
itsLength--;
tempVector = new int[itsLength];
int j = 0;
for (int i = 0; i < itsLength+1; i++)
{
if (i != location)
{
tempVector[j] = pVector[i];
j++;
}
}
delete pVector;
pVector = new int[itsLength];
for (int i = 0; i < itsLength; i++)
pVector[i] = tempVector[i];
delete tempVector;
}
void take_off() //takes off the element on the end
{
itsLength--;
tempVector = new int[itsLength];
int j = 0;
for (int i = 0; i < itsLength+1; i++)
{
if (i != itsLength+1)
{
tempVector[j] = pVector[i];
j++;
}
}
delete pVector;
pVector = new int[itsLength];
for (int i = 0; i < itsLength; i++)
pVector[i] = tempVector[i];
delete tempVector;
}
void wipe(int start, int finish) //deletes everything from start() to finish()
{
for (int i = start; i < finish+1; i++)
take_off(start);
}
void push_in(int location, int newElement) //Adds an element to a specified location
{
tempVector = new int[itsLength];
for (int i = 0; i < itsLength; i++)
tempVector[i] = pVector[i];
delete pVector;
itsLength++;
int test = itsLength;
pVector = new int[itsLength];
for (int i = 0, j = 0; i < test; i++)
{
if (i == location)
pVector[i] = newElement;
else
{
pVector[i] = tempVector[j];
j++;
}
}
delete tempVector;
}
void push_in(int location, myVector& myVec) //Adds another myVector to a specified location
{
tempVector = new int[itsLength];
for (int i = 0; i < itsLength; i++)
tempVector[i] = pVector[i];
delete pVector;
itsLength+=myVec.itsLength;
int test = itsLength;
pVector = new int[itsLength];
int perm, temp, i;
for (int j = 0, i = 0; i < test; i++)
{
if (i == location)
{
for (perm = location, temp = 0; temp < myVec.itsLength; temp++, perm++)
pVector[perm] = myVec[temp];
i = perm-1;
}
else
{
pVector[i] = tempVector[j];
j++;
}
}
delete tempVector;
}
void exchange(myVector& myOther) //Exchanges two myVectors
{
if (itsLength > myOther.len())
{
tempVector = new int[myOther.len()];
for (int i = 0; i < myOther.len(); i++)
tempVector[i] = myOther[i];
int itsNewLen = itsLength-(itsLength-myOther.len());
myOther.itsLength+=(itsLength-myOther.len());
for (int i = 0; i < myOther.len(); i++)
myOther[i] = pVector[i];
itsLength = itsNewLen;
for (int i = 0; i < itsLength; i++)
pVector[i] = tempVector[i];
}
else
{
tempVector = new int[itsLength];
for (int i = 0; i < itsLength; i++)
tempVector[i] = pVector[i];
int newLen = myOther.len()-(myOther.len()-itsLength);
itsLength+=(myOther.len()-itsLength);
for (int i = 0; i < itsLength; i++)
pVector[i] = myOther[i];
myOther.itsLength = newLen;
for (int i = 0; i < myOther.len(); i++)
myOther[i] = tempVector[i];
}
}
void clean() //deletes and restarts everything
{
delete pVector;
delete tempVector;
itsLength = 0;
}
//SOME SMALL INLINE FUNCTIONS
int start() {return 0;}
int finish() {return itsLength;} //Alternative iterator to len()
int len() {return itsLength;}
private:
int itsLength;
int *pVector;
int *tempVector;
};
Code: Select all
#include <iostream>
#include "myVector.h"
int main()
{
using namespace std;
myVector myVec, myOther;
cout << "myVec is " << sizeof(myVec) << " bytes\n\n";
myVec.add_on(4);
myVec.add_on(6);
myVec.add_on(12);
myOther.add_on(10);
myOther.add_on(20);
myOther.add_on(30);
cout << "add_on(int) Test: myVec length: " << myVec.len() << "\n";
for (int i = 0; i < myVec.len(); i++)
cout << myVec[i] << ", ";
myVec.add_on(myOther);
cout << "\n\nadd_on(myVector) Test: myVec length: " << myVec.len() << "\n";
for (int i = 0; i < myVec.len(); i++)
cout << myVec[i] << ", ";
myVec.take_off(0);
cout << "\n\ntake_off(0) Test: myVec length: " << myVec.len() << "\n";
for (int i = 0; i < myVec.len(); i++)
cout << myVec[i] << ", ";
myVec.take_off();
cout << "\n\ntake_off Test: myVec length: " << myVec.len() << "\n";
for (int i = 0; i < myVec.len(); i++)
cout << myVec[i] << ", ";
myVec.clean();
cout << "\n\nclean() Test: myVec length: " << myVec.len() << "\n";
myVec = myOther;
cout << "\n\n= operator Test: myVec length: " << myVec.len() << "\n";
for (int i = 0; i < myVec.len(); i++)
cout << myVec[i] << ", ";
myVec.wipe(0, 1);
cout << "\n\nwipe(0,1) Test: myVec length: " << myVec.len() << "\n";
for (int i = 0; i < myVec.len(); i++)
cout << myVec[i] << ", ";
myVec.add_on(22);
myVec.add_on(12);
cout << "\n\nAdded 22 and 12 to the end\n";
myVec.push_in(0, 28);
cout << "push_in(0, 28) Test: myVec length: " << myVec.len() << "\n";
for (int i = 0; i < myVec.len(); i++)
cout << myVec[i] << ", ";
myVec.push_in(2, myOther);
cout << "\n\npush_in(2, myOther) Test: myVec length: " << myVec.len() << "\n";
for (int i = 0; i < myVec.len(); i++)
cout << myVec[i] << ", ";
myVec.exchange(myOther);
cout << "\n\nmyVec.exchange(myOther) Test: myVec length: " << myVec.len() << "\nmyVec: ";
for (int i = 0; i < myVec.len(); i++)
cout << myVec[i] << ", ";
cout << "\nmyOther: ";
for (int i = 0; i < myOther.len(); i++)
cout << myOther[i] << ", ";
cout << "\n\nExhange again...\nmyVec: ";
myVec.exchange(myOther);
for (int i = 0; i < myVec.len(); i++)
cout << myVec[i] << ", ";
cout << "\nmyOther: ";
for (int i = 0; i < myOther.len(); i++)
cout << myOther[i] << ", ";
cout << "\n";
return 0;
}
