C++多線程實現(xiàn)電子詞典
更新時間:2019年03月20日 10:55:02 作者:Cosmop01itan
這篇文章主要為大家詳細(xì)介紹了C++多線程實現(xiàn)電子詞典,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了C++多線程實現(xiàn)電子詞典的具體代碼,供大家參考,具體內(nèi)容如下
// Dictionary.cpp : 定義控制臺應(yīng)用程序的入口點。
//vs2013編譯
//字典文件:https://pan.baidu.com/s/1YHtwptaq_V8j034U9_J96A
#include "stdafx.h"
#include <string>
#include <vector>
#include <iostream>
#include <map>
#include <fstream>
#include <io.h>
#include <thread>
#include <time.h>
#include <Windows.h>
using namespace std;
class ParseDirectory
{
public:
ParseDirectory(string path){
this->path = path;
getFiles(files);
isdone = false;
t = thread(&ParseDirectory::txtToDic, this);
//t.join();
}
bool isDone()
{
return isdone;
}
map<string, string> getDic()
{
return vecDics;
}
virtual ~ParseDirectory()
{
}
private:
vector<string> files;
string path;
thread t;
map<string, string> vecDics;
bool isdone;
void getFiles(vector<string>& files)
{
//文件句柄
long hFile = 0;
//文件信息
struct _finddata_t fileinfo;
string p;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
{
do
{
//如果是目錄,迭代之
if ((fileinfo.attrib & _A_SUBDIR))
{
//if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
//getFiles(p.assign(path).append("\\").append(fileinfo.name), files);
continue;
}
else
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}
void txtToDic()
{
for each (string file in files)
{
fstream f(file);
string word, explain;
//map<string, string> dic;
if (f.is_open())
{
//cout << file << endl;
while (1)
{
getline(f, word);
if (!getline(f, explain))
break;
vecDics[word] = explain;
}
}
f.close();
//vecDics.push_back(dic);
}
//cout << vecDics.size() << endl;
isdone = true;
}
};
void setColor(unsigned short ForeColor = 2, unsigned short BackGroundColor = 0)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);//獲取當(dāng)前窗口句柄
SetConsoleTextAttribute(handle, ForeColor + BackGroundColor * 0x10);//設(shè)置顏色
}
int _tmain(int argc, _TCHAR* argv[])
{
vector<ParseDirectory*> pds;
cout << "正在加載資源...";
long start = clock();
vector<map<string, string> > allWords;
for (int i = 0; i < 26; i++)
{
string name = ".\\";
name += 'A' + i;
pds.push_back(new ParseDirectory(name));
}
int cnt = 0;
for (int i = 0; i < pds.size(); i++)
{
if (pds[i]->isDone())
{
cnt++;
allWords.push_back(pds[i]->getDic());
Sleep(300);
}
if (cnt == pds.size())
break;
}
system("cls");
cout << "加載完成!" << "耗時:" << (clock()-start)/1000.0 << "s" << endl;
cout << allWords.size();
string inquir;
while (1)
{
bool flag = false;
setColor();
cout << "\n輸入要查詢的單詞:";
setColor(7, 0);
cin >> inquir;
for (int i = 0; i < allWords.size(); i++)
{
auto t = allWords[i][inquir];
if (t.size())
{
cout << t << endl;
flag=true;
}
}
if (!flag)
{
setColor(4, 0);
cout << "抱歉,未找到單詞" << endl;
}
}
system("pause");
return 0;
}
效果圖:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Cocos2d-x中獲取系統(tǒng)時間和隨機數(shù)實例
這篇文章主要介紹了Cocos2d-x中獲取系統(tǒng)時間和隨機數(shù)實例,本文代碼含有大量注釋來講解獲取系統(tǒng)時間和隨機數(shù)的方法,需要的朋友可以參考下2014-09-09
C++?Boost?weak_ptr智能指針超詳細(xì)講解
智能指針是一種像指針的C++對象,但它能夠在對象不使用的時候自己銷毀掉。雖然STL提供了auto_ptr,但是由于不能同容器一起使用(不支持拷貝和賦值操作),因此很少有人使用。它是Boost各組件中,應(yīng)用最為廣泛的一個2022-11-11
C++ 動態(tài)創(chuàng)建按鈕及 按鈕的消息響應(yīng)
這篇文章主要介紹了C++ 動態(tài)創(chuàng)建按鈕及 按鈕的消息響應(yīng)的相關(guān)資料,需要的朋友可以參考下2015-06-06
OpenCV實現(xiàn)彩色照片轉(zhuǎn)換成素描卡通片
這篇文章主要為大家詳細(xì)介紹了OpenCV實現(xiàn)彩色照片轉(zhuǎn)換成素描卡通片,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01
typedef和#define用法區(qū)別總結(jié)
在C還是C++代碼中,typedef都使用的很多,在C代碼中尤其多,typedef與#define有些相似,其實是不同的,特別是在一些復(fù)雜的用法上,下面這篇文章主要給大家介紹了關(guān)于typedef和#define用法區(qū)別總結(jié)的相關(guān)資料,需要的朋友可以參考下2023-06-06

