使用鍵更新字典值的C++程式
許多計算機語言都提供字典,這是一種資料結構。字典是一種更快的資料結構,它基於鍵和值儲存資料。它保留鍵值對,以便鍵可以幾乎即時地輕鬆搜尋特定元件。C++ STL語言標準包含類似字典的資料結構。“map”一詞用於描述這種資料結構。map建立任意型別的鍵值對(由於我們使用的是C++,因此必須在編譯前定義型別)。本節將演示如何在C++中更新已存在的map或字典中的值。
讓我們首先檢查map資料結構的定義。這些內部模板需要兩種不同的型別。以下是語法和必要的庫:
map資料結構的定義語法
#include
在這種情況下,我們必須匯入“map”庫才能使用map資料結構。這需要資料型別1和2。鍵引數的資料型別是type1,值引數的資料型別是type2。此處從map型別類派生的物件是mapVariable。現在讓我們看看如何使用C++ map來實現這一點。
在map資料結構中,我們可以透過訪問現有鍵或新鍵將值放入map中。由於這裡我們討論的是更新值,因此我們必須更新現有鍵。鍵將像陣列索引表示法一樣用方括號括起來。讓我們看看這方面的語法:
更新map中元素的語法
mapVariable [] = ;
演算法
一個已建立的字典或map D
已存在的鍵值k
新鍵k的值v
更新為 D[ k ] = v
返回 D
示例
#include <iostream>
#include <map>
using namespace std;
void display( map <string, int>& givenMap ){
for ( auto& it : givenMap ) {
cout << "Key: " << it.first << ", value: " << it.second << endl;
}
}
int main(){
map<string, int> givenMap;
givenMap = { { "ABCD", 25 },
{ "EFGH", 50 },
{ "IJKL", 75 },
{ "MNOP", 100 },
{ "QRST", 125 }
};
cout << "Before updation: " << endl;
display( givenMap );
cout << "After Updation: " << endl;
//update the value of MNOP to 500
givenMap[ "MNOP" ] = 500;
display( givenMap );
}
輸出
Before updation: Key: ABCD, value: 25 Key: EFGH, value: 50 Key: IJKL, value: 75 Key: MNOP, value: 100 Key: QRST, value: 125 After Updation: Key: ABCD, value: 25 Key: EFGH, value: 50 Key: IJKL, value: 75 Key: MNOP, value: 500 Key: QRST, value: 125
在這種方法中,我們已成功透過訪問鍵引數更新了值。但是,此過程並非一直準確。此過程有一個嚴重的缺點,即給定的鍵可能不存在於map中。但是,使用此過程將插入一個具有給定值的新鍵。因此,在下一種方法中,我們將看到如何在成功搜尋後搜尋和更新元素。
搜尋後更新
可以使用map物件中的find()函式檢查map中是否存在鍵。它將返回鍵的指標引用,否則將返回map的“end()”指標,這表示map中不包含該元素。讓我們看看演算法和實現以更好地理解。
演算法
一個已建立的字典或map D
已存在的鍵值k
新鍵k的值v
建立一個迭代器物件itr以獲取鍵值對的指標
呼叫字典D的find()方法到itr中
如果itr不是D的末尾,則表示鍵存在,然後
將v放入itr
結束if
示例
#include <iostream>
#include <map>
using namespace std;
void display( map <string, int>& givenMap ){
for ( auto& it : givenMap ) {
cout << "Key: " << it.first << ", value: " << it.second << endl;
}
}
void updateElement( map <string, int>& givenMap, string givenKey, int newValue ){
map <string, int>::iterator itr;
itr = givenMap.find( givenKey );
if( itr != givenMap.end() ){ // when item has found
itr->second = newValue;
}
}
int main(){
map<string, int> givenMap;
givenMap = { { "ABCD", 25 },
{ "EFGH", 50 },
{ "IJKL", 75 },
{ "MNOP", 100 },
{ "QRST", 125 }
};
cout << "Before updation: " << endl;
display( givenMap );
cout << "After Updation: " << endl;
//update the value of MNOP to 500
updateElement( givenMap, "MNOP", 1580 );
display( givenMap );
}
輸出
Before updation: Key: ABCD, value: 25 Key: EFGH, value: 50 Key: IJKL, value: 75 Key: MNOP, value: 100 Key: QRST, value: 125 After Updation: Key: ABCD, value: 25 Key: EFGH, value: 50 Key: IJKL, value: 75 Key: MNOP, value: 1580 Key: QRST, value: 125
在這種方法中,updateElement函式將map、現有鍵和newValue作為輸入。之後搜尋該鍵。如果存在,則只更新值,否則直接退出函式。因此,使用此方法,我們無法在map中建立新條目,而只能更新現有條目。
結論
在這篇文章中,我們瞭解瞭如何使用鍵更新map中的元素。在第一種方法中,我們使用直接賦值方法成功更新了元素,但當鍵不存在時,它也可以新增新元素。第二種方法透過在開始時進行簡單的搜尋來消除這個問題。有時我們可能會注意到,第二種方法需要額外的時間來搜尋鍵然後更新它。因此,它比第一種方法需要額外的搜尋時間。但是,如果我們仔細考慮,第一種方法中也隱含地進行了查詢。由於使用的結構使用了基於雜湊的技術,因此它將在恆定時間內執行(在大多數情況下)。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP