Time of Update: 2018-12-05
#include<iostream>#include<cstdlib>#include<ctime>using namespace std;void swap(int& big, int& small){int temp = big;big = small;small = temp;}int partition(int data[],int left,int right) {int base=data[right];
Time of Update: 2018-12-05
#include<iostream>#include<cstdlib>#include<ctime>using namespace std;void swap(int &small, int& big){int temp;temp = small;small = big;big = temp;}void qSort(int arr[], int left, int right ){int i, j, base;if (left <
Time of Update: 2018-12-05
#include<pic.h>#define uchar unsigned char uchar rtemp,sflag;void SerialInit(void){TXSTA=0x24;//開啟發射使能位、高傳輸速率,TRMT初始值可0可1RCSTA=0x90;//開啟串口、連續接收、SPBRG=0x19;//4M晶振,傳輸速率9600,則SPBRG初值為25RCIE=1;//開啟串口接收完成中斷GIE=1;//開全域中斷PEIE=1;//開第一外設中斷}void
Time of Update: 2018-12-05
首先來觀察一顆最大堆二叉樹 和一個普通序列的映射情況:在序列中從length(LIST)/2+1 -> length(LIST)的元素都對應於二叉樹的葉子節點,同時也是一顆獨立的樹,且最大數就是自己本身。#include<iostream>#define LEFT(i) (i<<2)#define RIGHT(i) ((i<<2)+1)using namespace std;void swap(int& small, int&
Time of Update: 2018-12-05
有些時候我們希望一個類成員除了自己的值以外,還能有一個“未被初始化”的狀態,例如在一個 Server 裡面,我們需要管理 user 數量:class Server{//... int userCount_;//...};userCount_ 當然是初始化為0,但是有時候並不如此簡單,很多資源可能是在第一個 user 使用的時候被分配的,而回收資源的時候,如果僅僅看到 userCount_ 為0,我們壓根就不知道到底是曾經有過 user
Time of Update: 2018-12-05
問題的起因很簡單,我實驗一個簡單的程式不過。這個程式的本意是想檢驗一下 boost.bind 在 1.33 中新加入的比較子,我以為這一點也不難,然而程式的運行結果大出我的意料:#include <iostream>#include <algorithm>#include <map>#include <string>//#include <boost/lambda/lambda.hpp>//#include
Time of Update: 2018-12-05
這段代碼在任何一個成熟的C++編譯器當中都不會通過:class Test{public: char *p; static void Tt() { p = 0; }};因為它在static函數中訪問執行個體成員,而在調用static函數的時候,可能根本就沒有執行個體存在,即便存在,它也不會知道該訪問哪一個執行個體的成員。在VC7.1當中,當然也會出現編譯錯誤。但是,如果你把這個類改成了template,情況就不同了:template <class
Time of Update: 2018-12-05
(看過了這麼多編譯期演算法之後)還記得運行時吧?我們已經在編譯期的天空飛行了好久,現在是時候腳踏實地了。一個有趣的程式終究還是要在運行時幹點什麼的。本章就是關於怎樣穿越C++編譯期和運行時的邊界——這一層“臭氧層”,如果你想要的話——這樣我們的元程式可以真正的使用者面前施展拳腳。在C++中,進行這趟旅程的辦法恐怕是無窮無盡,但是其中有一些更加有用,下面講到的就是最常用的一些技巧。9.1 for_eachSTL裡最簡單的演算法在MPL裡面也應該有,也的確有。先回顧一下:std::for_each
Time of Update: 2018-12-05
#include<iostream>#include<cstdlib>#define M 20using namespace std;static int count_insert = 0;static int count_partion = 0;void swap(int& small, int& big){int temp = small;small = big;big = temp;}/*|簡述下插入排序:||cur = 1 ->
Time of Update: 2018-12-05
從 STL 出現到現在已經這麼多年了,泛型演算法是它的重要組成,也是其中最“看起來很美”的東西之一。然而在真實的程式設計中,它往往成為程式員的心頭一痛,因為一旦要用 for_each ,accumulate 之類的演算法做一些稍微複雜一點的事情,你就會發現自己一個頭變成兩個大。即便是有了 boost::bind 和 boost.lambda 的協助,事情也仍然是撲朔迷離,求助於 comp.lang.c++ 雖然有用,但是又何嘗不是一種無奈。好了,現在我開始收集一些來自 comp.lang.c++
Time of Update: 2018-12-05
#include<reg52.h>#define uchar unsigned charuchar rtemp,sflag;void SerialInit()//11.0592M晶振,傳輸速率9600{TMOD=0x20;//設定定時器1工作方式為方式2TH1=0xfd;TL1=0xfd;TR1=1;//啟動定時器1SM0=0;//串口方式1SM1=1;REN=1;//允許接收PCON=0x00;//關倍頻ES=1;//開串口中斷EA=1;//開總中斷}void
Time of Update: 2018-12-05
#include<time.h>#include<stdio.h>void main(){ /*方法1time_t curTime = time(NULL);char* curDate =
Time of Update: 2018-12-05
開門見山好了,boost 1.33 對於 boost 1.32 的 shared_ptr 和 weak_ptr 有一個不小的改變,然而這個改變如此透明,以至於它甚至於沒有出現在 boost 1.33 的 release notes 中。在 1.32 中,shared_ptr 和 weak_ptr 的引用計數使用鎖來保證安全執行緒,下面一段摘自 boost 1.32 的 sp_counted_base 實現,大家可以在 boost 的 detail/shared_count.hpp 中找到它:
Time of Update: 2018-12-05
Boost.Lambda 的確是一個好東西,用來舉例最多的恐怕就是這個了:using namespace boost::lambda;std::vector<int> v;// init valuesstd::for_each(v.begin(), v.end(), std::cout << _1 << ' ');精緻、優雅、易於理解,下面是一個更漂亮的例子:std::map<int, int> values;// init valuesstd::
Time of Update: 2018-12-05
#include<reg52.h>#define uchar unsigned char#define uint unsigned int#define DELAY 1000void delay(uint z)//1ms{uint x,y;for(x=z;x>0;x--)for(y=111;y>0;y--);}void main(){uchar
Time of Update: 2018-12-05
#include<iom16v.h>#include<macros.h>#define uchar unsigned char#define uint unsigned int#pragma interrupt_handler timer1:9uchar i,temp;void delay(uint x){ uint y,z; for(y=x;y>0;y--) for(z=1111;z>0;z--);}void main(void){ i=0; temp=0
Time of Update: 2018-12-05
#include<iom16v.h>#define uchar unsigned char#define uint unsigned int#define cs_l PORTD&=0xfe//輸出低電平#define wr_l PORTD&=0xfd//輸出低電平void delayms(uint x)//8M晶振下延時1ms{uint y,z;for(y=x;y>0;y--)for(z=1325;z>0;z--);}void main(){uchar
Time of Update: 2018-12-05
功能說明:LCD1602液晶顯示年、月、日、時、分、秒、星期、溫度,三個按鍵控制時間日期調整以及鬧鐘設定。 三個按鍵:1、“校時”鍵,每次按下分別對應調整秒、調整分鐘、調整小時、調整星期、調整日、調整月份、調整年份、調整鬧鐘分鐘、調整鬧鐘小時、退出校時模式;2、“增加/開鬧鐘功能”鍵,校時模式下按下此鍵則數值加一,非校時模式下按下此鍵則顯示出鬧鐘時間並開啟鬧鐘功能(螢幕顯示字母A);3、“減小/關鬧鐘功能”鍵,校時模式下按下此鍵則數值減一,非校時模式下按下此鍵則關閉鬧鐘顯示並關閉鬧鐘功能(螢幕字
Time of Update: 2018-12-05
功能說明:LCD1602液晶顯示年、月、日、時、分、秒、星期、溫度,三個按鍵控制時間日期調整以及鬧鐘設定。 三個按鍵:1、“校時”鍵,每次按下分別對應調整秒、調整分鐘、調整小時、調整星期、調整日、調整月份、調整年份、調整鬧鐘分鐘、調整鬧鐘小時、退出校時模式;2、“增加/開鬧鐘功能”鍵,校時模式下按下此鍵則數值加一,非校時模式下按下此鍵則顯示出鬧鐘時間並開啟鬧鐘功能(螢幕顯示字母A);3、“減小/關鬧鐘功能”鍵,校時模式下按下此鍵則數值減一,非校時模式下按下此鍵則關閉鬧鐘顯示並關閉鬧鐘功能(螢幕字
Time of Update: 2018-12-05
在 STL 中有各種容器,而 STL 演算法允許我們對容器中的元素做各種操作,下面的程式對於每一個當代的 C++ 程式員都應該是輕而易舉:#include <iostream>#include <list>#include <algorithm>#include <string>using namespace std;struct print{ void operator()(const string& _str) {