将变量(从命令行)传递到类构造函数

Passing variable (from command line) into Class Constructor

本文关键字:构造函数 变量 命令行      更新时间:2023-10-16

我在使用命令行从用户那里传递变量(array_size)时遇到问题。我需要将这个变量传递给HashMap构造函数。在构造函数中,如果我将"array_size"更改为f.e 1000,它是有效的,但我需要它是"variable":)这是我的代码,如果有任何帮助,我将不胜感激。干杯

#include<iostream>
#include<cstdlib>
using namespace std;
int counter = 1;
class HashEntry
{
    public:
        string key, value;
        HashEntry(string key, string value)
        {
            this->key = key;
            this->value = value;
        }
};
class HashMap
{
    private:
        HashEntry **table;
    public:
        HashMap()
        {
            table = new HashEntry*[array_size];
            for (int i = 0; i < array_size; i++)
            table[i] = NULL;
        }
        void put(string key, string value, int option, int array_size)
        {
            int _key = atoi(key.c_str());
            int hash = _key;
            if(option == 1)
            {
                while (table[hash] != NULL && table[hash]->key != key)
                {
                    counter++;
                    hash = (hash + 10);
                        }
                hash = hash  % array_size;
            }
            else if(option == 2)
            {
                while (table[hash] != NULL && table[hash]->key != key)
                {
                    counter++;
                    hash = (hash + 10 + counter*counter) % array_size;
                        }
            }
            else if(option == 3)
            {
                while (table[hash] != NULL && table[hash]->key != key)
                {
                    counter++;
                    hash = (hash + counter*(_key%(array_size-2)+1));
                    if(hash >= array_size)
                    {
                        hash = 0;
                    }
                }
                hash = hash % array_size;
            }
            if(table[hash] == NULL)
            {
                table[hash] = new HashEntry(key, value);
            }           
            else
                    {
                if (table[hash] != NULL && table[hash]->key == key)
                {
                    table[hash]->value;
                }               
                else
                {                       
                    table[hash] = new HashEntry(key, value);
                }
            }   
        }
};
int main(int argc, char* argv[])
{
    HashMap map;
    string key, value;
    int array_size;
    array_size = atoi(argv[2]);
    int option = atoi(argv[1]);
    int records;
    cin>>records;
    for(int x = 0; x<records; x++)
    {
        cin >> key;
        cin >> value;
        map.put(key, value, option, array_size);
    }
    cout << counter << endl;
    return 0;
}

您的array_size变量在main函数的作用域中。如果您想让这些代码正常工作,可以将array_size放在主函数之外(即放在文件的顶部),使其具有全局作用域。但真正应该做的是在HashMap类的构造函数中传递array_size。

#include<iostream>
#include<cstdlib>
using namespace std;
int counter = 1;
class HashEntry
{
    public:
        string key, value;
        HashEntry(string key, string value)
        {
            this->key = key;
            this->value = value;
        }
};
class HashMap
{
    private:
        HashEntry **table;
        int array_size;
    public:
        HashMap(int size) :
            array_size(size)
        {
            table = new HashEntry*[array_size];
            for (int i = 0; i < array_size; i++)
            table[i] = NULL;
        }
        void put(string key, string value, int option, int array_size)
        {
            int _key = atoi(key.c_str());
            int hash = _key;
            if(option == 1)
            {
                while (table[hash] != NULL && table[hash]->key != key)
                {
                    counter++;
                    hash = (hash + 10);
                        }
                hash = hash  % array_size;
            }
            else if(option == 2)
            {
                while (table[hash] != NULL && table[hash]->key != key)
                {
                    counter++;
                    hash = (hash + 10 + counter*counter) % array_size;
                        }
            }
            else if(option == 3)
            {
                while (table[hash] != NULL && table[hash]->key != key)
                {
                    counter++;
                    hash = (hash + counter*(_key%(array_size-2)+1));
                    if(hash >= array_size)
                    {
                        hash = 0;
                    }
                }
                hash = hash % array_size;
            }
            if(table[hash] == NULL)
            {
                table[hash] = new HashEntry(key, value);
            }           
            else
                    {
                if (table[hash] != NULL && table[hash]->key == key)
                {
                    table[hash]->value;
                }               
                else
                {                       
                    table[hash] = new HashEntry(key, value);
                }
            }   
        }
};
int main(int argc, char* argv[])
{
    string key, value;
    int array_size;
    array_size = atoi(argv[2]);
    int option = atoi(argv[1]);
    int records;
    cin>>records;
    HashMap map(array_size);
    for(int x = 0; x<records; x++)
    {
        cin >> key;
        cin >> value;
        map.put(key, value, option, array_size);
    }
    cout << counter << endl;
    return 0;
}

叹气。没有人再学习初始化了吗?在知道HashMap对象应该有多大之前,不要创建它,并为HashMap提供一个构造函数,该构造函数接受一个提供所需大小的参数。

HashMap构造函数添加一个array_size参数,并在知道所需数组的大小后实例化map对象。

#include<iostream>
#include<cstdlib>
using namespace std;
int counter = 1;
class HashEntry {
public:
    string key, value;
    HashEntry (string key, string value)
    {
        this->key = key;
        this->value = value;
    }
};
class HashMap {
private:
    HashEntry **table;
public:
    HashMap (int array_size)    // Add array_size parameter to constructor
    {
        table = new HashEntry*[array_size];
        for (int i = 0; i < array_size; i++)
            table[i] = NULL;
    }
    void put (string key, string value, int option, int array_size)
    {
        int _key = atoi (key.c_str ());
        int hash = _key;
        if (option == 1) {
            while (table[hash] != NULL && table[hash]->key != key) {
                counter++;
                hash = (hash + 10);
            }
            hash = hash  % array_size;
        }
        else if (option == 2) {
            while (table[hash] != NULL && table[hash]->key != key) {
                counter++;
                hash = (hash + 10 + counter*counter) % array_size;
            }
        }
        else if (option == 3) {
            while (table[hash] != NULL && table[hash]->key != key) {
                counter++;
                hash = (hash + counter*(_key % (array_size - 2) + 1));
                if (hash >= array_size) {
                    hash = 0;
                }
            }
            hash = hash % array_size;
        }
        if (table[hash] == NULL) {
            table[hash] = new HashEntry (key, value);
        }
        else {
            if (table[hash] != NULL && table[hash]->key == key) {
                table[hash]->value;
            }
            else {
                table[hash] = new HashEntry (key, value);
            }
        }
    }
};
int main (int argc, char* argv[])
{
    string key, value;
    int array_size;
    array_size = atoi (argv[2]);
    int option = atoi (argv[1]);
    int records;
    cin >> records;
    HashMap map (array_size);   // Declare map using new constructor
    for (int x = 0; x<records; x++) {
        cin >> key;
        cin >> value;
        map.put (key, value, option, array_size);
    }
    cout << counter << endl;
    return 0;
}

函数中定义的变量,包括main,仅在该函数中可见。

您需要将大小从main传递到构造函数

HashMap(int array_size)
{
    table = new HashEntry*[array_size];
    for (int i = 0; i < array_size; i++)
    table[i] = NULL;
}

然后在main 中下降

array_size = atoi(argv[2]);
HashMap map(array_size); // now create the hashmap

但是

在没有确保用户输入良好的情况下,千万不要使用用户输入。如果用户提供的是"fubar"而不是数字,该怎么办?如果用户指定-10怎么办?还是14.998?atoi不善于处理这个问题。请改用strtoulstd::stoul。两者都只接受正数,并且很容易测试超出范围的值和无效输入字符。

array_size作为成员变量存储在HashMap中也是一个好主意。类应该包含并保护它所依赖的所有信息。

HashMap(unsigned int size): array_size(size)
{
    table = new HashEntry*[array_size];
    for (int i = 0; i < array_size; i++)
    table[i] = NULL;
}

和一个新的私人成员变量

unsigned int array_size;

请注意,它是unsigned。你不能有一个负大小的数组,为什么还要允许这种可能性呢?如果提供了负值,编译器在启用了足够的警告的情况下会发现错误。

强烈考虑用std::vector替换HashEntry **。使用当前指针方法,您可以获得大量当前未执行的内存管理。你的程序像筛子一样泄漏内存。矢量还可以使你免于违反"三条规则"。

相关文章: