将变量(从命令行)传递到 Class 构造函数
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 放在 main 函数之外(即在文件顶部)来使它具有全局范围。但是您真正应该做的是在 HashMap class.
的构造函数中传递 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
一个构造函数,该构造函数接受一个提供所需大小的参数。
将 array_size
参数添加到 HashMap
构造函数,并在知道所需数组的大小后实例化 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
or std::stoul
。两者都只接受正数,并且很容易测试超出范围的值和无效的输入字符。
将array_size
作为成员变量存储在HashMap
中也是一个好主意。 class 应该包含并保护它所依赖的所有信息。
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
。你不能有一个负数大小的数组,那么为什么还要允许这种可能性呢?如果提供了负值,编译器会在启用足够警告的情况下捕获错误。
并强烈考虑将 HashEntry **
替换为 std::vector
. With the current pointer approach you pick up a lot of memory management that you are not currently performing. Your program leaks memory like a sieve. The vector also would save you from violating the Rule of Three。
我在传递使用命令行从用户处获取的变量 (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 放在 main 函数之外(即在文件顶部)来使它具有全局范围。但是您真正应该做的是在 HashMap class.
#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
一个构造函数,该构造函数接受一个提供所需大小的参数。
将 array_size
参数添加到 HashMap
构造函数,并在知道所需数组的大小后实例化 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
or std::stoul
。两者都只接受正数,并且很容易测试超出范围的值和无效的输入字符。
将array_size
作为成员变量存储在HashMap
中也是一个好主意。 class 应该包含并保护它所依赖的所有信息。
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
。你不能有一个负数大小的数组,那么为什么还要允许这种可能性呢?如果提供了负值,编译器会在启用足够警告的情况下捕获错误。
并强烈考虑将 HashEntry **
替换为 std::vector
. With the current pointer approach you pick up a lot of memory management that you are not currently performing. Your program leaks memory like a sieve. The vector also would save you from violating the Rule of Three。