#include <iostream>
#include <string>
#include <deque>
#include <list>
using namespace std;
template <typename T>
bool compareLT(const T &v1, const T &v2)
{
return (v1 < v2);
}
float a = 13.3, b = 38.2;
string c = "Hello", d = "Goodbye";
cout << a << " < " << b << " = " << std::boolalpha << compareLT(a, b) << endl;
cout << c << " < " << d << " = " << std::boolalpha << compareLT(c, d) << endl;
13.3 < 38.2 = true Hello < Goodbye = false
template <class T>
class foo
{
private:
T a;
bool isDefault = true;
public:
foo(){};
foo(T v)
{
a = v;
isDefault = false;
}
T getFoo();
void setFoo(T v)
{
a = v;
isDefault = false;
}
};
template <class T>
T foo<T>::getFoo()
{
if (isDefault) {
cerr << "Error value was not set" << endl;
}
return a;
} // End of getFoo
foo<int> x;
foo<string> y("Hello");
foo<float> z;
x.setFoo(8);
cout << "X is " << x.getFoo() << endl;
cout << "Y is " << y.getFoo() << endl;
cout << "Z is " << z.getFoo() << endl;
X is 8 Y is Hello Z is
Error value was not set
0
template <class T>
class mypair
{
T a, b;
public:
mypair(T first, T second)
{
a = first;
b = second;
}
T getmax();
};
template <class T>
T mypair<T>::getmax()
{
T retval;
retval = a > b ? a : b;
return retval;
}
mypair<int> myint(100, 75);
cout << "Max is " << myint.getmax() << endl;
Max is 100
mypair<string> mystring("fish", "zebra");
cout << "Max is " << mystring.getmax() << endl;
Max is zebra
template<class T>
constexpr T pi = T(3.1415926535897932385L); // variable template
template<class T>
T circular_area(T r) // function template
{
return pi<T> * r * r; // pi<T> is a variable template instantiation
}
float r1 = 3.2;
circular_area(r1)
32.1699f
double r2 = 3.2;
circular_area(r2)
32.169909
int r3 = 3;
circular_area(r3)
27
pi<int>
3
template <typename FUNC>
void do_n_times(FUNC fn, int n) {
for(int i=0; i<n; ++i) fn();
}
void printHello() {
cout << "Hello!" << endl;
}
do_n_times(printHello, 10)
Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello!
bool isOdd(int i) // true if odd, false if even
{
return ((i % 2) == 1);
}
vector <int> li{0,1,2,3,4,5};
cout << *(find_if(li.begin(), li.end(), isOdd));
1
// What is the * for?
find_if(li.begin(), li.end(), isOdd)
@0x55560d6c1d00
// what if li were empty?
vector<int> li2;
find_if(li2.begin(), li2.end(), isOdd) // don't derefence this!
@0x55560e0fbfb0
li2.end() == find_if(li2.begin(), li2.end(), isOdd) // don't derefence this!
true
vector<int> li3{2,4,6,8};
li3.end() == find_if(li3.begin(), li3.end(), isOdd) // don't derefence this!
true
do_n_times([] () { cout << "Hello!" << endl; }, 3)
Hello! Hello! Hello!
int i = 0;
do_n_times([i] () { cout << "Hello! " << i << endl; }, 3)
Hello! 0 Hello! 0 Hello! 0
int i = 0;
do_n_times([i] () { cout << "Hello! " << i++ << endl; }, 3)
input_line_45:3:43: error: cannot assign to a variable captured by copy in a non-mutable lambda do_n_times([i] () { cout << "Hello! " << i++ << endl; }, 3) ~^
Interpreter Error:
int i = 0;
do_n_times([&i] () { cout << "Hello! " << i++ << endl; }, 3)
Hello! 0 Hello! 1 Hello! 2
i
3
vector<int> li4{2,4,6,8,9};
find_if(li4.begin(), li4.end(), [] (int j) { return j % 2 == 1; })
@0x55560f016290
*find_if(li4.begin(), li4.end(), [] () { return true; })
In file included from input_line_5:1: In file included from /home/dakoop/.conda/envs/cling-new/include/xeus/xinterpreter.hpp:13: In file included from /../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/functional:54: In file included from /../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/tuple:39: In file included from /../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/array:39: In file included from /../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/stdexcept:39: In file included from /../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/string:40: In file included from /../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/char_traits.h:39: In file included from /../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_algobase.h:71: /../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/predefined_ops.h:283:16: error: no matching function for call to object of type '(lambda at input_line_51:2:35)' { return bool(_M_pred(*__it)); } ^~~~~~~ /../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_algo.h:120:8: note: in instantiation of function template specialization '__gnu_cxx::__ops::_Iter_pred<(lambda at input_line_51:2:35)>::operator()<__gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > > >' requested here if (__pred(__first)) ^ /../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_algo.h:161:14: note: in instantiation of function template specialization 'std::__find_if<__gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > >, __gnu_cxx::__ops::_Iter_pred<(lambda at input_line_51:2:35)> >' requested here return __find_if(__first, __last, __pred, ^ /../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_algo.h:3969:19: note: in instantiation of function template specialization 'std::__find_if<__gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > >, __gnu_cxx::__ops::_Iter_pred<(lambda at input_line_51:2:35)> >' requested here return std::__find_if(__first, __last, ^ input_line_51:2:3: note: in instantiation of function template specialization 'std::find_if<__gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > >, (lambda at input_line_51:2:35)>' requested here *find_if(li4.begin(), li4.end(), [] () { return true; }) ^ input_line_51:2:35: note: candidate function not viable: requires 0 arguments, but 1 was provided *find_if(li4.begin(), li4.end(), [] () { return true; }) ^ input_line_51:2:35: note: conversion candidate of type 'bool (*)()'
Interpreter Error:
deque<int> mydeque(2, 100);
// deque <int>::iterator it; // but use auto
mydeque.push_front(200);
mydeque.push_front(300);
cout << "mydeque contains: ";
for(auto it3 = mydeque.begin(); it3 != mydeque.end(); ++it3)
cout << ' ' << *it3;
cout << '\n';
mydeque contains: 300 200 100 100
std::list <int> mylist;
for(int i = 1; i <= 5; ++i)
mylist.push_back(i); // 1 2 3 4 5
auto it = mylist.begin(); // std::list <int>::iterator
++it;
mylist.insert(it, 10);
mylist.insert(it, 2, 20);
--it;
std::vector <int> myvector(2, 30);
mylist.insert(it, myvector.begin(), myvector.end());
std::cout << "mylist contains: ";
for(it = mylist.begin(); it != mylist.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
mylist contains: 1 10 20 30 30 20 2 3 4 5
string s = "Hello", s2 = ", World", s3 = ", DeKalb";
s.size()
5
s + s2
"Hello, World"
s2 == s3
input_line_19:2:5: note: use '=' to turn this equality comparison into an assignment s2 == s3 ^~ =
false
s2 = s3;
s + s2
"Hello, DeKalb"