C++11
#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
#include <tuple>
#include <iomanip>
#include <regex>
using namespace std;
template <size_t N>
void Bitset(bitset<N> &t)
{
cout <<t<<endl;
bitset<32> bitvec(1U);
bitset<13> bitvec1(0xbeef);
bitset<20> bitvec2(0xbeef);
bitset<128> bitvec3(~0ULL);
string str("1111111000000011001101");
bitset<32> bitvec5(str, 5, 4);
bitset<32> bitvec6(str, str.size()-4);
cout <<bitvec6.any()<<endl;
cout <<bitvec6.count()<<endl;
cout <<bitvec6.size()<<endl;
cout <<bitvec6.all()<<endl;
cout <<bitvec6.test(10)<<endl;
bitvec6.set(10, true);
cout <<bitvec6.test(10)<<endl;
bitvec6.set();
cout <<bitvec6.all()<<endl;
bitvec6.reset(10);
cout <<bitvec6.test(10)<<endl;
bitvec6.reset();
cout <<bitvec6.none()<<endl;
bitvec6.flip(10);
cout <<bitvec6.any()<<endl;
bitvec6.reset();
bitvec6.flip();
cout <<bitvec6.count()<<endl;
cout <<bitvec6[10]<<endl;
unsigned long UL = bitvec6.to_ulong();
unsigned long long ULL = bitvec6.to_ullong();
string sbit = bitvec6.to_string();
cout <<hex<<(UL)<<endl;
cout <<hex<<(ULL)<<endl;
cout <<sbit<<endl;
cin >> bitvec6;
bitvec5 = bitvec6;
cout <<bitvec5<<endl;
}
void Regex()
{
string pattern("[^c]ei");
pattern = "[[:alpha:]]*" + pattern + "[[:alpha:]]*";
try{
regex r(pattern);
smatch results;
string test_str = "receipt friend thief receive";
if(regex_search(test_str, results, r))
cout << results.str() << endl;
}catch(regex_error e)
{
cout << e.what() << "\ncode: " << e.code() << endl;
}
}
void Random()
{
static default_random_engine e(time(NULL));
for(size_t i = 0; i < 10; ++i)
cout << e() <<" ";
cout<<endl;
static uniform_int_distribution<unsigned> u(0, 9);
for(size_t i=0; i<10; ++i)
cout << u(e) << " ";
cout << endl;
static uniform_real_distribution<double> ud(0, 1);
for(size_t i = 0; i< 10; ++i)
cout << ud(e) << " ";
cout<<endl;
bernoulli_distribution b;
cout << boolalpha << b(e) << noboolalpha <<endl;
cout << b(e) <<endl;
}
void Console()
{
cout << 20 << endl;
cout << oct <<20 <<endl;
cout << hex << 20 << endl;
cout << 20 << endl;
cout <<dec<<endl;
cout << 20 << endl;
}
class Father{
public:
Father(){
cout<<"Father()"<<endl;
}
Father(int a){
cout<<"Father(int)"<<endl;
}
Father(int a, int b){
cout << "Father(int ,int)" <<endl;
}
Father(int a,float b,int c){
cout << "Father(int, float, int)" <<endl;
}
};
class Son : Father
{
using Father::Father;
public:
Son()
{
cout<<"Son()"<<endl;
}
Son(int x)
{
score = x;
cout<<"Son(int)"<<endl;
}
Son(int ,int)
{
cout<<"Son(int,int)"<<endl;
}
Son(const Son& s):score(s.score)
{
cout <<" Son(const Son&)" <<endl;
}
bool operator == (const Son& s) const
{
cout <<"bool operator == (const Son& s1, const Son& s2)" <<endl;
return (s.score == this->score);
}
int GetScore() const
{
return score;
}
private:
int score = 0;
};
using compare = function<bool(int, float)> ;
void result(int t, float s, compare func = move([](int x, float y){return x > y;}))
{
cout << t << " > " << s << " ? " << (func(t,s) ? "right!" : "wrong!") << endl;
}
template <typename T, typename S>
auto add(T t, S s) -> decltype(t + s)
{
return t + s;
}
template <typename T, typename S>
void resultAdd(T t, S s)
{
cout << t << " + " << s << " = " << add<T, S>(t ,s) << endl;
}
template <typename T,typename S>
using funcpointer = bool (*)(T, S);
bool myFunc(int x, double y)
{
cout<<"myFunc"<<endl;
return x > y;
}
void regexTest()
{
try{
string fnames[] = {"foo.txt", "bar.txt", "test", "a0.txt", "AAA.txt"};
regex txt_regex("[a-z]+\\.txt");
for(const auto &fname: fnames)
{
cout << fname << ": " << regex_match(fname, txt_regex) << endl;
}
}catch(exception e)
{
}
}
void GetScore(Son& son)
{
cout<<"score = "<<son.GetScore()<<endl;
}
int main()
{
Console();
#if 0
Random();
Regex();
bitset<32> vbit(1UL);
Bitset<32>(vbit);
Son *son = new Son(59);
Son son2 = (*son);
Son son1 = son2;
cout <<"son1.score = "<<son1.GetScore()<<endl;
cout <<(son1==son2) <<endl;
delete son;
string str = {"theworldissobighowshouldigo"};
cout << "pos = " << str.find("e") <<endl;
string str1;
str1.assign(str.c_str(), str.find("e"));
cout << str1 <<endl;
tuple<int, string, float> stu0 =
make_tuple<int, string, float>(20, "Tang Xiao Long", 95.9);
cout << "num :" <<get<0>(stu0)
<< ", name :" << get<1>(stu0)
<< ", score :" << get<2>(stu0) << endl;
int num;
string name;
float score;
tie( num, name, score) = stu0;
cout << "num :" << num
<< ", name :" << name
<< ", score :" << score
<< endl;
vector<tuple<int , string, float>> vtup;
array<int, 4> arr = {4, 2, 3, 4} ;
sort(arr.begin(), arr.end());
for(auto &i : arr)
{
cout << "data = " << i <<endl;
}
array<int , 10> arrb;
int a=0, b=1;
generate(arrb.begin(), arrb.end(),
[&a, &b]{int value = b; b = a + b; a = value; return value;});
for(auto &i : arrb)
cout << "data b = " << i << endl;
vector<int> vec(5,300);
cout << "count = " << count(vec.begin(), vec.end(), 300) << endl;
cout << "count = " << count_if(vec.begin(), vec.end(), [](int i){return i == 300;})
<< endl;
for(auto &i : vec)
{
cout << "data = " << i << endl;
}
Son son(5,5);
Son son0;
Son son1(5);
result(50.1, 20);
result(50, 50.1 , [](int x, float y){return x < y;});
resultAdd(52, 50.1);
funcpointer<int, double> funcTemp;
funcTemp = std::move(&myFunc);
funcTemp(10, 20);
vector<int> arr(5,100);
for(auto &data : arr)
{
cout<<data<<endl;
}
Son son;
cout << add<int, float>(3,5.5) << endl;
int a = 8 , a1 = 9, a2 = 10;
auto b = [=]()mutable -> int {return a;};
auto f = [=,&a]{return a;};
a ++;
cout << f() << endl;
cout << b() << endl;
#endif
return 0;
}