作者yauhh (哟)
看板Programming
标题Re: [问题] 两题C++的问题.样板与结构
时间Tue Apr 5 00:37:11 2011
※ 引述《supercygnus (......)》之铭言:
: 第一题:试用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;
: }
这看不出疑点吗? T 型态的 a, b 透过 int 型态的 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。怎麽有这麽短的资料型态,到底是???
一般中文书很少提到的一种语法是标记使用位元数的语法:
struct BitCard { unsigned face:4; unsigned suit:2;
unsigned color:1; };
结构长度不变,但三个栏位会只使用指定数量的位元数目.
--
/yau
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 61.231.64.117
1F:推 supercygnus:嗯嗯,问题是现在要怎样写才能跑呢~?118.167.105.145 04/05 10:24