The two sets of output below show the results of successive circular rotations o
ID: 3911426 • Letter: T
Question
The two sets of output below show the results of successive circular rotations of a vector. One set of data is for a vector of integers, and the second is for a vector of strings.
Write two template functions that can be used to rotate and output a vector of a generic type:
void rotateLeft(vector <T>& v)
void output(vector <T> v)
The first function performs a single circular left rotation on a vector, and the second prints out the vector passed to it as parameter. Write a suitable driver program that will allow you to test the two functions by generating output similar to the above. Verify that the program works with vectors whose element types are char, int, double, and string.
1 3 5 7 3 5 7 1 5 7 1 3 7 1 3 5Explanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
//Code
#include<iostream>
#include<vector>
using namespace std;
//template method to rotate a vector to the left
template <class T>
void rotateLeft(vector <T>& v){
int size=v.size();//getting size of vector
if(size>0){
//getting first element
T data=v[0];
//removing first element
v.erase(v.begin());
//pushing first element at the end
v.push_back(data);
}
}
//template method to print a vector contents
template <class T>
void output(vector <T> v){
for(int i=0;i<v.size();i++){
cout<<v[i]<<" ";
}
cout<<endl;
}
int main(){
/* defining vectors of int, char, double and string, testing rotateLeft()
* and output() methods using each vectors
*/
vector<int> intVector;
intVector.push_back(1);
intVector.push_back(3);
intVector.push_back(5);
intVector.push_back(7);
cout<<"Original vector of ints:"<<endl;
output(intVector);
cout<<" Rotating left:"<<endl;
rotateLeft(intVector);
output(intVector);
cout<<" Rotating left:"<<endl;
rotateLeft(intVector);
output(intVector);
cout<<" Rotating left:"<<endl;
rotateLeft(intVector);
output(intVector);
vector<char> charVector;
charVector.push_back('a');
charVector.push_back('b');
charVector.push_back('c');
charVector.push_back('d');
charVector.push_back('e');
cout<<" Original vector of chars:"<<endl;
output(charVector);
cout<<" Rotating left:"<<endl;
rotateLeft(charVector);
output(charVector);
cout<<" Rotating left:"<<endl;
rotateLeft(charVector);
output(charVector);
cout<<" Rotating left:"<<endl;
rotateLeft(charVector);
output(charVector);
vector<double> doubleVector;
doubleVector.push_back(1.5);
doubleVector.push_back(55.99);
doubleVector.push_back(74.3);
doubleVector.push_back(20.99);
doubleVector.push_back(1.33);
cout<<" Original vector of doubles:"<<endl;
output(doubleVector);
cout<<" Rotating left:"<<endl;
rotateLeft(doubleVector);
output(doubleVector);
cout<<" Rotating left:"<<endl;
rotateLeft(doubleVector);
output(doubleVector);
cout<<" Rotating left:"<<endl;
rotateLeft(doubleVector);
output(doubleVector);
vector<string> stringVector;
stringVector.push_back("oliver");
stringVector.push_back("queen");
stringVector.push_back("laurel");
stringVector.push_back("lance");
stringVector.push_back("john");
cout<<" Original vector of strings:"<<endl;
output(stringVector);
cout<<" Rotating left:"<<endl;
rotateLeft(stringVector);
output(stringVector);
cout<<" Rotating left:"<<endl;
rotateLeft(stringVector);
output(stringVector);
cout<<" Rotating left:"<<endl;
rotateLeft(stringVector);
output(stringVector);
}
/*OUTPUT*/
Original vector of ints:
1 3 5 7
Rotating left:
3 5 7 1
Rotating left:
5 7 1 3
Rotating left:
7 1 3 5
Original vector of chars:
a b c d e
Rotating left:
b c d e a
Rotating left:
c d e a b
Rotating left:
d e a b c
Original vector of doubles:
1.5 55.99 74.3 20.99 1.33
Rotating left:
55.99 74.3 20.99 1.33 1.5
Rotating left:
74.3 20.99 1.33 1.5 55.99
Rotating left:
20.99 1.33 1.5 55.99 74.3
Original vector of strings:
oliver queen laurel lance john
Rotating left:
queen laurel lance john oliver
Rotating left:
laurel lance john oliver queen
Rotating left:
lance john oliver queen laurel
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.