VECTOR OF CHARACTERS
QUESTION:
CODE:
void charVector:: initializeVector(string S)
{
vector<char>::iterator it=cv.begin();
int i=0;
while(S[i]!='\0')
{
cv.push_back(S[i]);
it++;
i++;
}
}
void charVector:: dupVector()
{
vector<char> d=cv;
vector<char>::iterator it=d.begin();
while(it!=d.end())
{
cv.push_back(*it);
it++;
}
}
void charVector:: dupRevVector()
{
vector<char> d=cv;
vector<char>::iterator it=d.end();
while(it!=d.begin())
{
it--;
cv.push_back(*it);
}
}
void charVector:: print()
{
vector<char>::iterator it=cv.begin();
while(it!=cv.end())
{
cout<<*it;
it++;
}
cout<<"YES";
}
QUESTION:
Design a class charVector that has a character vector as datamember. Provide member functions in the class to createVector, duplicateVector, duplicateRevVector and print. Functions shall be defined as follows:
initializeVector – read a string and create a vector of characters
duplicateVector – Add the content of the vector once at the end. For example if the content of charVector is “bat” then after the function is called the content must “batbat”
duplicateRevVector – Add the content of the vector in reverse at the end. For example if the content of charVector is “bat” then after the function is called the content must “battab”
print – Print content of vector, use iterators for traversal
Use the vector class defined in STL for the implementation. Use [] operator in functions duplicateVector, duplicateRevVector and use iterator in print and initializeVector functions.
UML DIAGRAM:
CODE:
void charVector:: initializeVector(string S)
{
vector<char>::iterator it=cv.begin();
int i=0;
while(S[i]!='\0')
{
cv.push_back(S[i]);
it++;
i++;
}
}
void charVector:: dupVector()
{
vector<char> d=cv;
vector<char>::iterator it=d.begin();
while(it!=d.end())
{
cv.push_back(*it);
it++;
}
}
void charVector:: dupRevVector()
{
vector<char> d=cv;
vector<char>::iterator it=d.end();
while(it!=d.begin())
{
it--;
cv.push_back(*it);
}
}
void charVector:: print()
{
vector<char>::iterator it=cv.begin();
while(it!=cv.end())
{
cout<<*it;
it++;
}
cout<<"YES";
}
No comments:
Post a Comment