如何通过多线程技术优化电子词典的查询效率?

2026-05-08 18:473阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计835个文字,预计阅读时间需要4分钟。

如何通过多线程技术优化电子词典的查询效率?

本文将分享一个C++多线程实现电子词典的示例代码。代码主要用于控制台应用程序的输入点定义。以下为代码内容:

cpp// Dictionary.cpp: 定义控制台应用程序的输入点。// 使用vs2013编译// 字典文件:https://pan.baidu.com/s/1YHtwptaq

#include #include #include #include #include #include

如何通过多线程技术优化电子词典的查询效率?

std::mutex mtx; // 用于线程同步的互斥锁

// 读取字典文件,填充词汇表void loadDictionary(const std::string& filename, std::vector& dictionary) { std::ifstream file(filename); std::string word, definition; while (file >> word >> definition) { dictionary.emplace_back(word, definition); } file.close();}

// 查询词汇void queryWord(const std::vector& dictionary, const std::string& word) { std::lock_guard lock(mtx); // 线程同步 for (const auto& entry : dictionary) { if (entry.first==word) { std::cout << Definition: <

int main() { std::vector dictionary; loadDictionary(dictionary.txt, dictionary); // 加载字典

std::string word; std::cout <> word; // 获取用户输入

std::thread t(queryWord, std::ref(dictionary), word); // 创建线程进行查询 t.join(); // 等待线程完成

return 0;}

本文实例为大家分享了C++多线程实现电子词典的具体代码,供大家参考,具体内容如下

// Dictionary.cpp : 定义控制台应用程序的入口点。 //vs2013编译 //字典文件: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);//获取当前窗口句柄 SetConsoleTextAttribute(handle, ForeColor + BackGroundColor * 0x10);//设置颜色 } 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; }

效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

本文共计835个文字,预计阅读时间需要4分钟。

如何通过多线程技术优化电子词典的查询效率?

本文将分享一个C++多线程实现电子词典的示例代码。代码主要用于控制台应用程序的输入点定义。以下为代码内容:

cpp// Dictionary.cpp: 定义控制台应用程序的输入点。// 使用vs2013编译// 字典文件:https://pan.baidu.com/s/1YHtwptaq

#include #include #include #include #include #include

如何通过多线程技术优化电子词典的查询效率?

std::mutex mtx; // 用于线程同步的互斥锁

// 读取字典文件,填充词汇表void loadDictionary(const std::string& filename, std::vector& dictionary) { std::ifstream file(filename); std::string word, definition; while (file >> word >> definition) { dictionary.emplace_back(word, definition); } file.close();}

// 查询词汇void queryWord(const std::vector& dictionary, const std::string& word) { std::lock_guard lock(mtx); // 线程同步 for (const auto& entry : dictionary) { if (entry.first==word) { std::cout << Definition: <

int main() { std::vector dictionary; loadDictionary(dictionary.txt, dictionary); // 加载字典

std::string word; std::cout <> word; // 获取用户输入

std::thread t(queryWord, std::ref(dictionary), word); // 创建线程进行查询 t.join(); // 等待线程完成

return 0;}

本文实例为大家分享了C++多线程实现电子词典的具体代码,供大家参考,具体内容如下

// Dictionary.cpp : 定义控制台应用程序的入口点。 //vs2013编译 //字典文件: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);//获取当前窗口句柄 SetConsoleTextAttribute(handle, ForeColor + BackGroundColor * 0x10);//设置颜色 } 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; }

效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。