I was wondering how to do the following. This will all be in a header file to be
ID: 656512 • Letter: I
Question
I was wondering how to do the following. This will all be in a header file to be called in a .cpp file. They all have to be template functions because the .cpp file will test a few different values for each function. This should be done in C++
======
Write a variadic template for a function named problem5 that joins an arbitrary number of strings together. This function will serve as the terminating case for two strings and your variadic template version will extend that functionality to an arbitrary number of strings. std::string problem5(std::string a, std::string b) {return a+" "+b;}
======
Write a template function named problem6 that accepts a constant integer value as its template argument. Remember, the syntax for this is "template", where N is then a template parameter. The function itself accepts an int as its argument and returns true if the argument is divisible by the template argument and false otherwise.
======
Write a template function named problem7 that accepts iterators to the beginning and end of a range as the first two arguments and a function as the third argument. Sort the elements in the range from beginning and up to but not including end using the function argument instead of the less than operator.
=========================
Here is the code in the .cpp file to test the three functions above.
Explanation / Answer
problem 5 Concatenating arbitrary number/types of arguments into a string using the varidac template.
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
#include <sstream>
#include <iostream>
#include <string>
using namespace std;
class Mystrcat{
public:
template<typename T, typename ...P>
explicit Mystrcat(T t, P... p){init(t,p...);}
operator const string(){return o.str();}
private:
ostringstream o;
void init(){}
template<typename T, typename ...P>
void init(T t, P... p);
};
template<typename T, typename ...P>
void Mystrcat::init(T t, P ...p){
o << t << ' ';
init(p...);
}
int main(){
int x;
cin >> x;
if (!x) throw runtime_error(Mystrcat("The value you entered is", x, "but non zero value expected", "error from file:", __FILE__, "line no:",__LINE__));
}
for problem 7 Returns an iterator pointing to the first element in the range [first,last) which does not compare less than val.
The elements are compared using operator< for the first version, and comp for the second. The elements in the range shall already be sorted according to this same criterion (operator< or comp), or at least partitioned with respect to val.
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
#include <sstream>
#include <iostream>
#include <string>
using namespace std;
class Mystrcat{
public:
template<typename T, typename ...P>
explicit Mystrcat(T t, P... p){init(t,p...);}
operator const string(){return o.str();}
private:
ostringstream o;
void init(){}
template<typename T, typename ...P>
void init(T t, P... p);
};
template<typename T, typename ...P>
void Mystrcat::init(T t, P ...p){
o << t << ' ';
init(p...);
}
int main(){
int x;
cin >> x;
if (!x) throw runtime_error(Mystrcat("The value you entered is", x, "but non zero value expected", "error from file:", __FILE__, "line no:",__LINE__));
}
for problem 7 Returns an iterator pointing to the first element in the range [first,last) which does not compare less than val.
The elements are compared using operator< for the first version, and comp for the second. The elements in the range shall already be sorted according to this same criterion (operator< or comp), or at least partitioned with respect to val.
template <class ForwardIterator, class T> ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last, const T& val) { ForwardIterator it; iterator_traits<ForwardIterator>::difference_type count, step; count = distance(first,last); while (count>0) { it = first; step=count/2; advance (it,step); if (*it<val) { first=++it; count-=step+1; } else count=step; } return first; } Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.