[問題] 兩題C++的問題.樣板與結構

看板Programming作者 (......)時間13年前 (2011/04/04 23:44), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/6 (看更多)
第一題:試用C++裡function template的方式, 寫出一個swap function,作任何兩個相同型態變數的交換功能。 以下是我的程式碼 #include <iostream> #include <cstdlib> using namespace std; template<class T> T swap(T &a,T &b){ int temp; temp=a; a=b; b=temp; } int main(void){ cout<<"swap(5,8)="<<swap(5,8)<<endl; system("pause"); return 0; } 我有想到用call by reference的方式,可是要怎麼寫到樣版呢~? 目前程式碼不能跑 第二題:利用C語言的語法,定義下列資料型態: Structures BitCard包含三個unsigned bit欄位(fields), 換言之,face, suit, color的長度分別是4, 2及1 bits。 長度分別是4, 2及1 bits。怎麼有這麼短的資料型態,到底是??? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.44.98.95 ※ 編輯: supercygnus 來自: 114.44.98.95 (04/04 23:45)
文章代碼(AID): #1DcUTXd6 (Programming)
討論串 (同標題文章)
文章代碼(AID): #1DcUTXd6 (Programming)