// : operator
//
// , ,
// operator() ,
// , this
// :
// ::
// .*
// .
// ?:
// :
// &&
// ||
// ,
// &
// :
// =
// []
// ()
// ->
// :
//
// /
// :
//
//
//
//
//
// : ostream
// :
// ostream , ,
// ,
//
// :ostream
// :
// iostream , ,
// ,
// ,
//
//
// ,
//
// , ,
//
// ,
//
// ,
//
//
//
//
//
//
//
//
// ,
// operator++() , ,
// operator++(int) , ,int 2 ,
//
// -> , ->
// * , const
//
// (function object) lambda
// , ,
// lambda
// lambda ,
// eg.
vector words;
stable_sort(words.begin(), words.end(), [](const string &a, const string &b){return a.size() < b.size();})
// :
class Shortstring{
public:
bool operator()(const string &a, const string &b) const {
return a.size() < b.size();
}
};
// :
stable_sort(words.begin(), words.end(), Shortstring())
// , ,
// , , ,
// eg.
vector words;
size_t sz = 0;
cin >> sz;
auto wc = find_if(words.begin(), words.end(), [sz](const string &a){return a.size() >= sz;})
// :
class Sizecomp{
public:
Sizecomp(size_t n):sz(n){}
bool operator(const string &a) const {
return a.size() >= sz;
}
private:
size_t sz;
};
// :
auto wc = find_if(words.begin(), words.end(), Sizecomp(sz));
// functional.h
//
// plus
// minus
// multiplies
// divides
// modulus
// negate
//
// equal_to
// not_equal_to
// greater
// greater_equal
// less
// less_equal
//
// logical_and
// logical_or
// logical_not
// :
// ,
// ,
// lambda,
// bind ,
//
// (function table), , map
//
int add(int a, int b){
return a + b;
}
// lambda
auto mod = [](int a, int b){return a % b;};
//
class divide{
public:
int operator()(int a, int b){
return a / b;
}
};
//
map< string, int(*)(int, int)> binops;
//
binops.insert({"+", add});
// lambda ,
// function , functional.h
function f1 = add;
function f2 = [](int a, int b){return a % b;};
function f3 = divide();
//
map> binops{
{"+", add},
{"-", std::minus()},
{"*", [](int a, int b){return a * b;}},
{"/", divide()},
{"%", mod}
};
//
binops["+"](10,5);
binops["-"](10,5);
binops["*"](10,5);
binops["/"](10,5);
binops["%"](10,5);
// function ,
//
//
// operator type() const;
// type , . ,
// , , , const
class SmallInt{
public:
SmallInt( int i = 0):val(i){
if(i < 0 || i > 255)
throw std::out_of_range("wrong smallint value!");
}
operator int() const {
return val;
}
private:
std::size_t val;
}
SmallInt si;
si = 1; //1 SmallInt , operator=
si + 3; //si int
si + 3.14; //si int, double
istream::cin operator bool() ?
int n = 5;
cin << n; // ,cin bool, int, n .
// ,c++11
class SmallInt{
public:
SmallInt( int i = 0):val(i){
if(i < 0 || i > 255)
throw std::out_of_range("wrong smallint value!");
}
explicit operator int() const {
return val;
}
private:
std::size_t val;
}
si = 1; //1 SmallInt , operator=
si + 3; // ,
static_cast(si) + 3; //
// ,
// if(si + 3) ,
// while, if, for, !, ||, &&, ?:
// ,
// :A B ,B A
// : 2