1template <class T>
2void swap(T & lhs, T & rhs)
3{
4 T tmp = lhs;
5 lhs = rhs;
6 rhs = tmp;
7}
8
9void main()
10{
11 int a = 6;
12 int b = 42;
13 swap<int>(a, b);
14 printf("a=%d, b=%d\n", a, b);
15
16 // Implicit template parameter deduction
17 double f = 5.5;
18 double g = 42.0;
19 swap(f, g);
20 printf("f=%f, g=%f\n", f, g);
21}
22
23/*
24Output:
25a=42, b=6
26f=42.0, g=5.5
27*/
1#include <string>
2#include <iostream>
3
4using namespace std;
5
6template<typename T>
7void removeSubstrs(basic_string<T>& s,
8 const basic_string<T>& p) {
9 basic_string<T>::size_type n = p.length();
10
11 for (basic_string<T>::size_type i = s.find(p);
12 i != basic_string<T>::npos;
13 i = s.find(p))
14 s.erase(i, n);
15}
16
17int main() {
18 string s = "One fish, two fish, red fish, blue fish";
19 string p = "fish";
20
21 removeSubstrs(s, p);
22
23 cout << s << '\n';
24}
25The basic_string member func