中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

Asp做網(wǎng)站前期準(zhǔn)備互聯(lián)網(wǎng)營銷師有什么用

Asp做網(wǎng)站前期準(zhǔn)備,互聯(lián)網(wǎng)營銷師有什么用,怎樣做商城網(wǎng)站的推廣,網(wǎng)站建設(shè)開發(fā)環(huán)境散列表(Hash Table)是一種高效的數(shù)據(jù)結(jié)構(gòu),廣泛用于實(shí)現(xiàn)快速的鍵值對存儲。 基本概念 散列表使用哈希函數(shù)將鍵映射到數(shù)組的索引。其主要優(yōu)點(diǎn)在于平均情況下提供常數(shù)時(shí)間復(fù)雜度的查找、插入和刪除操作。 哈希函數(shù): 將鍵映射到一個(gè)固定大小的…

散列表(Hash Table)是一種高效的數(shù)據(jù)結(jié)構(gòu),廣泛用于實(shí)現(xiàn)快速的鍵值對存儲。

基本概念

散列表使用哈希函數(shù)將鍵映射到數(shù)組的索引。其主要優(yōu)點(diǎn)在于平均情況下提供常數(shù)時(shí)間復(fù)雜度的查找、插入和刪除操作。

  • 哈希函數(shù): 將鍵映射到一個(gè)固定大小的數(shù)組索引。一個(gè)好的哈希函數(shù)應(yīng)該具備:
    • 散列均勻性:不同的鍵應(yīng)該盡量映射到不同的索引。
    • 計(jì)算簡單:哈希值的計(jì)算應(yīng)該高效。

沖突處理

由于多個(gè)鍵可能映射到同一個(gè)索引,必須采取措施處理沖突。常見的沖突解決方法包括:

  • 鏈地址法: 在每個(gè)數(shù)組索引處使用鏈表存儲所有映射到該索引的鍵值對。
  • 開放尋址法: 在數(shù)組中查找下一個(gè)可用位置,例如線性探測、二次探測和雙重散列等方法。

性能分析

時(shí)間復(fù)雜度:

  • 查找:O(1)(平均情況),O(n)(最壞情況,發(fā)生沖突時(shí))
  • 插入:O(1)(平均情況),O(n)(最壞情況)
  • 刪除:O(1)(平均情況),O(n)(最壞情況)

空間復(fù)雜度: O(n),即存儲元素的數(shù)量。

負(fù)載因子(Load Factor): 定義為元素?cái)?shù)量與表大小的比率。一般在負(fù)載因子超過一定閾值(如0.7)時(shí)進(jìn)行擴(kuò)容,以保持性能。

代碼實(shí)現(xiàn)

鏈地址法

#include <iostream>
#include <vector>
#include <list>
#include <utility> // for std::pair
#include <functional> // for std::hashtemplate <typename Key, typename Value>
class HashTable {
public:HashTable(size_t size = 10) : table(size), current_size(0) {}void Insert(const Key& key, const Value& value) {size_t index = Hash_(key) % table.size();for (auto& pair : table[index]) {if (pair.first == key) {pair.second = value; // 更新值return;}}table[index].emplace_back(key, value); // 插入新鍵值對current_size++;if (current_size > table.size() * load_factor) {Resize_();}}bool Get(const Key& key, Value& value) const {size_t index = Hash_(key) % table.size();for (const auto& pair : table[index]) {if (pair.first == key) {value = pair.second;return true;}}return false; // 未找到}bool Remove(const Key& key) {size_t index = Hash_(key) % table.size();auto& cell = table[index];for (auto it = cell.begin(); it != cell.end(); ++it) {if (it->first == key) {cell.erase(it); // 刪除current_size--;return true;}}return false; // 未找到}private:std::vector<std::list<std::pair<Key, Value>>> table; // 哈希表的數(shù)組size_t current_size; // 當(dāng)前存儲的元素?cái)?shù)量const float load_factor = 0.7; // 負(fù)載因子size_t Hash_(const Key& key) const {return std::hash<Key>()(key); // 使用標(biāo)準(zhǔn)哈希函數(shù)}void Resize_() {std::vector<std::list<std::pair<Key, Value>>> old_table = table;table.resize(old_table.size() * 2); // 擴(kuò)容current_size = 0;for (const auto& cell : old_table) {for (const auto& pair : cell) {Insert(pair.first, pair.second); // 重新插入}}}
};int main() {HashTable<std::string, int> hash_table;hash_table.Insert("apple", 1);hash_table.Insert("banana", 2);int value;if (hash_table.Get("apple", value)) {std::cout << "apple: " << value << std::endl; // 輸出: apple: 1}hash_table.Remove("apple");if (!hash_table.Get("apple", value)) {std::cout << "apple not found" << std::endl; // 輸出: apple not found}return 0;
}

代碼解析

  1. 數(shù)據(jù)結(jié)構(gòu):
    • 使用 std::vector 存儲鏈表,鏈表用于處理沖突。
    • 每個(gè)鏈表中的元素是 std::pair<Key, Value>,用于存儲鍵值對。
  2. 插入操作:
    • 計(jì)算哈希值并確定索引。
    • 檢查索引處是否存在相同的鍵,如果存在則更新值,否則插入新鍵值對。
    • 如果當(dāng)前元素個(gè)數(shù)超過負(fù)載因子,則調(diào)用 Resize 擴(kuò)容。
  3. 查找操作:
    • 計(jì)算索引,遍歷鏈表查找對應(yīng)的鍵。
  4. 刪除操作:
    • 計(jì)算索引并在鏈表中查找鍵,找到后刪除。
  5. 擴(kuò)容:
    • 創(chuàng)建新的、更大的表,重新插入舊表中的元素以保證均勻分布。

開放尋址法

#include <iostream>
#include <vector>
#include <utility> // for std::pair
#include <stdexcept> // for std::out_of_rangetemplate <typename Key, typename Value>
class HashTable {
public:HashTable(size_t size = 10) : table(size), current_size(0), load_factor(0.7) {}void Insert(const Key& key, const Value& value) {if (current_size >= table.size() * load_factor) {Resize_();}size_t index = Hash_(key) % table.size();while (table[index].first != Key() && table[index].first != key) {index = (index + 1) % table.size(); // 線性探測}table[index] = { key, value };current_size++;}bool Get(const Key& key, Value& value) const {size_t index = Hash_(key) % table.size();while (table[index].first != Key()) {if (table[index].first == key) {value = table[index].second;return true;}index = (index + 1) % table.size(); // 線性探測}return false; // 未找到}bool Remove(const Key& key) {size_t index = Hash_(key) % table.size();while (table[index].first != Key()) {if (table[index].first == key) {table[index] = { Key(), Value() }; // 標(biāo)記為刪除current_size--;return true;}index = (index + 1) % table.size(); // 線性探測}return false; // 未找到}private:std::vector<std::pair<Key, Value>> table; // 散列表的數(shù)組size_t current_size; // 當(dāng)前存儲的元素?cái)?shù)量const float load_factor; // 負(fù)載因子size_t Hash_(const Key& key) const {return std::hash<Key>()(key); // 使用標(biāo)準(zhǔn)哈希函數(shù)}void Resize_() {std::vector<std::pair<Key, Value>> old_table = table;table.resize(old_table.size() * 2, { Key(), Value() }); // 擴(kuò)容current_size = 0;for (const auto& pair : old_table) {if (pair.first != Key()) {Insert(pair.first, pair.second); // 重新插入}}}
};int main() {HashTable<std::string, int> hash_table;hash_table.Insert("apple", 1);hash_table.Insert("banana", 2);int value;if (hash_table.Get("apple", value)) {std::cout << "apple: " << value << std::endl; // 輸出: apple: 1}hash_table.Remove("apple");if (!hash_table.Get("apple", value)) {std::cout << "apple not found" << std::endl; // 輸出: apple not found}return 0;
}

代碼解析

  1. 數(shù)據(jù)結(jié)構(gòu):
    • 使用 std::vector<std::pair<Key, Value>> 存儲鍵值對。未使用的槽位初始化為 Key()Value(),用于標(biāo)記空槽。
  2. 插入操作:
    • 計(jì)算哈希值并確定初始索引。
    • 如果發(fā)生沖突,使用線性探測法查找下一個(gè)可用的索引。
    • 如果當(dāng)前元素?cái)?shù)量超過負(fù)載因子,則調(diào)用 Resize 方法進(jìn)行擴(kuò)容。
  3. 查找操作:
    • 計(jì)算索引并線性探測,直到找到對應(yīng)的鍵或到達(dá)空槽。
  4. 刪除操作:
    • 在查找過程中,如果找到目標(biāo)鍵,則標(biāo)記該位置為已刪除。
  5. 擴(kuò)容:
    • 創(chuàng)建一個(gè)更大的數(shù)組并重新插入舊表中的元素,以保持均勻分布。

總結(jié)

散列表是一種高效且靈活的數(shù)據(jù)結(jié)構(gòu),適合用于需要快速查找和存儲的場景。通過合理設(shè)計(jì)哈希函數(shù)和沖突處理策略,可以實(shí)現(xiàn)良好的性能。

http://www.risenshineclean.com/news/29397.html

相關(guān)文章:

  • 自己做網(wǎng)站可以掙錢嗎windows優(yōu)化大師怎么使用
  • 給客戶做網(wǎng)站需要付法律責(zé)任嗎長春網(wǎng)站seo
  • 草坪網(wǎng)站怎么做幫忙推廣的平臺
  • 單頁面網(wǎng)站怎么做的網(wǎng)站流量數(shù)據(jù)
  • 租服務(wù)器做網(wǎng)站app推廣代理加盟
  • 裝飾網(wǎng)站建設(shè)策劃書中山網(wǎng)站建設(shè)
  • 百度制作公司網(wǎng)頁常州百度搜索優(yōu)化
  • 如何做外賣網(wǎng)站app東莞百度搜索網(wǎng)站排名
  • 自己如何免費(fèi)做網(wǎng)站重慶營銷型網(wǎng)站建設(shè)公司
  • wordpress網(wǎng)站發(fā)布時(shí)間網(wǎng)絡(luò)推廣求職招聘交流群
  • 電子商務(wù)網(wǎng)站建設(shè)的過程和步驟廣告聯(lián)盟廣告點(diǎn)擊一次多少錢
  • 微信做單子的網(wǎng)站源碼搜什么關(guān)鍵詞能找到網(wǎng)站
  • 消防做設(shè)計(jì)有什么網(wǎng)站無錫網(wǎng)站建設(shè)優(yōu)化公司
  • 做美女網(wǎng)站賺錢千峰培訓(xùn)可靠嗎?
  • 媒體公關(guān)廈門谷歌seo
  • 網(wǎng)絡(luò)公司開發(fā)網(wǎng)站今日nba數(shù)據(jù)帝
  • 免費(fèi)企業(yè)建站開源系統(tǒng)企業(yè)培訓(xùn)師資格證
  • html5網(wǎng)站編寫青島網(wǎng)站制作推廣
  • 網(wǎng)站可以做無形資產(chǎn)嗎優(yōu)化網(wǎng)站排名需要多少錢
  • 云表無代碼開發(fā)平臺baike seotl
  • 網(wǎng)站備案值得嗎打廣告的免費(fèi)軟件
  • 專業(yè)做化妝品的網(wǎng)站在線生成html網(wǎng)頁
  • 怎么做淘寶客網(wǎng)站做淘客拼多多代運(yùn)營一般多少錢
  • 佰匯康網(wǎng)站建設(shè)河南seo快速排名
  • 南京網(wǎng)站建設(shè)案例蘇州做網(wǎng)站哪家比較好
  • 嘉興網(wǎng)站制作軟件如何做一個(gè)網(wǎng)站
  • 有沒有幫人做簡歷的網(wǎng)站營銷型網(wǎng)站和普通網(wǎng)站
  • 只做襯衣網(wǎng)站百度seo排名優(yōu)化技巧分享
  • 帝國cms企業(yè)網(wǎng)站模板網(wǎng)站優(yōu)化方案案例
  • 怎么做網(wǎng)站能夠增加人氣鏈接提交入口