1) Write a function named problem3 that accepts iterators to the beginning and e
ID: 656503 • Letter: 1
Question
1) Write a function named problem3 that accepts iterators to the beginning and
end of a range and an element to search for. The function returns the number
of steps taken if binary search were used to find the element or determine
that it is not present. Base your code upon the code example from class.
2) Write a template class named problem4 that stores a single value of the
template type that is initialized to 0 in the class' null constructor.
A member function named "save" accepts a single value of the template type
and adds it to the class' variable.
A member function named "spend" takes no arguments and returns the value of
the variable and sets it to 0.
3) 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;
}
Explanation / Answer
##3
#include <iostream>
#include <string>
using namespace std;
class Mytem
{
public:
template<typename T1, typename ...P>
explicit Mytem(T1 t, P... p){init(t,p...);}
operator const string()
{
return os.str();
}
private:
Ostringstream os;
void init()
{
template<typename T, typename ...P>
void init(T1 t, P... p);
}
};
template<typename T1, typename ...P>
void Mytem::init(T1 t, P ...p)
{
os<< t << ' ';
init(p...);
}
int main()
{
int p;
cin >> p;
if (!p) throw runtime_error(Mytem("The entered value is", p, "but non zero value expected",
"error from file:", __FILE__, "line no:",__LINE__))
}
}
## 2
#include <iostream>
#include <string>
template<class T1>
struct HELLO
{
static const T1 , null;
private:
static const char temp[sizeof(T1)];
};
template<class T1>
const char HELLO<T1>::temp[] = {};
template<class T1>
const T& HELLO<T1>::null = *(T1*)temp;
struct ab
{
double data;
ab()=delete;
};
int main()
{
cout << (int)HELLO<char*>::null<<endl;
cout << HELLO<int>::null<<endl;
cout <<HELLO<st>::null.data<<endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.