刘总的笔记小站

生活常识,娱乐搞笑,编程技巧,智能家居,深度学习,网络神经,数据挖掘

C++容器迭代器和算法

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;
}


发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
搜索
«   2024年9月   »
1
2345678
9101112131415
16171819202122
23242526272829
30
网站分类
最新留言
文章归档
网站收藏
友情链接
图标汇集
Powered by Z-BlogPHP

  • Copyright ©2021 @liuzong All rights reserved.
  • 陕ICP备17016542号