将变量(从命令行)传递到类构造函数
Passing variable (from command line) into Class Constructor
我在使用命令行从用户那里传递变量(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
不善于处理这个问题。请改用strtoul
或std::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 **
。使用当前指针方法,您可以获得大量当前未执行的内存管理。你的程序像筛子一样泄漏内存。矢量还可以使你免于违反"三条规则"。
- 没有用于初始化C++中的变量模板的匹配构造函数
- 使用仅使用一次的变量调用的复制构造函数.这可能是通过调用move构造函数进行编译器优化的情况吗
- 在类构造函数中使用结构变量
- 在 c++ 中将变量作为结构构造函数中的引用传递
- 在类构造函数中定义结构变量的参数
- 修改程序的入口点时未调用全局变量的构造函数
- C++:将向量传递到构造函数以创建成员变量的最佳方法?
- 为什么从另一个构造函数内部调用C++构造函数不修改类变量?
- 在 C++ 中声明 const 对象需要用户定义的默认构造函数.如果我有一个可变成员变量,为什么不呢?
- 构造函数干扰成员变量指定的初始值设定项?
- 类中的数组变量C++导致"was not declared in this scope"实现文件的一个函数中错误,但在构造函数中不会导致错误
- 通过 C++ 中的重载构造函数初始化未知类型的变量
- 如何在初始化列表中的构造函数之后初始化变量/对象?
- 我可以使用在类构造函数中初始化的流类型的成员变量吗?
- 为什么 std::move 不将默认移动构造函数中的源变量更改为默认值?
- C++为具有引用成员变量的类创建复制构造函数
- 如何在全局变量的构造函数之前运行函数
- C++/Win32 构造函数不使用从对话框获取的字符串初始化变量
- 将内置类型变量传递给只有一个类类型参数的"+"运算符函数时自动类型转换的构造函数
- std::线程构造函数(变量的数量)