• C++随笔


      1 #include <windows.h>
      2 #include <iostream>
      3 #include <stdio.h>
      4 #include <string>
      5 #include <cstring>
      6 #include <cctype>
      7 
      8 #include <initializer_list>        //函数可变参数模板.和vector类似
      9 #include <cassert>                //assert()的头文件
     10 
     11 //STL
     12 #include <stack>    //堆栈
     13 #include <map>        //字典
     14 #include <list>        //链表
     15 #include <vector>    //数组
     16 #include <queue>    //队列
     17 #include <deque>    //动态数组
     18 
     19 
     20 using namespace std;
     21 
     22 typedef struct listNode                    //节点对象
     23 {
     24     //1.数据域
     25     int data;
     26 
     27 
     28     //2.链接域 or 指针域
     29     struct listNode *link;
     30 }node;
     31 
     32 
     33 node *create3()//创建3个节点的链表
     34 {
     35     node *first, *second, *third;
     36 
     37     first = (node *)malloc(sizeof(node));//给第1个节点分配内存空间,并且指针转换
     38     second = (node *)malloc(sizeof(node));
     39     third = (node *)malloc(sizeof(node));
     40 
     41     third->link = NULL;//最后1个元素的地址应该是null
     42     third->data = 30;
     43     second->link = third;
     44     second->data = 20;
     45     first->link = second;
     46     first->data = 10;
     47 
     48 
     49     return first;
     50 }
     51 
     52 
     53 //测试编译器的一些函数相关东西
     54 void CompilerTest()
     55 {
     56 #ifndef NDEBUG
     57     cerr << __FUNCDNAME__ << endl;
     58     cerr << __FUNCTION__ << endl;
     59     cerr << __FILE__ << endl;//文件路径
     60     cerr << __LINE__ << endl;
     61     cerr << __TIME__ << endl;
     62     cerr << __TIMESTAMP__ << endl;
     63     cerr << __DATE__ << endl;
     64 
     65     cout << "这些信息通常都可以作为log的辅助信息" << endl;
     66 #endif
     67 }
     68 
     69 
     70 
     71 void show_Test()
     72 {
     73     node *list = create3();
     74 
     75     cout << list->link->link->data << endl;
     76 
     77 
     78     while (NULL != list)
     79     {
     80         cout << list->data << endl;
     81         list = list->link;
     82     }
     83 
     84     delete list;
     85     list = NULL;
     86 }
     87 
     88 //初始化次数
     89 size_t count_calls()
     90 {
     91     static size_t ctr = 0;//不再经过 这一句
     92     return ++ctr;
     93 }
     94 
     95 
     96 
     97 //可变形参
     98 void error_msg(initializer_list<int> ilist)
     99 {
    100     auto beg = ilist.begin();
    101     auto end = ilist.end();
    102 
    103     while (beg != end)
    104     {
    105         cout << *beg << "  "<<endl;
    106         ++beg;
    107     }
    108 }
    109 
    110 
    111 //不可以返回函数内部的定义的  引用和指针...,卧槽,可以的哦 流弊了
    112 int& func()
    113 {
    114 
    115     int a = 10;
    116     int &r = a;
    117     return r;
    118 
    119 }
    120 
    121 //返回类型是左值的函数可以写在 = 的左边
    122 char& get_val(string &str, string::size_type ix)
    123 {
    124     return str[ix];
    125 }
    126 
    127 
    128 //测试 assert()函数
    129 void AssertTest()
    130 {
    131     int x = 10;
    132     assert(!x);
    133 
    134     return;
    135 }
    136 
    137 //范围for,与引用
    138 void ForRange()
    139 {
    140     vector<int> v;
    141     v.push_back(0);
    142     v.push_back(0);
    143     v.push_back(0);
    144     v.push_back(0);
    145 
    146     int i = 0;
    147     for (auto &r : v)
    148     {
    149         r = 1;
    150         cout << v[i] << endl;
    151     }
    152 }
    153 
    154 //getline
    155 void GetLineTest()
    156 {
    157     string line;
    158     while (getline(cin, line))
    159     {
    160         cout << line.size() << endl;
    161         cout << line.length() << endl;
    162 
    163         cout << line << endl;
    164     }
    165     getline(cin, line);
    166     cout << line << endl;
    167 
    168 }
    169 
    170 
    171 
    172 struct Student
    173 {
    174     Student() = default;
    175 
    176 
    177 };
    178 
    179 class ObjectSize
    180 {
    181 public:
    182     int a;
    183     int b;
    184     int c;
    185     double d;
    186 
    187 
    188 };
    189 
    190 class Screen
    191 {
    192 public:
    193     typedef std::string::size_type pos;
    194     Screen() = default;
    195 
    196     Screen(pos ht, pos wd, char c) : height(ht), width(wd)
    197     {}
    198 
    199 private:
    200     pos cursor = 0;    //暂时不明白这个地方为啥有值了呢????????????????????????????
    201     pos height = 0;
    202     pos width = 0;
    203     string contents;
    204 
    205 };
    206 
    207 //string size()  length()
    208 void StringVar()
    209 {
    210     string a = "abc";
    211     cout << a.length() << endl;
    212     
    213 
    214     string str("123456");
    215     auto len = str.size();
    216     cout << len << endl;
    217 
    218 
    219 
    220 
    221     string str2 = "panzhengming";
    222     for (auto c:str2)
    223     {
    224         cout << c << "  ";
    225     }
    226 }
    227 
    228 //迭代器会移动的
    229 void MoveIter()
    230 {
    231     const int sz = 10;
    232     int arr[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    233     auto pBegin = begin(arr);
    234     auto pEnd = end(arr);
    235 
    236     while (pBegin != pEnd)
    237     {
    238         cout << *pBegin << " ";
    239         pBegin++;
    240     }
    241 
    242     cout << endl;
    243     pBegin = begin(arr);
    244     pEnd = end(arr);
    245     while (pBegin != pEnd)
    246     {
    247         *pBegin = 0;
    248 
    249         pBegin++;
    250     }
    251 
    252     pBegin = begin(arr);
    253     pEnd = end(arr);
    254     while (pBegin != pEnd)
    255     {
    256         cout << *pBegin << " ";
    257         pBegin++;
    258     }
    259 }
    260 
    261 //迭代器的用法
    262 void Iter1()
    263 {
    264     int arr[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    265     vector<int> ivec(begin(arr), end(arr));
    266     auto Iter = ivec.begin();
    267     while (Iter != ivec.end())
    268     {
    269         cout << *Iter << " ";
    270 
    271         Iter++;
    272     }
    273 }
    274 
    275 //for语句的第1个表达式只执行1次初始化!!!
    276 void ForExp1()
    277 {
    278     vector<int> iver;
    279     iver.push_back(0);
    280     iver.push_back(0);
    281     iver.push_back(0);
    282     iver.push_back(0);
    283 
    284     for (int i = 0, sz = iver.size(); i != sz; i++)                
    285     {
    286         iver.push_back(iver[i]);
    287         cout << "hello" << endl;
    288         cout << sz << endl;
    289     }
    290     cout << iver.size() << endl;
    291 }
    292 
    293 //位操作
    294 void BitTest()
    295 {
    296     int bits = 0233;
    297     cout << bits << endl;
    298     bits << 8;
    299     cout << bits << endl;
    300     bits >> 3;
    301     cout << bits << endl;
    302     bits = 0227;
    303     ~bits;
    304     cout << bits << endl;
    305 
    306 
    307 }
    308 
    309 //
    310 void ForTest2()
    311 {
    312     for (size_t i = 0; i != 10; ++i)
    313     {
    314         cout << count_calls() << endl;
    315     }
    316 }
    317 
    318 //函数返回值作为左值
    319 void FuncValueAsLeft()
    320 {
    321     string s = "hello world";
    322     get_val(s, 0) = 'a';                //函数返回左值
    323     cout << s << endl;
    324 }
    325 
    326 
    327 
    328 //指针转换
    329 void CastPoint()
    330 {
    331     //----------------------------------------------------------------1
    332     //double dval;
    333     //int ival;
    334     //int *p;
    335     //dval = p = 0;
    336     //----------------------------------------------------------------2
    337 
    338     //double dval = 10.1;    
    339     //void *p = &dval;
    340     //cout << &dval << "  " << p << endl;
    341     ////cout << *p << endl; 错误
    342     //double *dp = static_cast<double *>(p);
    343     //cout << dp << "  " << *dp << endl;
    344 
    345     //----------------------------------------------------------------3
    346     //int i = 110;
    347     //int *ip = &i;
    348     //char *pc = (char *)ip;
    349     //cout << ip << "  " << *pc << endl;
    350 
    351     //----------------------------------------------------------------4
    352     //int i = 10;
    353     //{
    354     //    int *p = &i;
    355     //    cout << *p << "1111"<<endl;
    356     //}
    357     //cout << *p << endl;
    358 }
    359 
    360 //检查对象大小
    361 void TestObjectSize()
    362 {
    363     cout << sizeof(ObjectSize) << endl;//内存空间大小
    364     ObjectSize tempObj;
    365     cout << sizeof(tempObj) << endl;
    366 }
    367 
    368 //const 练习
    369 void ConstTest()
    370 {
    371     //变量是可读可写的
    372     //常量是只读不可写的
    373     //const赋值时,左边的读写权限要 <= 右边的读写权限
    374 
    375     int x = 3;
    376     int y = 4;
    377 
    378     int const *p = &x;
    379     cout << *p << endl;
    380     x = 5;
    381     cout << *p << endl;
    382 
    383     //*p = 1; 错误 *p是常量
    384 
    385     int *const p2 = &x;
    386     cout << x << "," << *p2 << "," << p << endl;
    387     *p2 = 11;
    388     cout << x << "," << *p2 << "," << p << endl;
    389     //p2 = &y;  错误 p2是常量
    390 
    391 
    392     cout << "-------------------------" << endl;
    393     int b = 50;
    394     const int *p3 = &b;
    395     b = 39;
    396     cout << *p3 << endl;
    397     //*p3 = 100;
    398 
    399     cout << "-------------------------" << endl;
    400     int ii = 0;
    401     const int ci = 42;
    402     ii = ci;
    403     //ci = ii;
    404     cout << ii << "," << ci << endl;
    405 
    406     /*
    407         网上查的结果
    408         我理解的const是一种约定,是说我用了const就不会改变变量的值了,而其他人乱动我也没办法。
    409         我是不是可以简单理解为,顶层是指 自己 不变,底层是指 所指向的 对象不变
    410 
    411         1, 只有在使用指针的时候我们才说const分为 顶层(top)const 和 底层(bottom)const.
    412         2, 右值引用你还用const你有毒吧.
    413         3, 如果是两个指针之间相互拷贝其 底层(bottom)const 起到很关键的作用.
    414 
    415         https://my.oschina.net/SHIHUAMarryMe/blog/595409
    416     */
    417 }
    418 
    419 
    420 //stack 练习堆栈.被称为是 专业程序员使用的数据结构
    421 void StackTest()
    422 {
    423     stack<int, deque<int>>        a;
    424     stack<int, vector<int>>        b;
    425     stack<int, list<int>>        c;
    426     stack<int>                    d;
    427 
    428     d.push(25);
    429     d.push(10);
    430     d.push(1);
    431     d.push(5);
    432 
    433     cout << "一共有" << d.size() << "个数据" << endl;
    434 
    435     int x = d.top();//只看栈顶个,不删除数据
    436     d.pop();//删除栈顶
    437 
    438     x = d.top();
    439     d.pop();
    440 
    441     x = d.top();
    442 
    443     cout << d.size();
    444 
    445 
    446     cout << "----------------------" << endl;
    447 }
    448 
    449 //队列 练习
    450 void QueueTest()
    451 {
    452     //queue不能使用vector的哦,不方便两端操作
    453     queue<int, deque<int>>  a;
    454     queue<int, list<int>>    b;
    455 
    456     queue<int> q;//默认是 deque<int>
    457 
    458     //队尾插入数据
    459     q.push(10);
    460     q.push(5);
    461     q.push(-1);
    462     q.push(20);
    463 
    464     cout << q.size() << endl;
    465 
    466     cout << "队首数据,但是不删除" << q.front() << endl;
    467     cout << "队尾数据,但是不删除" << q.back() << endl;
    468 
    469     //队首删除
    470     q.pop();
    471     cout << "队首数据,但是不删除" << q.front() << endl;
    472     cout << "队尾数据,但是不删除" << q.back() << endl;
    473 
    474     /*
    475         循环通过 while(q.size() != 0)   或者 while(q.empty() == false)
    476 
    477         stack和queue都没有迭代器,所以不能用Iter
    478         因为stack和queue只允许操作2端的数据,不可以操作中间的数据,所以没有Iter
    479     */
    480 }
    481 
    482 
    483 //STL动态数组 deque
    484 void DequeTest()
    485 {
    486     deque<int> a;
    487 
    488     //从尾部插入
    489     a.push_back(3);
    490     a.push_back(4);
    491     a.push_back(5);
    492 
    493     //从前面插入
    494     a.push_front(2);
    495     a.push_front(1);
    496     a.push_front(0);
    497 
    498     for (size_t nCount = 0; nCount < a.size();++nCount)
    499     {
    500         cout << a[nCount] << ",";
    501     }
    502 
    503 
    504     cout << endl;
    505     a.pop_front();//删除前面
    506     a.pop_back();//删除后面
    507     //for (auto Iter = a.begin(); a.begin() != a.end();++Iter)    //这个地方报错.暂时不知道原因
    508     //{
    509     //    cout << *Iter << ",";
    510     //}
    511     
    512 }
    513 int main()
    514 {
    515     
    516     
    517 
    518 
    519 /*
    520 DequeTest();
    521 
    522 QueueTest();
    523 
    524 StackTest();
    525 
    526 ConstTest();
    527 
    528 CastPoint();
    529 
    530 TestObjectSize();
    531 
    532 error_msg({ 100, 200, 300 });//可变形参 列表
    533 
    534 FuncValueAsLeft();
    535 
    536 int a = func();//返回函数内的局部变量引用 ...  c++ primer认为这是错误的.....那我这里是不是代码写错了
    537 
    538 AssertTest();// assert()函数类似  if()判断,false就弹出警告
    539 
    540 CompilerTest();//log的辅助信息
    541 
    542 show_Test();
    543 
    544 ForTest2()
    545 
    546 ForRange();
    547 
    548 GetLineTest();
    549 
    550 StringVar();
    551 
    552 MoveIter();
    553 
    554 Iter1();
    555 
    556 ForExp1();
    557 
    558 BitTest();
    559 
    560 */
    561 
    562 
    563 
    564     cout << endl;
    565     system("pause");
    566     return 0;
    567 }
  • 相关阅读:
    scrapy之download middleware
    远程采集
    未能加载文件或程序集“Oracle.DataAccess, Version=4.112.2.0, Culture=neutral, PublicKeyTok”
    【转】如何解决C盘根目录无法创建或写入文件
    C#报算术运算导致溢出的错误
    【转】C# String 前面不足位数补零的方法
    【转】C# 使用正则表达式去掉字符串中的数字,或者去掉字符串中的非数字
    【转】Asp.Net页面生命周期
    【转】processOnServer
    【转】oracle的分析函数over
  • 原文地址:https://www.cnblogs.com/Froger/p/7015847.html
Copyright © 2020-2023  润新知