C++11

C++11

#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
#include <tuple>
#include <iomanip>
#include <regex>

using namespace std;
/**
 * @brief Bitset
 */
template <size_t N>
void Bitset(bitset<N> &t)
{
    cout <<t<<endl;                 //print: 000 0000 0000 0000 0000 0000 0000 0001
    bitset<32> bitvec(1U);          //0b: 0000 0000 0000 0000 0000 0000 0000 0001
    bitset<13> bitvec1(0xbeef);     //0b: 1 1110 1110 1111
    bitset<20> bitvec2(0xbeef);     //0b: 0000 1011 1110 1110 1111
    bitset<128> bitvec3(~0ULL);     //0b: 0~63: 1, 64~127: 0

    string str("1111111000000011001101");
    bitset<32> bitvec5(str, 5, 4);  //0b: 1100
    bitset<32> bitvec6(str, str.size()-4);  //0b: 1101(the last four char)
    cout <<bitvec6.any()<<endl;         //print: 1
    cout <<bitvec6.count()<<endl;       //print: 3
    cout <<bitvec6.size()<<endl;        //print: 32
    cout <<bitvec6.all()<<endl;         //print: 0
    cout <<bitvec6.test(10/*position*/)<<endl;  //print: 0 (if the bit is 1 at position than return 1)
    bitvec6.set(10/*position*/, true);
    cout <<bitvec6.test(10/*position*/)<<endl;   //print: 1
    bitvec6.set();                      //set all bits  1
    cout <<bitvec6.all()<<endl;         //print: 1
    bitvec6.reset(10/*position*/);      //reset bit  0 at position
    cout <<bitvec6.test(10)<<endl;      //print: 0
    bitvec6.reset();                    //reset all bits  0
    cout <<bitvec6.none()<<endl;        //print: 1
    bitvec6.flip(10/*position*/);       //change one bit at position
    cout <<bitvec6.any()<<endl;         //print: 1
    bitvec6.reset();
    bitvec6.flip();                     //change all bits
    cout <<bitvec6.count()<<endl;       //print: 32
    cout <<bitvec6[10]<<endl;           //print: 1
    unsigned long UL = bitvec6.to_ulong();
    unsigned long long ULL = bitvec6.to_ullong();
    string sbit = bitvec6.to_string();
    cout <<hex<<(UL)<<endl;             //print: 0b ffffffff
    cout <<hex<<(ULL)<<endl;            //print: 0b ffffffff
    cout <<sbit<<endl;                  //print: "11...11"(32)
    cin >> bitvec6;
    bitvec5 = bitvec6;
    cout <<bitvec5<<endl;
}
/**
 * @brief Regex
 */
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;
    }
}
/**
 * @brief Random
 */
void Random()
{
    static default_random_engine e(time(NULL));
    for(size_t i = 0; i < 10; ++i)
        cout << e() <<" ";
    cout<<endl;

    //random in 0~9
    static uniform_int_distribution<unsigned> u(0, 9);
    for(size_t i=0; i<10; ++i)
        cout << u(e) << " ";
    cout << endl;

    //random in 0~1 double
    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;   //true or false
    cout << b(e) <<endl;   //0 or 1
}

/**
 * @brief Console
 */
void Console()
{
    cout << 20 << endl;         //20
    cout << oct <<20 <<endl;    //24
    cout << hex << 20 <</* hex <<*/  endl; //14
    cout <<  20 << endl;        //14
    cout <<dec<<endl;
    cout <<  20 << endl;        //20

//    double d = 3.1415926;
//    cout << hexfloat <<d << defaultfloat <<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) /*: Son()*/
    {
        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;
};
// function
//typedef  function<bool(int, float)> compare;   or
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 & function & auto
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 & func pointer & using
template <typename T,typename S>
using funcpointer = bool (*)(T, S);
bool myFunc(int x, double y)
{
    cout<<"myFunc"<<endl;
    return x > y;
}

//regex
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 foo(int *p, int len) {
        return;
    }

    std::array<int 4> arr = {1,2,3,4};

    // C 风格接口传参
    // foo(arr, arr.size());           // 非法, 无法隐式转换
    foo(&arr[0], arr.size());
    foo(arr.data(), arr.size());

    // 使用 `std::sort`
    std::sort(arr.begin(), arr.end());
 *
 *
 *
 * */
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);
    //    GetScore(*son);
    Son son2 = (*son);
    Son son1 = son2;
    cout <<"son1.score = "<<son1.GetScore()<<endl;
    cout <<(son1==son2) <<endl;
    delete son;

    //    regexTest();
    string str = {"theworldissobighowshouldigo"};
    cout << "pos = " << str.find("e") <<endl;
    string str1;
    str1.assign(str.c_str(), str.find("e"));
    cout << str1 <<endl;

    //tuple
    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, 50.1, [](int x,float y){return x > y;});
    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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值