zoukankan      html  css  js  c++  java
  • C++ 简单代码,语法参照

    遍历进程:

    #include <windows.h>                                                                                                      
    #include <TCHAR.h>                                                                                                        
    #include <tlhelp32.h>                                                                                                     
    #include <locale.h>                                                                                                       
    #include <stdio.h>                                                                                                        
                                                                                                                              
    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR szCmdLine,int nCmdShow)                              
    {                                                                                                                         
        AllocConsole();//分配控制台                                                                                           
        freopen("CONOUT{1}quot.txt", "w", stdout);                                                                                    
                                                                                                                              
        PROCESSENTRY32 pe32;                                                                                                  
        pe32.dwSize=sizeof(pe32);                                                                                             
        HANDLE hProcessSnap=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);                                                   
        BOOL bMore=Process32First(hProcessSnap,&pe32);                                                                        
        setlocale(LC_ALL, "chs");                                                                                             
        while(bMore)                                                                                                          
        {                                                                                                                     
            _tprintf(TEXT("进程名称:%s
    "),pe32.szExeFile);                                                                   
            _tprintf(TEXT("进程ID:%u 
    
    "),pe32.th32ProcessID);                                                              
            bMore=Process32Next(hProcessSnap,&pe32);                                                                          
        }                                                                                                                     
                                                                                                                              
        CloseHandle(hProcessSnap);                                                                                            
                                                                                                                              
                                                                                                                              
        Sleep(3000);  //睡眠3秒钟                                                                                             
        FreeConsole();  //关闭释放控制台                                                                                      
        return 0;                                                                                                             
    }    
    View Code

    类的构造析构顺序:

    #include <iostream>
    
    using namespace std;
    
    class A {
    public:
        A(){cout<<"A is created!"<<endl;}
        ~A(){cout<<"A is destroyed!"<<endl;system("pause");}
        void func1() {
            cout << "A::func1" << endl;
        }
        void func2() {
            func1();
            cout << "A::func2" << endl;
        }
        void func3() {
            cout << "A::func3" << endl;
        }
    };
    class B: public A {
    public:
        B(){cout<<"B is created!"<<endl;}
        ~B(){cout<<"B is destroyed!"<<endl;system("pause");}
        void func1() {
            cout << "B::func1" << endl;
        }
        void func3() {
            //A::func3();
            cout << "B::func3" << endl;
            //func2();
        }
    };
    int main() {
        B b;
    
        b.func1();
        b.func2();
        b.func3();
        system("pause");
    }
    View Code

    单目运算符重载:

     1 #include<iostream>
     2 using namespace std;
     3 class myTime
     4 {
     5     int minute;
     6     int second;
     7 public:
     8     myTime(){minute=second=0;};
     9     myTime(int m,int s)
    10     {minute=m;second=s;}
    11     void operator++();
    12     void display()
    13     {cout<<"minute "<<minute<<" second "<<second<<endl;};      
    14 };
    15 void myTime::operator++()
    16 {
    17     second++;
    18     while(second>=60)
    19     {
    20         minute+=second/60;
    21         second=second%60;            
    22     }    
    23     return;                
    24 }
    25 int main()
    26 {
    27     myTime o;
    28     int n=65;
    29     while(n--)
    30     {
    31        o.display();
    32        ++o;
    33     }
    34     system("pause");
    35 }
    View Code

    双目运算符重载:

    #include<iostream>
    using namespace std; 
    class myString 
    {
      private:
              char *p;
      public:
      myString(){p=NULL;}
      myString(char* str) {p=str;};
      
      void display();    
      friend bool operator > (myString s1,myString s2);
    
    };
    bool operator > (myString s1,myString s2)
    {
           if(strcmp(s1.p,s2.p)>0)
               return true;
           else
               return false;       
      };
    void myString::display()
    {
        cout<<p<<endl;     
    }
    int main()
    {
        myString a;
        myString b("xiaoqiang"); 
        myString c("xiaohong");
        cout<<(b>c)<<endl;
        //a.display();   
        b.display();   
        c.display();   
        system("pause"); 
    }
    View Code

    模板类试用:

    // xy.h
    template<int Base, int Exponent>
    class XY
    {
        public:
        enum { result_ = Base * XY<Base, Exponent-1>::result_ };
    };
    template<int Base>
    class XY<Base, 0> 
    {
        public:
        enum { result_ = 1 };
    };
    
    //main.cpp
    #include <iostream>
    #include "xy.h"
    using namespace std;
    
    int main(int argc, char *argv[])
    {
     
        cout << "X^Y<5, 4>::result_ = " << XY<5,4>::result_;
        system("PAUSE");
         return EXIT_SUCCESS;
    }
    View Code
  • 相关阅读:
    Redis5设计与源码分析 (第17章 HyperLogLog相关命令的实现)
    Redis5设计与源码分析 (第16章 GEO相关命令)
    ES5和ES6函数的this指向
    vue响应式原理 (响应式并不等于数据双向绑定,千万不要混淆)
    vue中data为什么是函数而不是对象
    vue-enum 前端常量 枚举工具
    Vue3 写业务逻辑不适合用TS(TypeScript)
    vue-property-decorator vue3 ts 用的装饰器
    github git clone下载加速 && npm install 下载加速
    vue3 如果用ts,导出时候要用 defineComponent,这俩是配对的,为了类型的审查正确
  • 原文地址:https://www.cnblogs.com/huangzq/p/3534385.html
Copyright © 2011-2022 走看看