CTP相關(guān)c代碼說明_第1頁
CTP相關(guān)c代碼說明_第2頁
CTP相關(guān)c代碼說明_第3頁
CTP相關(guān)c代碼說明_第4頁
CTP相關(guān)c代碼說明_第5頁
已閱讀5頁,還剩217頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、1.容器vector刪除元素,漏掉被刪除元素的下一個元素(for循環(huán)也是一樣,不要在括號里就+itr,要在else里面)#include#includeusing namespace std;int main()vectorarray1; array1.push_back(1); array1.push_back(2); array1.push_back(3); array1.push_back(4); array1.push_back(5);vector:iterator itr = array1.begin();coutwhile循環(huán)里面,3沒有被刪除,為何打印不出?endl;while(

2、itr!=array1.end() cout*itrendl;if(*itr = 2) itr = array1.erase(itr); /cout*itrendl;/這邊可以打印出3 +itr; cout-endl;cout這邊可以打印出3endl;for(itr = array1.begin();itr!=array1.end();itr+)cout*itrendl;return 0;這樣才不會漏掉#include#includeusing namespace std;int main()vectorarray1; array1.push_back(1); array1.push_back

3、(2); array1.push_back(3); array1.push_back(4); array1.push_back(5);vector:iterator itr = array1.begin();while(itr!=array1.end() cout*itrendl;/單獨(dú)打印,這邊要小心漏掉被刪除元素的下一個元素if(*itr = 2) itr = array1.erase(itr);/刪除值為2的元素,itr已經(jīng)指向被刪除元素的下一個元素 else /cout*itrendl;/單獨(dú)打印 +itr;/要else,才不會漏掉被刪除元素的下一個元素 cout-endl;for(i

4、tr = array1.begin();itr!=array1.end();itr+)cout*itrendl;return 0;另#include#includeusing namespace std;int main()vectorarray1; array1.push_back(1); array1.push_back(2); array1.push_back(3); array1.push_back(4); array1.push_back(5);vector:iterator itr = array1.begin();while(itr!=array1.end() /cout*itr

5、endl;/單獨(dú)打印,這邊要小心漏掉被刪除元素的下一個元素if(*itr = 2) itr = array1.erase(itr);/刪除值為2的元素,itr已經(jīng)指向被刪除元素的下一個元素 else cout*itrendl;/單獨(dú)打印 +itr;/要else,才不會漏掉被刪除元素的下一個元素 cout-endl;for(itr = array1.begin();itr!=array1.end();itr+)cout*itrendl;return 0;2.結(jié)構(gòu)體vector的排序#include#include#include#include/#include /Linux的vim要增加這個u

6、sing namespace std; struct AssessTypeInfounsigned int m_uiType; /類型ID char m_szName64; /類型名稱unsigned int m_uiTotal; /總分?jǐn)?shù);bool lessmark(const AssessTypeInfo& s1,const AssessTypeInfo& s2) return s1.m_uiType s2.m_uiType; int main() vectorctn; AssessTypeInfo a1; a1.m_uiType=1;AssessTypeInfo a2; a2.m_uiT

7、ype=2;AssessTypeInfo a3; a3.m_uiType=3;ctn.push_back(a1);ctn.push_back(a2);ctn.push_back(a3); sort(ctn.begin(), ctn.end(),lessmark); /升序排序for(int i=0; i3;i+) printf(%dn,ctni.m_uiType); cout-endl; /像數(shù)組那樣輸出 sort(ctn.begin(), ctn.end(),greatermark) ; /降序排序for(int i=0;i3;i+)coutctni.m_uiTypeendl;cout-en

8、dl;vector:iterator iter;for(iter = ctn.begin();iter != ctn.end();iter+)coutm_uiTypeendl;return 0; 3.容器迭代器前一個值#include#includeusing namespace std; int main()vectora,b;for(int i = 0;i10;i+)a.push_back(i);vector:iterator iter;for(iter = a.begin();iter!=a.end();iter+)cout*iterendl;cout-endl;for(iter = a

9、.begin()+1;iter!=a.end();iter+)cout*itert*(iter-1)endl;4.a容器的值賦給b容器#include#includeusing namespace std; int main()vectora,b;for(int i = 0;i10;i+)a.push_back(i);vector:iterator iter;for(iter = a.begin();iter!=a.end();iter+)cout*iterendl;b.push_back(*iter);/a容器的值賦給b容器cout-endl;for(iter = b.begin();ite

10、r!=b.end();iter+)cout*iterendl;return 0;5Atof()轉(zhuǎn)換字符串到double,Cout出的是12345.7,但實(shí)際還是12345.67/#include /#include #includeusing namespace std;int main()/float f;/float不行double f;char *str = 12345.67;f = atof(str);printf(string = %s float = %5.2fn, str, f);coutfendl;coutf+1endl;cout(1 = 1)endl;cout(f=12345

11、.67)endl;if(f=12345.67)couthelloendl;return 0;6.函數(shù)對結(jié)構(gòu)體變量賦值#include#includeusing namespace std;struct struct_varint a;string b;void struct_fun(struct_var &s,int x,string y)s.a = x;s.b = y;int main()struct_var stu;stu.a = 1;stu.b = h;coutstu.aendl;coutstu.bendl;struct_var s1;struct_fun(s1,100,hello);c

12、outs1.aendl;couts1.bendl;return 0;7.函數(shù)對結(jié)構(gòu)體容器進(jìn)行賦值#include#include#includeusing namespace std;struct struct_varint a;string b;void struct_fun(struct_var &s,vector&vec1,int x,string y)s.a = x;s.b = y;vec1.push_back(s);int main()struct_var stu;stu.a = 1;stu.b = h;cout直接對結(jié)構(gòu)體賦值:endl;coutstu.aendl;coutstu.

13、bendl;struct_var s1;vectorvec;struct_fun(s1,vec,100,hello);cout通過函數(shù)對結(jié)構(gòu)體賦值:endl;couts1.aendl;couts1.bendl;cout通過函數(shù)對結(jié)構(gòu)體容器賦值:endl;for(vector:iterator iter = vec.begin();iter!=vec.end();iter+)coutaendlbendl;return 0;8.結(jié)構(gòu)體容器賦值給另一個結(jié)構(gòu)體容器#include#include#includeusing namespace std;struct struct_varint a;str

14、ing b;int main()struct_var stu;vectorvec1,vec2;stu.a = 0;stu.b = a_vec1;vec1.push_back(stu);stu.a = 1;stu.b = b_vec1;vec1.push_back(stu);stu.a = 2;stu.b = a_vec2;vec2.push_back(stu);for(vector:iterator iter = vec1.begin();iter!=vec1.end();iter+)coutatbendl;for(vector:iterator iter = vec2.begin();ite

15、r!=vec2.end();iter+)coutatbendl;cout-endl;vec2.insert(vec2.end(),vec1.begin(),vec1.end();for(vector:iterator iter = vec2.begin();iter!=vec2.end();iter+)coutatbendl;return 0;9.對vector中滿足條件的值的計(jì)數(shù),count_fun調(diào)用不需要實(shí)參?#include #include #include using namespace std; bool count_fun (int value) return value =

16、1; int main() vectorvec1;vec1.push_back(1);vec1.push_back(1);vec1.push_back(100);vec1.push_back(1);for(vector:iterator iter = vec1.begin();iter != vec1.end();iter+)cout*iterendl;cout-endl;vector:iterator:difference_type num;num = count_if(vec1.begin(), vec1.end(), count_fun);/類型為vector:iterator:diff

17、erence_typeint num1 = count_if(vec1.begin(), vec1.end(), count_fun);/類型為intcoutvec1中值為1的個數(shù)為numendl;coutvec1中值為1的個數(shù)為num1endl;return 0;10對結(jié)構(gòu)體vector中滿足條件的值的計(jì)數(shù),count_stage(student &s),參數(shù)不加&也可以?#include #include #include #includeusing namespace std; struct studentint a;string b;bool count_stage(student &

18、s)return s.a = 1;int main() student stu;vectorvec;int num;stu.a = 1;stu.b = str1;vec.push_back(stu);stu.a = 10;stu.b = str10;vec.push_back(stu);stu.a = 1;stu.b = str3;vec.push_back(stu);for(vector:iterator iter = vec.begin();iter!=vec.end();iter+)coutatbendl;cout-endl;num = count_if(vec.begin(), vec

19、.end(), count_stage);cout容器vec中的結(jié)構(gòu)體中的a值為1的有:numendl;return 0;11.string對象和C字符串之間的轉(zhuǎn)換#include#includeusing namespace std;int main() char a = hello;/C字符串/char c = h;/C字符串,錯誤,大小不夠大char c = hello,how are you;/C字符串,錯誤,大小不夠大string b;coutaendl;b = a;/直接賦值給string類型變量coutbendl;strcpy(c,b.c_str();/賦值給C字符串,要求變量c

20、的大小要大于b,否則溢出coutcendl;return 0; 另:上面那段代碼,char c不足的話,g+不會報(bào)錯,值也正確,但是那樣的代碼不行。有人建議Linux用strncpy:VS2010編譯的時候根據(jù)長度進(jìn)行了校驗(yàn),而g+是不校驗(yàn)的這個是肯定的,Linux我們嚴(yán)格限制不許使用strcpy,而改為strncpy的#include#include#include using namespace std;int main() char c = h;coutc,strlen(c)endl; string b=hhhh;strcpy(c,b.c_str();/在VS報(bào)錯,但在g+不報(bào)錯cout

21、cendl;return 0; 12.容器排序#include#include#includeusing namespace std;void show_val(char val)coutvalendl;int main()vector char_vec;char_vec.push_back(Z);char_vec.push_back(D);char_vec.push_back(F);char_vec.push_back(S);for_each(char_vec.begin(),char_vec.end(),show_val);sort(char_vec.begin(),char_vec.en

22、d();cout-endl;for_each(char_vec.begin(),char_vec.end(),show_val);return 0;另int排序:#include#include#includeusing namespace std;void show_val(int val)coutvalendl;int main()vector char_vec;char_vec.push_back(3);char_vec.push_back(9);char_vec.push_back(7);char_vec.push_back(1);for_each(char_vec.begin(),c

23、har_vec.end(),show_val);sort(char_vec.begin(),char_vec.end();cout-endl;for_each(char_vec.begin(),char_vec.end(),show_val);return 0;13.ini數(shù)據(jù)讀寫,相對路徑是./student.ini,就是項(xiàng)目路徑,若CString變量要轉(zhuǎn)成string,需要用(LPCTSTR)先對CString變量強(qiáng)制轉(zhuǎn)換#include#include using namespace std;int main(void)/往ini寫數(shù)據(jù) CString strName,strTemp;i

24、nt nAge = 10;strName=張三;:WritePrivateProfileString(StudentInfo,Name,strName,c:studstudent.ini); /往ini寫字符串strTemp.Format(%d,nAge);:WritePrivateProfileString(StudentInfo,Age,strTemp,c:studstudent.ini); /往ini寫int,先轉(zhuǎn)換成CString/往ini讀數(shù)據(jù)CString read_name;int read_age; GetPrivateProfileString(StudentInfo,Nam

25、e,默認(rèn)姓名,read_name.GetBuffer(MAX_PATH),MAX_PATH,c:studstudent.ini); /讀取ini字符串,StudentInfo和Name有錯的話則取默認(rèn)姓名cout讀取到的名字:read_nameendl;read_age=GetPrivateProfileInt(StudentInfo,Age,17,c:studstudent.ini); /讀取ini整型值cout讀取到的年齡:read_ageendl;return 0;另:#include#include using namespace std;int main(void)/往ini寫數(shù)據(jù),

26、都要是CString CString strName,strTemp;int nAge = 10;strName=張三;/:WritePrivateProfileString(StudentInfo,Name,strName,c:studstudent.ini); /往ini寫字符串:WritePrivateProfileString(StudentInfo,Name,strName,c:/stud/student.ini);strTemp.Format(%d,nAge);/:WritePrivateProfileString(StudentInfo,Age,strTemp,c:studstu

27、dent.ini); /往ini寫int,先轉(zhuǎn)換成CString:WritePrivateProfileString(StudentInfo,Age,strTemp,c:/stud/student.ini); /往ini讀數(shù)據(jù)CString read_name;int read_age; GetPrivateProfileString(StudentInfo,Name,默認(rèn)姓名,read_name.GetBuffer(MAX_PATH),MAX_PATH,c:/stud/student.ini); /讀取ini字符串,StudentInfo和Name有錯的話則取默認(rèn)姓名cout讀取到的名字:r

28、ead_nameendl;read_age=GetPrivateProfileInt(StudentInfo,Age,17,c:/stud/student.ini); /讀取ini整型值cout讀取到的年齡:read_ageendl;return 0;另:double#include#include using namespace std;int main(void)/往ini寫數(shù)據(jù),都要是CString CString strName,strTemp;double nAge = 10.7;strName=張三;:WritePrivateProfileString(StudentInfo,Na

29、me,strName,c:/stud/student.ini);/往ini寫字符串strTemp.Format(%lf,nAge);/double對應(yīng)%lf,float對應(yīng)%f:WritePrivateProfileString(StudentInfo,Age,strTemp,c:/stud/student.ini); /往ini寫double,先轉(zhuǎn)換成CString/往ini讀數(shù)據(jù)CString read_name, read_doubleAge;/int read_age; GetPrivateProfileString(StudentInfo,Name,默認(rèn)姓名,read_name.Ge

30、tBuffer(MAX_PATH),MAX_PATH,c:/stud/student.ini); /讀取ini字符串,StudentInfo和Name有錯的話則取默認(rèn)姓名cout讀取到的名字:read_nameendl;/read_age=GetPrivateProfileInt(StudentInfo,Age,17,c:/stud/student.ini); /讀取ini整型值/cout讀取到的年齡:read_ageendl;GetPrivateProfileString(StudentInfo,Age,17,read_doubleAge.GetBuffer(MAX_PATH),MAX_PA

31、TH,c:/stud/student.ini);cout讀取到的nAge字符串:read_doubleAgeendl;double age = atof(read_doubleAge);coutdouble age:ageendl;return 0;另:double顯示問題#include#include using namespace std;int main(void)/往ini寫數(shù)據(jù),都要是CString CString strName,strTemp;double nAge = 990000.3;strName=張三;:WritePrivateProfileString(Student

32、Info,Name,strName,c:/stud/student.ini);/往ini寫字符串strTemp.Format(%lf,nAge);/double對應(yīng)%lf,float對應(yīng)%f:WritePrivateProfileString(StudentInfo,Age,strTemp,c:/stud/student.ini); /往ini寫double,先轉(zhuǎn)換成CString/往ini讀數(shù)據(jù)CString read_name, read_doubleAge;GetPrivateProfileString(StudentInfo,Name,默認(rèn)姓名,read_name.GetBuffer(

33、MAX_PATH),MAX_PATH,c:/stud/student.ini); /讀取ini字符串,StudentInfo和Name有錯的話則取默認(rèn)姓名cout讀取到的名字:read_nameendl;GetPrivateProfileString(StudentInfo,Age,17,read_doubleAge.GetBuffer(MAX_PATH),MAX_PATH,c:/stud/student.ini);cout讀取到的nAge字符串:read_doubleAgeendl;double age = atof(read_doubleAge);coutdouble age:ageend

34、l;/只顯示990000,但實(shí)際是990000.3coutage-990000:age-990000endl;return 0;另:/讀取字符串CString read_brokerID;GetPrivateProfileString(Account,brokerID,brokerID_error,read_brokerID.GetBuffer(MAX_PATH),MAX_PATH,./AccountParam.ini);/cout-讀取到的read_brokerID:read_brokerIDendl;/讀取double,合約組單筆虧損上限CString maxLossOne_cs;GetP

35、rivateProfileString(Config,maxLossOne,999999,maxLossOne_cs.GetBuffer(MAX_PATH),MAX_PATH,./configuration.ini);cout讀取到的maxLossOne_cs:maxLossOne_csendl;double maxLossOne = atof(maxLossOne_cs);coutdouble maxLossOne:maxLossOneendl;/讀取int,單個合約最大持倉上限CString maxHolding_cs;GetPrivateProfileString(Config,maxH

36、olding,999999,maxHolding_cs.GetBuffer(MAX_PATH),MAX_PATH,./configuration.ini);cout讀取到的maxHolding_cs:maxHolding_csendl;int maxHolding = atoi(maxHolding_cs);coutint maxHolding:maxHoldingendl;14.容器嵌套#include#includeusing namespace std;int main()vectorvec_int1,vec_int2;for(int i = 0;i10;i+)vec_int1.push

37、_back(i);for(int i = 10;i20;i+)vec_int2.push_back(i);/*vectorvectorvec_int_vec;/保存容器的容器vec_int_vec.push_back(vec_int1);vec_int_vec.push_back(vec_int2);*/保存容器的容器,保存指針效率更高vectorvector *vec_int_vec;vec_int_vec.push_back(&vec_int1);vec_int_vec.push_back(&vec_int2);for(vector:iterator iter = vec_int1.beg

38、in();iter != vec_int1.end();iter+)cout*iterendl;cout-endl;for(vector:iterator iter = vec_int2.begin();iter != vec_int2.end();iter+)cout*iterendl;cout-endl;for(vectorvector*:iterator iter = vec_int_vec.begin();iter != vec_int_vec.end();iter+)/這邊要從保存容器的容器vec_int_vec中打印數(shù)據(jù)cout*iterendl;/這樣只能打印出指針值,就是小容器

39、的地址cout(*iter)endl;for(int i = 0;i size() ; +i)/最好加(int),(*iter),不是*iter/for(int i = 0;i size() ; +i)cout(*(*iter)iendl;return 0;另外一種辦法#include#includeusing namespace std;int main()vectorvec_int1,vec_int2;for(int i = 0;i10;i+)vec_int1.push_back(i);for(int i = 10;i20;i+)vec_int2.push_back(i);/*vector

40、vectorvec_int_vec;/保存容器的容器vec_int_vec.push_back(vec_int1);vec_int_vec.push_back(vec_int2);*/保存容器的容器,保存指針效率更高vectorvector *vec_int_vec;vec_int_vec.push_back(&vec_int1);vec_int_vec.push_back(&vec_int2);for(vector:iterator iter = vec_int1.begin();iter != vec_int1.end();iter+)cout*iterendl;cout-endl;for

41、(vector:iterator iter = vec_int2.begin();iter != vec_int2.end();iter+)cout*iterendl;cout-endl;for(vectorvector*:iterator iter = vec_int_vec.begin();iter != vec_int_vec.end();iter+)/這邊要從保存容器的容器vec_int_vec中打印數(shù)據(jù)cout*iterendl;/這樣只能打印出指針值,就是小容器的地址cout(*iter)endl;/*for(int i = 0;i size() ; +i)/最好加(int),(*

42、iter),不是*iter/for(int i = 0;i size() ; +i)cout(*(*iter)iendl;*/for(vector:iterator iter1 = (*iter)-begin();iter1 != (*iter)-end();iter1+)cout*iter1endl;return 0;15.map用法#include #include using namespace std;int main() map m1; map:iterator m1_Iter;m1.insert ( pair ( 1, 20 ) ); m1.insert ( pair ( 4, 4

43、0 ) ); m1.insert ( pair ( 3, 60 ) ); m1.insert ( pair ( 2, 50 ) ); m1.insert ( pair ( 6, 40 ) ); m1.insert ( pair ( 7, 30 ) );cout The original map m1 is:endl; for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter+ ) coutfirst secondcout-endl;for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m

44、1_Iter+ ) cout(*m1_Iter).first (*m1_Iter).secondendl;/指針對應(yīng)的值,用.cout-first)coutsecondendl;coutm14endl;/4不是下標(biāo),是map的第一個值coutm15endl;/沒有5,對應(yīng)值為0return 0;另:#include #include using namespace std;int main() map m1; map:iterator m1_Iter;m1.insert ( pair ( 1, 20 ) ); m1.insert ( pair ( 4, 40 ) ); m1.insert (

45、pair ( 3, 60 ) ); m1.insert ( pair ( 2, 50 ) ); m1.insert ( pair ( 6, 40 ) ); m1.insert ( pair ( 7, 30 ) );cout The original map m1 is:endl; for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter+ ) coutfirst secondcout-endl;for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter+ ) cout(*m1_

46、Iter).first (*m1_Iter).secondendl;/指針對應(yīng)的值,用.cout-first)coutsecondendl;coutm14endl;/4不是下標(biāo),是map的第一個值coutm1.size():m1.size()endl;coutm15endl;/沒有5,對應(yīng)值為0,這樣使得m1增加了KEY為5的值了cout-endl;coutm1.size():m1.size()endl;cout-endl;for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter+ ) coutfirst secondendl;m1.

47、clear();coutclear()后m1.size():m1.size()endl;return 0;另:指向頭尾元素#include #include using namespace std;int main() map m1; map:iterator m1_Iter;m1.insert ( pair ( 1, 20 ) ); m1.insert ( pair ( 4, 40 ) ); m1.insert ( pair ( 3, 60 ) ); m1.insert ( pair ( 2, 50 ) ); m1.insert ( pair ( 7, 40 ) ); m1.insert (

48、 pair ( 6, 30 ) );m1_Iter = m1.begin();coutfirst:firstendl;m1_Iter = m1.end();m1_Iter-;/VS下如果沒這個,會報(bào)錯,Linux 不會coutm1.end()前面,最后一個元素:firstendl;return 0;另:字符串#include #include#include using namespace std;int main( ) map m1; map:iterator m1_Iter;string str1 = 000001,str2 = 2013-07-07;m1.insert( pair ( s

49、tr1, str2 );m1.insert ( pair ( 700001,2013-07-01 ) ); m1.insert ( pair ( 000077,2013-07-02 ) ); m1.insert ( pair ( 000088 ,2013-07-03) ); m1.insert ( pair ( 600001 ,2013-07-04) ); m1.insert ( pair ( 600002 ,2013-07-05) ); m1.insert ( pair ( 600003,2013-07-06 ) );cout The original map m1 is:endl; for

50、 ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter+ ) coutfirst secondfirst)coutsecondendl;coutm1aendl;coutm1600001endl;return 0;另:#include #include#include using namespace std;int main() map m1; map:iterator m1_Iter;m1.insert ( pair ( IF1403, 0.20 ) ); m1.insert ( pair ( rb1410, 40 ) ); m1.ins

51、ert ( pair ( j1405, 60 ) ); m1.insert ( pair ( j1405, 160 ) ); /重復(fù)了,自動略去cout The original map m1 is:endl; for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter+ ) coutfirst secondendl; coutm1rb1410endl;return 0;另:#include #include#include using namespace std;int main() map m1; map:iterator m1_I

52、ter;m1.insert ( pair ( IF1403, 0.20 ) ); m1.insert ( pair ( rb1410, 40 ) ); m1.insert ( pair ( j1405, 60 ) ); m1.insert ( pair ( j1405, 160 ) ); /重復(fù)了,自動略去cout The original map m1 is:endl; for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter+ ) coutfirst secondendl; coutm1rb1410endl;cout-endl;c

53、har charvar31 = rb1410;coutm1charvar,charvarendl;m1charvar = 100;/更改元素值coutm1charvarendl;m1h = 7;/也可以這樣添加元素coutm1hendl;cout-endl;for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter+ ) coutfirst secondendl; return 0;16.截取字符串#include#includeusing namespace std;int main()string s(SH600000);strin

54、g a=s.substr(2,6);coutaendl;return 0;另:倒數(shù)截取#include#includeusing namespace std;int main()string a = abc2013-07-31.txta;string b = a.substr(a.length()-4, 3);/倒數(shù)第4個字符開始coutbendl;return 0;17.int相除,注意0#include#includeusing namespace std;int main()int a = 3;couta / 100endl;couta / 100.0endl;return 0;18.m

55、ultimap#include #include #include using namespace std;int main()multimap m;/multimap的創(chuàng)建m.insert(pair(Jack,1);m.insert(pair(Jack,7);m.insert(pair(Body,8);m.insert(pair(Navy,4);m.insert(pair(Demo,3);multimap:iterator iter;for (iter = m.begin();iter != m.end();+iter)/遍歷cout(*iter).first (*iter).seconde

56、ndl;m.erase(Navy);/multimap的刪除coutThe element after delete:endl;for (iter = m.begin();iter != m.end();+iter)cout(*iter).first (*iter).secondendl;/multimap元素的查找 multimap:iterator it;int num=m.count(Jack);coutJacks number:numendl;it = m.find(Jack);coutthe search result is :endl;for(int i=1;i=num;i+)co

57、ut(*it).first (*it).secondendl;it+;/if(i=1) coutcan not find!endl; int num_Body = m.count(Body);coutBody個數(shù):num_Bodyendl;it = m.find(Body);for(int i = 0; i num_Body; i+)cout(*it).firstt(*it).secondendl;it+;return 0;19.函數(shù)形參傳引用void PrintIt (string& StringToPrint) cout StringToPrint endl;void PrintIt1 (

58、string StringToPrint) cout StringToPrint endl;上面?zhèn)魉偷氖菍?shí)參string變量的地址給stringtoPrint 下面是直接把實(shí)參string變量拷貝一份給副本stringtoPrintstring&不用生成副本啊string需要個temp臨時對象加個const也許更好= =前者是引用,后者是string類型變量的副本,前者可以改變原變量的值,后者不能20.#include #include #includeusing namespace std;struct studentint num;char name10;char subject20;vo

59、id main (void)struct student student1, *p;student1.num = 13;p = &student1;strcpy(*p).name, 米粒);/*易犯錯誤:(*p).name = 米粒;是錯誤的,字符串只能在數(shù)組定義的時候賦值,定義后不能再進(jìn)行賦值操作; */(*p).name = 米粒1;strcpy(p-subject, 音樂);printf(學(xué)號:%dn姓名:%sn專業(yè):%sn, (*p).num, , p-subject);coutnum,name,subjectendl;21.#includeusing namespace std;st

60、ruct studentint num;char name10;char subject20;work = 1,name,專業(yè);int main (void)coutwork.num,work.subjectendl;return 0;22.如果是Linux下,要增加include 且不能#include #includeusing namespace std;struct studentint num;char name10;char subject20;int main (void)student pr1, *p;p = &pr1;p-num = 1;strcpy(p-name,name)

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論