1,类的const变量必须初始化,并且只能在类的构造函数初始化,static变量只能在类外初始化。
2,对象的的构造和析构遵循金字塔模型,基类先构造然后派生类,析构则是先子类然后基类。
3,标准库基本数据容器,插入和删除元素可能会影响迭代器的位置,最好结合算法谨慎操作。
#include <iostream> // iostream istream ostream #include <fstream> // fstream ifstream ofstream #include <sstream> // stringstream istringstream ostringstream #include <string> #include <iostream> // std::cout std::cin #include <thread> // std::thread, std::thread::id, std::this_thread::get_id #include <chrono> // std::chrono::seconds #include <mutex> // std::mutex::lock std::mutex::unlock #include <list> #include <array> #include <tuple> #include <set> #include <map> #include <vector> #include <iterator> #include <algorithm> // algorithm: count find search copy fill move remove replace reverse swap // algorithm: unique partition merge sort max min using namespace std; class NumFilter{ public: string trim(string line); static bool endian(); bool threads(int *p); bool cmpNum(string const& textAFile, string const& textBFile); private: static string textFileName; }; string NumFilter::textFileName = "text.txt"; string NumFilter::trim(string line){ if (line.empty()) { return line; } line.erase(0, line.find_first_not_of(" ")); line.erase(line.find_last_not_of(" ") + 1); return line; } bool NumFilter::endian(){ int a = 0x12345678; bool d = (*(char*)(&a) == (char) a); // cout << "endian = " << d << endl; return d; } bool NumFilter::threads(int *p){ mutex g_mutex; g_mutex.lock(); (*p)++; g_mutex.unlock(); thread th(endian); th.join(); this_thread::sleep_for(chrono::seconds(1)); return true; } //compare two files bool NumFilter::cmpNum(string const& textAFile, string const& textBFile) { try{ set < string > textASet; set < string > textBSet; set < string > textsSet; ifstream textAStream(textAFile.c_str()); ifstream textBStream(textBFile.c_str()); if(!textAStream || !textBStream) { return false; } fstream textsStream(textFileName.c_str(), ofstream::out); if(!textsStream) { return false; } string line; while(getline(textAStream, line)) { string strNum = trim(line); if(!strNum.empty()) textASet.insert(strNum); } textAStream.close(); while(getline(textBStream, line)) { string strNum = trim(line); if(!strNum.empty()) textBSet.insert(strNum); if( textASet.end() != textASet.find(strNum) ) { pair<set<string>::iterator, bool > retPair; retPair = textsSet.insert(strNum); if( true == retPair.second ) { ostringstream stmtext; stmtext << "" << strNum << ";" << endl; string strtext = stmtext.str(); textsStream << strtext ; //textsStream << strtext << endl; }else{ }// exists } } textBStream.close(); textsStream.close(); return true; } catch (...) { cout << "exception" << endl ; } return false; } int main(int argc, char* argv[]){ return 0; }