C C 乙個類型別的例項和排序輸出的例子

2021-08-27 02:36:07 字數 2146 閱讀 2589

// c++例項程式

// vc++ 6.0能編譯通過並且正常執行

// 輸出時,標記有黃色背景的是被排列項,以從小到大排序

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

class census

census(void)

census(std::string s, long int n, double f)

};bool _sort_with_name(const census& x1, const census& x2) //城市名按城市的字母先後排序

bool _sort_with_people(const census& x1, const census& x2) //城市人口按數的大小排序

bool _sort_with_civilization(const census& x1, const census& x2) //城市人口文化水平按數的大小排序

int main()

//排序

std::sort(vcity.begin(), vcity.end(), _sort_with_name);

//輸出

std::cout << "sort with name of city: " << std::endl << "***********************************==" << std::endl;

std::cout << "name\tpeople\tcivilization" << std::endl;

std::cout << "***********************************==" << std::endl;

for(std::vector::iterator it = vcity.begin(); it != vcity.end(); it++)

std::cout << "***********************************==" << std::endl;

//排序

std::sort(vcity.begin(), vcity.end(), _sort_with_people);

//輸出

std::cout << "sort with quantity of people: " << std::endl << "***********************************==" << std::endl;

std::cout << "name\tpeople\tcivilization" << std::endl;

std::cout << "***********************************==" << std::endl;

for(it = vcity.begin(); it != vcity.end(); it++)

std::cout << "***********************************==" << std::endl;

//排序

std::sort(vcity.begin(), vcity.end(), _sort_with_people);

//輸出

std::cout << "sort with civilization of people: " << std::endl << "***********************************==" << std::endl;

std::cout << "name\tpeople\tcivilization" << std::endl;

std::cout << "***********************************==" << std::endl;

for(it = vcity.begin(); it != vcity.end(); it++)

system("pause");

return 0;

}

開源乙個C C 日誌類

日誌類可以作為乙個練手專案,實現乙個好的日誌類並不容易,這裡先出第乙個版本,後期持續優化。功能簡單對於新手非常友好,歡迎指正錯誤。該日誌類我命名為cclog,第乙個c是class的意思,忽略這一點的話可以叫clog。作用當然是日誌記錄,寫日誌操作是執行緒安全的,支援類似字串format的形式。基於w...

保證乙個類只有乙個例項存在

需求 在做一些彈出視窗的時候,往往需要在新的彈出視窗被建立出來之前,要檢查是否已經有視窗存在。如果存在,則先移除已經存在的例項,然後再建立新的例項。這樣就保證了乙個類只有乙個例項存在。思路 主要就是在類中設定乙個指向自身的靜態屬性。package private function show void...

例項化乙個類的步驟

用程式測試了一下例項化乙個類的步驟,現總結如下,不對之處請指正。1 從堆 heap 中為例項變數以及其他資料 如指向其他類的指標以及方法的資料 分配記憶體 2 例項變數初始化 a.先初始化最高一級父類中的static變數,再按級別由高到低,最後初始化當前類中的static變數 b.先初始化最高一級父...