Hello,
In my programming class we have just started looking at template functions and I have been given a simple exercise to do to start off with. I have most of it but its a bit about strings that has me stumped.
I am to write 2 template functions, the first needs to add the integers 5 and 7 and also add the string "pine" to "apple". Here's the code for that
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <cmath>
usingnamespace std;
template <typename T>
T add(T a, T b){
T result=a+b;
return (result);
}
int main(){
int i=5,j=7;
string p("pine"),a("apple");
int sum=add<int>(i,j);
string word=add<string>(p,a);
cout<<sum<<endl;
cout<<word<<endl;
}
This is fine, then we have to write another one which subtracts 7 from 2 and 'subtracts' the string "cone" from the string "pinecone". The subtraction of the integers is easy, the above code would just be modified so that i=2 and j=7 and the + changed to a - but I don't know what to do with the strings, I have tried just straightforwardly subtracting them in the same way I had added them in the above example but this didn't work. Any help would be appreciated.
Thanks,
I've put together this crude bit of code which will delete sections of the string based on the position at which it finds the given string at.
1 2 3 4 5 6 7 8 9 10
#include <iostream>
#include <cmath>
usingnamespace std;
int main(){
string p("pinecone"),a("cone");
int position=p.find(a,0);
string remainder=p.erase(position,4);
std::cout<<remainder<<std::endl;
}
The only way I can think of putting this into my function template is via an IF statement. This is because obviously this code is very different from the simple a-b operation for the integers so to have both of these in the same template would require an IF statement which was implemented if the type T was a string. Is it possible to have the type as the condition in an IF statement? I've never seen that before. So in principle I mean something like
#include <iostream>
#include <cmath>
usingnamespace std;
template <typename T>
T sub(T a, T b){
if(T==string){
int position=p.find(a,0);
T result=p.erase(position,4);
return (result);}
else{
T result=a-b;
return (result);}
}
int main(){
int i=2,j=7;
string p("pinecone"),a("cone");
int diff=sub<int>(i,j);
string word=sub<string>(p,a);
cout<<diff<<endl;
cout<<word<<endl;
}
I realize this would not work but it illustrates what I am getting at. Trying to squeeze a way of subtracting two integers, and erasing portions of a string which match another string all in one general template. Again any comments are appreciated.
#include <iostream>
#include <cmath>
usingnamespace std;
template <class T>
class subtraction{
public:
T diff(T a, T b)
{
T result=a-b;
return (result);
}
};
template<>
class subtraction<string>{
public:
string erase(string a,string b)
{
int position=a.find(b,0);
string remainder=a.erase(position,4);
return (remainder);
}
};
int main(){
subtraction<int> numbs;
subtraction<string> words;
int i=2,j=7;
string p("pinecone"),c("cone");
int sum=numbs.diff(i,j);
string word=words.erase(p,c);
cout<<word<<endl;
cout<<sum<<endl;
}
The output is
pine
-5
If anyone knows of a simpler way please let me know, hope this helps anybody else stuck on a similar problem.