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.
Input Format
String to be stored in vector1
String to be stored in vector2
Output Format
Print duplicateVector of vector1
Print duplicateRevVector of vector2
C++ code:
void charVector :: initializeVector(string s)
{
for(int i=0;s[i]!='\0';i++)
cv.push_back(s[i]);
}
void charVector :: dupVector()
{
vector < char > new_cv=cv;
for(vector < char > :: iterator i=new_cv.begin();i!=new_cv.end();i++)
cv.push_back(*i);
}
void charVector :: dupRevVector()
{
vector < char > new_cv=cv;
for(vector < char > :: iterator i=new_cv.end();i!=new_cv.begin();i--)
cv.push_back(*(i-1));
}
void charVector :: print()
{
for(vector < char > :: iterator i=cv.begin();i!=cv.end();i++)
cout<<*i;
}
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.
Input Format
String to be stored in vector1
String to be stored in vector2
Output Format
Print duplicateVector of vector1
Print duplicateRevVector of vector2
C++ code:
void charVector :: initializeVector(string s)
{
for(int i=0;s[i]!='\0';i++)
cv.push_back(s[i]);
}
void charVector :: dupVector()
{
vector < char > new_cv=cv;
for(vector < char > :: iterator i=new_cv.begin();i!=new_cv.end();i++)
cv.push_back(*i);
}
void charVector :: dupRevVector()
{
vector < char > new_cv=cv;
for(vector < char > :: iterator i=new_cv.end();i!=new_cv.begin();i--)
cv.push_back(*(i-1));
}
void charVector :: print()
{
for(vector < char > :: iterator i=cv.begin();i!=cv.end();i++)
cout<<*i;
}
0 Comments