这是我正在运行的代码,我无法使用printDicionary方法。 我不确定如何使用我创建的矢量,我想知道如何以这种方式打印所有元素:
[0]:
[1] :( 1501,“adiós”),(301,“ nuevo”)
[2]:
[3] :( 23,“ perro”)
…
[5] :( 15,“ hola”)
…
[9] :( 9,“ gato”)
#include <iostream>
#include <vector>
using namespace std;
template <class K, class V>
class KeyPair{
public:
K key;
V value;
KeyPair(){
key=NULL;
value=NULL;
}
KeyPair(K key, V val){
this->key=key;
this->value=val;
}
};
template <class K, class V>
class Dictionary{
public:
vector<KeyPair<K,V> > *dict;
int positions;
Dictionary(){
positions=10;
dict=new vector<KeyPair<K,V> >[positions];
}
Dictionary(int pos){
positions=pos;
dict=new vector<KeyPair<K,V> >[positions];
}
void insert(K key, V value){
KeyPair<K,V> kp(key, value);
int hash=key%positions;
for(int i=0; i<dict[hash].size(); i++){
if(dict[hash][i].key==key){
cout<<"llave ya existente"<<endl;
return;
}
}
dict[hash].push_back(kp);
//dict//arreglo de vectores keypair
//dict[hash]//vector de keypair
}
V checkAtKey(K key){
int hash=key%positions;
for(int i=0; i<dict[hash].size(); i++){
if(dict[hash][i].key==key){
return dict[hash][i].value;
}
}
return NULL;
}
void printDictionary(){
这是我创建方法的地方
for(int i=0; i<dict[i].size(); i++){
cout << dict.key <<endl;
我不确定这是否是我打电话给字典的方式
cout << dict.value <<endl;
return;
}
}
};
int main(){
Dictionary<int, string> a;
a.insert(5, "perro");
a.insert(4, "gato");
Dictionary<int, string> b;
b.insert(15, "lombriz");
b.printDictionary();
}
伙计,您的代码中有很多错误。
Dictionary的“ kp”成员是一个指针(它带有“ *”星号),因此必须使用“->”来访问其成员,例如“ .at(i)”,该成员与[i相同]或“ .push_back(i)”在向量的末尾添加元素。
我已经将您的原始代码放入注释中,并用正确的代码替换了它,以便您可以看到我所做的更改。问我你是否想知道我的改变,兄弟。祝好运!