[英]Most efficient way to search array of string objects in c++?
我一直在尋找有關此主題的更多信息,但似乎找不到我要找的答案,因此希望您能提供幫助!
我正在做的作業的一部分是編寫一個程序,該程序搜索字符串數組(地址簿),如果找到完全或部分匹配項,則返回匹配項。 我可以使用C字符串數組輕松完成此操作,其中strstr()函數通過for循環運行,並將指針設置為將用戶輸入關鍵字運行到數組的結果(請參見下文)。
我的問題是,如果有的話,我怎么能利用String對象做到這一點? 我還需要考慮可能存在不止一場比賽。 這也是解決該程序的最有效方法嗎? 我已經提交了我的工作版本,我只是好奇其他一些方法可以完成相同的任務!
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
bool isFound = false; // Flag to indicate whether contact is found
const int SIZE = 11; // Size of contacts array
const int MAX = 50; // Maximum characters per row
char contacts[SIZE][MAX] = {
"Jig Sawyer, 555-1223",
"Michael Meyers, 555-0097",
"Jason Vorhees, 555-8787",
"Norman Bates, 555-1212",
"Count Dracula, 555-8878",
"Samara Moran, 555-0998",
"Hannibal Lector, 555-8712",
"Freddy Krueger, 555-7676",
"Leather Face, 555-9037",
"George H Bush, 555-4939",
"George W Bush, 555-2783"
};
char *ptr = NULL; // Pointer to search string within contacts
char input[MAX]; // User search input string
// Get the user input
cout << "Please enter a contact to lookup in the address book: ";
cin.getline(input,MAX);
// Lookup contact(s)
for (int i=0; i<SIZE; i++)
{
ptr = strstr(contacts[i], input);
if (ptr != NULL)
{
cout << contacts[i] << endl;
isFound = true;
}
}
// Display error message if no matches found
if (!contactFound)
cout << "No contacts found." << endl;
return 0;
}
如您所知,我喜歡恐怖電影:)
您確實需要將每個字符串分成可排序的組件。 如果您還不了解結構,則可以使用更多數組。 這將使您能夠構建“索引”表,從而加快搜索速度。
最有效的方法取決於數據量和數據組織。
對於小型數據集,不同搜索方法之間的時間差通常可以忽略不計-程序中的某些其他項將花費更長的時間(例如輸入或輸出)。
對於字符串數據,大部分時間都花費在將一個字符串的每個字符與另一個字符串進行比較上。 其他操作(例如,移動索引)可以忽略不計。
由於已經執行了搜索方法之間的比較,因此請在網上搜索“性能字符串搜索比較”。
另一種選擇是使用正則表達式進行字符串搜索。 現在有很多信息 ,我將僅提供一個簡單的示例,在該示例中,您嘗試將記錄(地址)的子范圍與word2Search
(為避免使示例混亂,我對其進行了硬編碼)。
我還使用了(注釋中已經提到的一種技術)對數組進行排序的預處理步驟。 當心兩件事:
完成排序以啟用快速搜索方法,即二進制搜索(在此處使用lower_bound
upper_bound
實現)
如果您要搜索的單詞不在記錄的開頭,則對記錄進行排序是沒有意義的,因為您將無法找到要搜索的有效范圍(此處it
ite
)(例如,如果搜索的是數字,字符串的排序將在字符串之間按字典順序進行比較,因此在以M
J
開頭的字符串之間定位555
不會有什么好處,依此類推)
注釋中的解釋:
int main()
{
// 1. Minor change - an array of strings is used
string contacts[] = {
"Jig Sawyer, 555-1223",
"Michael Meyers, 555-0097",
"Jason Vorhees, 555-8787",
"Norman Bates, 555-1212",
"Count Dracula, 555-8878",
"Samara Moran, 555-0998",
"Hannibal Lector, 555-8712",
"Freddy Krueger, 555-7676",
"Leather Face, 555-9037",
"George H Bush, 555-4939",
"George W Bush, 555-2783"
};
// 2. The array is sorted to allow for binary search
sort(begin(contacts), end(contacts));
// 3. Example hard coded a word to search
string word2Search = "George";
// 4. A regular expression is formed out of the target word
regex expr(word2Search);
// 5. Upper and lower bounds are set for the search
char f = word2Search[0];
std::string val1(1, f);
std::string val2(1, ++f);
// 6. Perform the search using regular expressions
for (auto it(lower_bound(begin(contacts), end(contacts), val1)),
ite(lower_bound(begin(contacts), end(contacts), val2)); it != ite; ++it)
{
if (regex_search(it->begin(), it->end(), expr)) {
cout << *it << endl;
}
}
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.