Dealing with strings in a template function

Nov 23, 2013 at 8:37pm
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>
using namespace 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
Nov 23, 2013 at 8:46pm
Subtraction is not overloaded for the std::string class, so it won't work.

It cannot work unless you use one of:

    1 template specialization
    2 overload operator - ( string, string ) yourself

Hope this helps.
Nov 23, 2013 at 9:08pm
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>
using namespace 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cmath>
using namespace 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.

Thanks
Last edited on Nov 23, 2013 at 9:22pm
Nov 23, 2013 at 10:26pm
Hi again,

Played around with class template specialization and this works

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <cmath>
using namespace 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.

Topic archived. No new replies allowed.