C++

迴圈

錯誤
priority_queue< ... > pq
for(int i=0; i<pq.size(); ++i){ ...}

template

1.變數分3種:型別(type),值(value),樣板(template)
*不完全型別 -> 在struct / class 前加上 "template <typename T>"
EX:
template <typename T> struct Bar{
    //.....
    T::A x;                //x is ???
    typename T::B y;// y is type;

}
2. template<calss T> <==> template<typename T>

亂數

2.
#include<ctime>
#include<cstdlib> //rand function, RAND_MAX
並在一開始執行:srand(time(NULL));

structure

struct Node
{
    int freq;   // 符號出現次數
    int leaf;   // 各節點涵蓋的樹葉數量
    int length; // 各節點涵蓋的碼長總和
};

bool operator<(Node n1, Node n2)
{
    if (n1.freq != n2.freq)
        return n1.freq > n2.freq;
    return n1.length > n2.length;
}

取double 最大值

#include<limits>
std::numeric_limits<double>::max()

--------------------------------------------------------------------------------------------------------------------------

API

檔案讀寫

char str[] = "test";
file = fopen("/file location");
fwrite( str,1,sizeof(str),file);
fclose(file); //file 不能為NULL

stdio.h 的函數 fflush() 強制將緩衝區的輸出串流寫到檔案中。

***
f = fopen( ,,,, "w");fscanf(f,"%s",str);//str=?\

隱藏Console

HWND hWnd = GetConsoleWindow();
ShowWindow(hWnd, SW_HIDE);
--------------------------------------------------------------------------------------------------------------------------

技巧

Priority_queue

1.定義structure
2.定義operator
3.push

ex:
struct Node
{
    int freq;   // 符號出現次數
    int leaf;   // 各節點涵蓋的樹葉數量
    int length; // 各節點涵蓋的碼長總和
};
int freq[8] = {4, 2, 1, 2, 3, 1, 2, 1}; // 符號出現次數
bool operator<(Node n1, Node n2)
{
    if (n1.freq != n2.freq)
        return n1.freq > n2.freq;
    return n1.length > n2.length;
}
int main(){
priority_queue<Node> pq;
    for (int i=0; i<8; ++i)
        pq.push((Node){freq[i], 1, 0});
        
    for (int i=0; i<8; ++i){
    Node p = pq.top();
    pq.pop();
    cout << p.freq << " ";
}//1,1,1,2,2
return 0;
}

STL make_heap,pop_heap

#include <iostream>
#include <algorithm>
#include <vector>
 
int main()
{
    std::vector<int> v { 3, 1, 4, 1, 5, 9 };
 
    std::cout << "initially, v: ";
    for (auto i : v) std::cout << i << ' ';
    std::cout << '\n';
 
    std::make_heap(v.begin(), v.end());
 
    std::cout << "after make_heap, v: ";
    for (auto i : v) std::cout << i << ' ';
    std::cout << '\n';
 
    std::pop_heap(v.begin(), v.end());
    auto largest = v.back();
    v.pop_back();
    std::cout << "largest element: " << largest << '\n';
 
    std::cout << "after removing the largest element, v: ";
    for (auto i : v) std::cout << i << ' ';
    std::cout << '\n';
}

#include<iostream>
#include<vector>
#include<queue>
#include<ctime>
#include<cstdlib>
using namespace std;

typedef struct Node{
int d;
}Node,*pNode;
class Comparator {
public:
    bool operator()(const Node *a, const Node *b)
    {
        return (a->d > b->d);
    }
};
int main(){
int N=10;
srand(time(NULL));
priority_queue<Node*,vector<Node*>, Comparator> pq;
vector<Node*> vec;
Node node[N];
for(int i=0; i<N; ++i){
node[i].d = rand() % 100;
cout << node[i].d << endl;
vec.push_back(&node[i]);
}
cout << "------------------" << endl;
make_heap(vec.begin(),vec.end(),Comparator() );
for(int i=0; i<N; ++i){
pop_heap(vec.begin(),vec.end(),Comparator());
Node *p = vec.back();
vec.pop_back();
cout << p->d << endl;
}
return 0;
}

執行命令

system("cmd");
popen("cmd"); //_popen(...) in VS

編譯Detours Version3.0

1.新增環境變數: E:\Tool\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.12.25827\bin\Hostx64\x64
>set path = %path%;E:\Tool\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.12.25827\bin\Hostx64\x64
2.開啟cmd
>cd /d Detours
>"E:\Tool\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat"
>nmake all

留言

這個網誌中的熱門文章

組合語言 Assembly Language

C++ DirectX Draw Triangle