Respuesta :
Answer:
Explanation:
Program is divided into 3 files. stringVector.h , stringVectory.cpp (implementation file) and main.cpp (testing file)
stringVector.h
#include<iostream>
using namespace std;
class stringVector {
private:
string * data;
unsigned length;
unsigned allocated_length;
public:
stringVector();
virtual ~stringVector();
unsigned size();
unsigned capacity();
void reserve(unsigned new_size);
bool empty();
void append(string new_data);
void swap(unsigned pos1, unsigned pos2);
stringVector &operator = (stringVector const &rhs);
string& operator[](unsigned position);
void sort();
};
stringVector.cpp
#include<iostream>
#include<stdexcept>
#include"stringVector.h"
stringVector::stringVector()
{
data = NULL;
length = 0;
allocated_length = 0;
}
stringVector::~stringVector()
{
delete [] data;
}
unsigned stringVector::size()
{
return length;
}
unsigned stringVector::capacity()
{
return allocated_length;
}
void stringVector::reserve(unsigned new_size)
{
string *temp = new string[new_size]; // Create a new array
/*Copy the contents of the array*/
for(int i =0; i < new_size; i++){
if(i < length){
temp[i] = data[i];
}
else
break;
}
delete []data ;// Delete previous array
data = temp;
allocated_length = new_size;
if(length > new_size){
length = new_size;
}
}
bool stringVector::empty(){
return (length == 0) ? true : false;
}
void stringVector::append(string new_data)
{
string *temp = NULL;
if(length == allocated_length){
if(allocated_length == 0){
data = new string[10];
allocated_length = 10;
}
else{
temp = new string[2 * allocated_length]; // Create a new array with double the size
for(int i = 0; i < length ; i++){
temp[i] = data[i];
}
allocated_length = 2 * allocated_length;
if(data != NULL)
delete []data;
data = temp;
}
}
data[length] = new_data;
length++;
}
void stringVector::swap(unsigned pos1, unsigned pos2)
{
string str;
if((pos1 >= length) || (pos2 >= length)){
cout << "Index Out of bounds" << endl;
return;
}
str = data[pos1];
data[pos1] = data[pos2];
data[pos2] = str;
}
stringVector& stringVector::operator = (stringVector const &rhs)
{
delete [] data;
length = rhs.length;
allocated_length = rhs.allocated_length;
this->data = new string[allocated_length];
for(int i=0 ; i < length; i++)
this->data[i] = rhs.data[i];
}
string& stringVector::operator[](unsigned position)
{
if(position > length){
throw std::out_of_range("Position out of range");
}
return data[position - 1];
}
void stringVector::sort()
{
string str;
for(int i= (length - 1) ; i > 0; i--){
for(int j = 0; j < i; j++){
if(data[j].compare(data[j+1]) > 0){
//Swap
str = data[j];
data[j] = data[j+1];
data[j+1] = str;
}
}
}
}
main.cpp
#include<iostream>
#include<stdexcept>
#include"stringVector.h"
void printVector(stringVector &);
int main()
{
stringVector vector;
string str1("California");
string str2("Alabama");
string str3("Oklahoma");
string str4("Texas");
vector.append(str1);
vector.append(str2);
vector.append(str3);
vector.append(str4);
cout << vector.size() << " - " << vector.capacity() << endl;
printVector(vector);
cout << endl;
vector.reserve(3);
cout << vector.size() << " - " << vector.capacity() << endl;
printVector(vector);
cout << endl;
vector.append(str4);
cout << vector.size() << " - " << vector.capacity() << endl;
printVector(vector);
cout << endl << "Copied vector : " << endl;
stringVector vector2 = vector;
printVector(vector2);
cout << endl;
vector.sort();
cout << "Vector 1 after sorting" << endl;
printVector(vector);
return 0;
}
void printVector(stringVector &vec)
{
cout << "Vector : ";
for(int i = 1 ; i <= vec.size(); i++){
cout << vec[i] << " ";
}
cout << endl;
}
OUTPUT:
[oracle@localhost cppProg3]$ ./main
4 - 10
Vector: California Alabama Oklahoma Texas
3 -3
Vector: California Alabama Oklahoma
4 - 6
Vector: California Alabama Oklahoma Texas
Copied Vector:
Vector: California Alabama Oklahoma Texas
Vector 1 after sorting
Vector: Alabama California Oklahoma Texas