#include <iostream>
using namespace std;
class item {
int size;
int value[30];
char key[30][20];
int n, k;
int index;
char* a[30], *b[30], search, remove;
public:
void putdatavalue(void) {
for (int i = 0; i < size; i++) {
cout << value[i] << ","
<< "\n";
}
}
void display(void) {
for (int i = 0; i < size; i++) {
cout << "(" << key[i] << "," << value[i] << ")"
<< "\n";
}
}
};
void item::getdata(void) {
cout << "entr size\n";
cin >> size;
for (int i = 0; i < size; i++) {
cout << "entr key\n";
cin >> key[i];
cout << "entr value\n";
cin >> value[i];
}
cout << "chk whether keys are different \n";
for (int i = 0; i < size; i++) {
a[i] = key[i];
b[i] = key[i + 1];
if (*a[i] == *b[i]) {
cout << "key" << i << "and key" << i + 1 << "are same\n";
cout << "re-entr key\n";
cin >> key[i + 1];
} else {
cout << "key[" << i << "] and key[" << i + 1 << "] are diff\n";
}
}
}
int main() {
item obj1;
obj1.getdata();
int m;
do {
cout << "choose ur option and enter appropriate no"
<< "\n";
cout << "\n1 : display keys \n";
cout << "\n2 : display value \n";
cout << "\n3 : display key-value pair \n";
cout << "\n4 : add a key-value pair \n";
cout << "\n5 : remove a key-value pair \n";
cout << "\n6 : search for key and its value \n";
cout << "\n7 : quit \n";
cin >> m;
switch (m) {
break;
case 1: {
obj1.putdatakey();
} break;
case 2: {
obj1.putdatavalue();
} break;
case 3: {
obj1.display();
} break;
case 7:
break;
default:
cout << "error in input \n";
}
} while (m != 7);
return 0;
}
我想創造+key-value
對用C具有以下功能的詞典的詞典: 顯示鍵,顯示值,顯示鍵 - 值對,加密鑰 - 值,刪除一個鍵值對,找出一個鍵是否存在並返回值。創建在C++的關鍵值對
字典應該可能爲空。
您應該重載operator+
做聯合兩個字典,如果字典每個包含相同的鍵,輸出一個錯誤消息。
我的老師讓我們不要使用strings
和vectors
所以我用數組 我堅持與聯合功能,不知道該怎麼做。 Plz幫助我。
如果你被允許,使用'std :: map' ... tada! – OMGtechy 2014-09-13 00:41:48
_「我的老師要求我們不要使用字符串和矢量」_ - 您應該要求退款。 – 2014-09-13 00:41:51
@Captain意識到它可能只是讓他們思考底層發生了什麼,併爲了學習而自己實現這些工具。雖然我承認在這個例子中看起來很奇怪。 – OMGtechy 2014-09-13 00:45:07