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

/**//**
* 秘密在于conio.h中的getch()從鍵盤中讀取字符時,并不會在屏幕上輸出已經輸入的字符,
* 而用一個putch('*')來哄騙,代表已經輸入一個字符
* 怪不得這個頭文件要叫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這個函數有關.
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)中添加數字后,然后隨便輸入待比較的數字( int a ;),求找出在vector中比a大的數字,比a小的數字,和a相等的數字,或者是a的3倍,5倍等之類的數字的集合.
//開始看下面的代碼之前,你是怎么思考的呢?你是不是覺得直接在里面寫個函數比較就是了?或者是其他的呢?
#include<iostream>
#include <vector>
#include <string>
using namespace std;
/**//*
* 單獨寫出相關的函數 如 比較,倍數之類的,后面調用 。
* 此處僅寫了小于和大于函數 其他的依次類推。
* 注意參數 與指針函數的匹配
*/
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)) 
//個人覺得這個函數設置得很好,用到函數指針傳遞相關的函數。注意相關形參的匹配

{
vector<int> nvec;
for(size_t ix = 0;ix != vec.size(); ++ix)
if(pred(vec [ix],filter_value)) // 調用相關函數進行操作
nvec.push_back(vec[ix]); //滿足結果就保存
return nvec;
}
int main()

{
int value;
vector<int> ivec;
cout <<"請輸入數字: "<<endl;
while(cin >> value)
ivec.push_back(value);
cin.clear(); //使輸入流有效
int ival;
cout<<"請輸入你要比較數字: "<<endl;
cin >>ival;
vector<int> vec=filter_ver1(ivec,ival ,greater_than); // 函數的調用 傳遞函數名即可
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) | 編輯 收藏