Commit b407f0d2 by Xinfu L

Homework-17

parent 6eacf24a
File added
#include<iostream>
#include<array>
#include<list>
#include<iterator>
#include<type_traits>
using namespace std;
/*
template<class T, enable_if_t<is_arithmetic_v<T>> * = nullptr>
T
foo(T t) {
return t*t;
}
int main() {
int a = 10;
cout << foo(a) << endl;
return 0;
}
*/
template<typename T, enable_if_t<is_same_v<typename iterator_traits<T>::iterator_category, random_access_iterator_tag>> * = nullptr>
void
mySort(T beg, T end) {
cout << "Sort->random_access_iterator" << endl;
}
template<typename T, enable_if_t<is_same_v<typename iterator_traits<T>::iterator_category, forward_iterator_tag>> * = nullptr>
void
mySort(T beg, T end) {
cout << "Sort->forward_iterator" << endl;
}
template<typename T, enable_if_t<is_same_v<typename iterator_traits<T>::iterator_category, bidirectional_iterator_tag>> * = nullptr>
void
mySort(T beg, T end) {
cout << "Sort->bidirectional_iterator" << endl;
}
int main() {
array<int, 3> a = {3, 2, 1};
list<int> b = {3, 2, 1};
mySort(a.begin(), a.end());
mySort(b.begin(), b.end());
return 0;
}
\ No newline at end of file
#include<boost/function_output_iterator.hpp>
#include<iostream>
#include<string>
#include<vector>
template<typename T, typename t>
class ostream_joiner {
public:
ostream_joiner(T& s, t c)
: m_str(&s), delim(c) {}
void operator()(const T& x) {
if(!is_first) {
std::cout << delim << x;
}
else {
std::cout << x;
is_first = false;
}
}
private:
T* m_str;
t delim;
bool is_first = true;
};
int main(int, char*[])
{
std::vector<std::string> x1;
x1.push_back("hello");
x1.push_back("world");
std::string s1 = "";
std::string c1 = ",";
ostream_joiner o1(s1, c1);
std::copy(x1.begin(), x1.end(),
boost::make_function_output_iterator(o1));
std::cout << std::endl;
std::vector<int> x2;
x2.push_back(2);
x2.push_back(3);
int s2;
std::string c2 = ":";
ostream_joiner o2(s2, c2);
std::copy(x2.begin(), x2.end(),
boost::make_function_output_iterator(o2));
std::cout << std::endl;
return 0;
}
\ No newline at end of file
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
class student {
public:
student(string n, int a): name(n), age(a) {}
string name;
int age;
};
int main() {
int byte = 255;
double x = 2.3;;
cout << to_string(byte) << endl;
// cout << to_string(hex, uppercase, byte) << endl;
cout << to_string(0 - 0) << endl;
cout << to_string(false) << endl;
cout << setw(10) << to_string(x) << endl;
student s1("Einstein", 30);
cout << s1.name << " " << s1.age << endl;
// cout << to_string(s1) << endl;
/*
I tried both std::to_string and generic to_string. From my limited experience, I think std::to_string is quite useful when we convert a val(int/double) to string representation. The generic to_string function to me is a combination of std::to_string and std::cout. In my opinion, for std::to_string, we can not trace back to the input type. For example std::to_string(0 - 0) and std::to_string(false) both have the same output "0". But the generic to_string, we have this ability to trace back to the input type and I hope this is an advantage in some case.
*/
return 0;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment