struct 與class 的區(qū)別
以前一直沒有明白struct結(jié)構(gòu)體與class類 的區(qū)別:
(1)
名字不同一個是struct,一個是class
(2)
默認(rèn)的訪問屬性不同 struct是public,class 是private
posted @ 2009-05-21 22:19 彈杯一笑 閱讀(242) | 評論 (0) | 編輯 收藏
以前一直沒有明白struct結(jié)構(gòu)體與class類 的區(qū)別:
(1)
名字不同一個是struct,一個是class
(2)
默認(rèn)的訪問屬性不同 struct是public,class 是private
posted @ 2009-05-21 22:19 彈杯一笑 閱讀(242) | 評論 (0) | 編輯 收藏
//此代碼有一個網(wǎng)友所寫
#include <iostream>
#include <conio.h>

/**//**
* 秘密在于conio.h中的getch()從鍵盤中讀取字符時,并不會在屏幕上輸出已經(jīng)輸入的字符,
* 而用一個putch('*')來哄騙,代表已經(jīng)輸入一個字符
* 怪不得這個頭文件要叫conio.h, con的意思就有哄騙,看來就是由此而來.
*/
using namespace std;

int main()
{
char* password;
char* passwordConfirm;
int length = 4;
password = new char[length + 1];
passwordConfirm = new char[length + 1];
char* p = NULL;
int count = 0;
cout << "Input password : ";
p = password;
count = 0;
//fflush(stdin);
while (((*p = getch()) != 13) && count < length)
{
// 這里不是'\n'(10), new line
// 而是'\r'(13), reback. 即是按下回車鍵,好像這個東西是linux的.
// 主要是與getch這個函數(shù)有關(guān).
putch('*');
fflush(stdin);
p++;
count++;
}
password[count] = '\0';
cout << endl << "Confirm the password : ";
p = passwordConfirm;
count = 0;
//fflush(stdin);
while (((*p = getch()) != 13) && count < length)
{
putch('*');
fflush(stdin);
p++;
count++;
}
passwordConfirm[count] = '\0';
cout << endl;
if (strcmp(password, passwordConfirm) == 0)
{
cout << "The password is right." << endl;
cout << password << endl;
} else
{
cout << "Confirm password fail." << endl;
cout << password << endl << passwordConfirm << endl;
}
return 0;
}

posted @ 2009-05-12 10:44 彈杯一笑 閱讀(892) | 評論 (0) | 編輯 收藏
posted @ 2009-04-01 23:17 彈杯一笑 閱讀(439) | 評論 (0) | 編輯 收藏
// essential c++
向一個容器(比如 vector)中添加數(shù)字后,然后隨便輸入待比較的數(shù)字( int a ;),求找出在vector中比a大的數(shù)字,比a小的數(shù)字,和a相等的數(shù)字,或者是a的3倍,5倍等之類的數(shù)字的集合.
//開始看下面的代碼之前,你是怎么思考的呢?你是不是覺得直接在里面寫個函數(shù)比較就是了?或者是其他的呢?
#include<iostream>
#include <vector>
#include <string>
using namespace std;
/**//*
* 單獨(dú)寫出相關(guān)的函數(shù) 如 比較,倍數(shù)之類的,后面調(diào)用 。
* 此處僅寫了小于和大于函數(shù) 其他的依次類推。
* 注意參數(shù) 與指針函數(shù)的匹配
*/
bool less_than(int v1,int v2)

{
return v1<v2?true:false;
}
bool greater_than(int v1,int v2)

{
return v1 > v2 ? true:false;
}
vector<int> filter_ver1(const vector<int> &vec,int filter_value,bool(*pred)(int,int)) 
//個人覺得這個函數(shù)設(shè)置得很好,用到函數(shù)指針傳遞相關(guān)的函數(shù)。注意相關(guān)形參的匹配

{
vector<int> nvec;
for(size_t ix = 0;ix != vec.size(); ++ix)
if(pred(vec [ix],filter_value)) // 調(diào)用相關(guān)函數(shù)進(jìn)行操作
nvec.push_back(vec[ix]); //滿足結(jié)果就保存
return nvec;
}
int main()

{
int value;
vector<int> ivec;
cout <<"請輸入數(shù)字: "<<endl;
while(cin >> value)
ivec.push_back(value);
cin.clear(); //使輸入流有效
int ival;
cout<<"請輸入你要比較數(shù)字: "<<endl;
cin >>ival;
vector<int> vec=filter_ver1(ivec,ival ,greater_than); // 函數(shù)的調(diào)用 傳遞函數(shù)名即可
vector<int>::iterator it=vec.begin();
while(it!= vec.end())
cout<<*it++<<" ";
cout<<endl;
system("pause");
return 0;
}posted @ 2009-03-30 23:54 彈杯一笑 閱讀(427) | 評論 (0) | 編輯 收藏