首页 > 解决方案 > 如何在 C++ 中打印字典?这是我正在运行的代码,我无法使用 printDicionary 方法

问题描述

这是我正在运行的代码,我无法使用 printDicionary 方法。我不确定如何使用我创建的向量,我想知道如何以这种方式打印所有元素:

[0]:

[1]: (1501, “adiós”), (301, “nuevo”)

[2]:

[3]: (23, “perro”)

…</p>

[5]:(15,“你好”)

…</p>

[9]:(9,“加图”)

    #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; 

我不确定这是否是我应该调用 dict 的方式

        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();
}

标签: c++dictionaryvector

解决方案


你的代码有很多错误,伙计。

Dictionary 的“kp”成员是一个指针(它有那个“*”星号),所以必须使用“->”来访问它的成员,例如“.at(i)”,它与 [i ] 或 ".push_back(i)" 在向量的末尾添加一个元素。

我已将您的原始代码放在注释中并用正确的代码替换它,这样您就可以看到我做了哪些更改。问我你是否想知道更多关于我改变了什么,兄弟。祝你好运!

#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++)
        for (int i = 0; i < dict->size(); i++)
        {
            //if (dict[hash][i].key == key)
            if (dict->at(i).key == key)
            {
                cout << "llave ya existente" << endl;
                return;
            }
        }

        //dict[hash].push_back(kp);
        dict->push_back(kp);
    }

    V checkAtKey(K key)
    {
        //int hash = key % positions;

        //for (int i = 0; i < dict[hash].size(); i++)   
        for (int i = 0; i < dict->size(); i++)
        {
            //if (dict[hash][i].key == key)
            if (dict->at(i).key == key)
            {
                //return dict[hash][i].value;
                return dict->at(i).value;
            }
        }

        return NULL;
    }

    void printDictionary()
    {
        //for (int i = 0; i < dict[i].size(); i++){
        for (int i = 0; i < dict->size(); i++)
        {
            // cout << dict.value << endl;
            KeyPair<K, V> curr_key = dict->at(i);
            printf("%d - %s\n", curr_key.key, curr_key.value.c_str());
            // or you can do this
            // std::cout << curr_key.key << " - " << curr_key.value.c_str();
        }
    }
 };

int main()
{
    Dictionary<int, string> a;
    a.insert(5, "perro");
    a.insert(4, "gato");
    Dictionary<int, string> b;
    b.insert(15, "lombriz");

    b.printDictionary();

    return 0;
}

推荐阅读