Time of Update: 2018-12-03
valarray可能大家都不是很熟悉,下面簡單介紹一下:(下表是常用函數,跟cmath類似)1valarray在valarray標頭檔裡,下面是valarray的構造與輸出#include<iostream>#include<string>#include<valarray>using namespace std;int main(){ int a[]={1,2,3,4,5,6,7,8,9,10}; valarray<int>val(a,10);
Time of Update: 2018-12-03
auto_ptr是這樣一種指標,它是“它所指向的對象”的擁有者,所以,當身為對象擁有者的auto_ptr被摧毀時,該對象也將遭到摧毀。auto_ptr要求一個對象只能有一個擁有者,嚴禁一物二主。設計動機:1擷取一些資源2執行一些動作3釋放所擷取的資源,不用自己調用delete,不用擔心異常。。。的原因,導致記憶體流失的問題了。#include<iostream>#include<memory>#include<string>using namespace
Time of Update: 2018-12-03
不用擔心,你沒有上當,資料計算,分析一系列的變化非常有用,例如分析股票值的變化,所涉及的就是numeric的計算,請看,相信你不再害怕查文檔,不再害怕函數原型分別解說:下面是函數原型,不要被函數原型嚇到了,下面有具體的代碼。1template<class InIt, class T> T accumulate(InIt first, InIt last, T val);template<class InIt, class T, class Pred> T
Time of Update: 2018-12-03
#include<iostream>#include<string>using namespace std;template<typename T>int max(T &a,T &b){ if(a>b){ return 1; }else if(a<b){ return -1; }else{ return 0; }}template<>int max<double>(double
Time of Update: 2018-12-03
可能有很大部分人只能看著書本編程,萬一書沒在身邊呢,所以要學會怎麼使用MSDN,雖然裡面解釋大多是英文,只要理解了其中的參數,就迎刃而解了。這裡是直接從MSDN複製過來的,以transform為例:transformtemplate<class InIt, class OutIt, class Unop> OutIt transform(InIt first, InIt last, OutIt x, Unop uop);template<class InIt1,
Time of Update: 2018-12-03
跟auto_ptr對照一下,auto_ptr複製或轉移擁有權,看看計數指標有什麼不同,看看計數指標是怎麼實現的?#include<iostream>#include<string>#include<deque>#include<algorithm>#include<vector>#include<list>using namespace std;template<typename T>class
Time of Update: 2018-12-03
algorithm的sort演算法給我們帶了很多大的方便,用來排序容器的內容,真是方便,不僅可以從小到大的排序,還能從大到小的排序。可以結合function的函數對象,也能自己寫謂詞。真是太方便了。其他的演算法也如出一轍,舉一反三。1普通排序:#include<iostream>#include<vector>#include<algorithm>using namespace std;int main(){ int a[]={8,6,2,4,7,1,5,3,
Time of Update: 2018-12-03
組合型函數分成以下幾種,自己寫函數適配器f(g(elem)) ------> compose_f_gxf(g(elem1,elem2)) ------> compose_f_gxyf(g(elem),h(elem)) ------> compose_f_gx_hxf(g(elem),h(elem2)) ------>
Time of Update: 2018-12-03
今天偵錯工具的時候,遇到了這樣一個問題bool check(int elem);vecot<int>v; ...pos=find_if(v.begin(),v.end(),not1(check))竟然出錯,尋找資料之後,發現,原來原因如下:ptr_fun做的唯一的事是使一些typedef有效(
Time of Update: 2018-12-03
Windows的GetKeyState函數能夠返回滑鼠按鍵或Shift等鍵的狀態,當GetKeyState函數返回一個負值時,表示已經按下了滑鼠按鍵或相應的Shift或Ctrl鍵。在沒有按下滑鼠時,不能使用GetKeyState函數。在滑鼠事件的時候,有時候,需要對參數wParam和MK_XX進行位與運算if(wParam & MK_SHIFT){ if(wParam & MK_CONTROL) { 按下Shift+Ctrl }
Time of Update: 2018-12-03
此處引用了某個人的文章的回複,我覺得回答得不錯,引用一下:**在c++中,記憶體分為heap(堆區)--放置由new動態分配的記憶體,stack(棧區)--放置臨時(局部)變數,和靜態儲存區--放置靜態和全域變數;**非局部靜態變數,在main()函數前由編譯器分配記憶體並初始化(構造),在main()結束後由編譯器將其佔用的記憶體釋放(析構),所以這部分記憶體只要程式不結束就不會釋放;**局部變數在程式執行過程中由編譯器自動產生和銷毀;
Time of Update: 2018-12-03
直接代碼如下//merge#include<iostream>#include<string>#include<vector>#include<algorithm>#include<functional>using namespace std;bool fanxu(int a,int b){return a>b;}int main(){int a[]={1,5,3,9,7};int b[]={4,2,6,10,8};vector&
Time of Update: 2018-12-03
按照c++之父的經典之作 c++程式設計語言的說法dynamic_cast<type>()可以把基類的指標轉化為子類的指標,代碼如下:#include<iostream>#include<string>using namespace std;class Person{public: Person(string name,int age){ this->name=name; this->age=age; } virtual void show()
Time of Update: 2018-12-03
直接代碼:#include<iostream>#include<string>#include<vector>#include<algorithm>#include<functional>#include<cmath>using namespace std;int main(){int
Time of Update: 2018-12-03
const變數可以是const變數和其他任何類型的引用,但是非const只能引用與自己類型相同的變數,如下#include<iostream>using namespace std;int main(){ const int a=100; const int &b=a; //正確cout<<a<<endl;cout<<b<<endl;return 0;} #include<iostream>using
Time of Update: 2018-12-03
先來對比一下用虛基類和不用虛基類的區別#include<iostream>using namespace std;class Base{public: Base(){ a=5; cout<<"Base a="<<a<<endl; }protected: int a;};class Base1:public Base{public: Base1(){ a=a+10; cout<<"Base1
Time of Update: 2018-12-03
直接代碼:#include<iostream>#include<string>#include<vector>#include<algorithm>using namespace std;int main(){int
Time of Update: 2018-12-03
代碼如下:#include<iostream>#include<string>#include<vector>#include<algorithm>#include<functional>using namespace std;int main(){int
Time of Update: 2018-12-03
直接進入主題,請看pair的源碼:namespace std{template<typename T1,typename T2>struct pair{typedef T1 first_type; //簡化類型名稱typedef T2 second_type;T1 first; //兩個資料成員T2 second;pair():first(T1()),second(T2()){} //預設建構函式,用類型的預設建構函式初始化兩個資料成員pair(const T1&
Time of Update: 2018-12-03
代碼如下:#include<iostream>#include<string>#include<vector>#include<algorithm>#include<functional>using namespace std;bool jianyi(int a,int b){return a==b-1;}int main(){int a[]={1,2,3,4,5,6,7,8,9,10};int b[]={1,2,3,5,6,7,8,9