How using size_t as a parameter works
So I am making my first attempt in making a hash table and am completely stumped as to what to do for my constructors?
class HashTable
typedef vector <list<HashNode> > Table;
Table *table; // size of table is stored in the Table data structure
size_t num; // number of entries in the HashTable;
this is the class I'm trying to make constructors for and I have been given.
public:
HashTable(); // constructor, initializes table of size 1;
HashTable(size_t num); // constructor, requires size of table as arg
I don't really understand as to what am supposed to do. So first I'm to initialize the size of the table to 1. Which since I'm using a vector, I'm assuming I'm to use the reserve function? So I went ahead and got this:
this->table->resize(num);
which compiled fine and I had no issue with and I think is correct. The second constructor is where I had an issue understanding, mainly due to the use of size_t. By my understanding here is where I set the size of the table to size_t? But what is size_t supposed to be? I think its a data type but why would I set the Hashtable size to a datatype? Unless I'm missing something obvious here I can't see.
c++ hashtable size-t
|
show 4 more comments
So I am making my first attempt in making a hash table and am completely stumped as to what to do for my constructors?
class HashTable
typedef vector <list<HashNode> > Table;
Table *table; // size of table is stored in the Table data structure
size_t num; // number of entries in the HashTable;
this is the class I'm trying to make constructors for and I have been given.
public:
HashTable(); // constructor, initializes table of size 1;
HashTable(size_t num); // constructor, requires size of table as arg
I don't really understand as to what am supposed to do. So first I'm to initialize the size of the table to 1. Which since I'm using a vector, I'm assuming I'm to use the reserve function? So I went ahead and got this:
this->table->resize(num);
which compiled fine and I had no issue with and I think is correct. The second constructor is where I had an issue understanding, mainly due to the use of size_t. By my understanding here is where I set the size of the table to size_t? But what is size_t supposed to be? I think its a data type but why would I set the Hashtable size to a datatype? Unless I'm missing something obvious here I can't see.
c++ hashtable size-t
It is possible you are looking for the under-taught and very-important Member Initiallizer List. EgHashTable::HashTable(size_t size): table(new Table(size)), num(0).
– user4581301
Nov 15 '18 at 3:43
2
Note:Table *tableborders on being an incredibly stupid thing to do. The entire point ofstd::vectoris to remove the burden of manual memory management from the programmer. Dynamically allocating avectorthrusts this responsibility back onto them, defeating the point. You almost never want to dynamically allocate avector. I don't think I ever have.
– user4581301
Nov 15 '18 at 3:46
2
In addition to having todeletethevectorat some point, one bit of nastiness this pointer drops in your lap is the Rules of Three and Five.vectorobserves the Rule of Five for you, allowing your structures to letvectordo the dirty work and observe the Rule of Zero--do nothing. With a pointer to avector, you're now responsible for making certain that thevectoris properly handled whenHashTableis copied, moved, or assigned.
– user4581301
Nov 15 '18 at 3:50
1
Your instructor has probably set this up to teach you a difficult lesson. The alternative is the instructor should not be allowed to teach C++. Pray for the first and get yourself some alternate learning materials in case they're the second.
– user4581301
Nov 15 '18 at 3:51
1
Make sure you understand the difference betweenstd::vector::reserveandstd::vector::resize. I suspect you would prefer the second for this case, but you're better off constructing thevectorwith the correct size and save yourself the trouble of resizing later.
– user4581301
Nov 15 '18 at 4:05
|
show 4 more comments
So I am making my first attempt in making a hash table and am completely stumped as to what to do for my constructors?
class HashTable
typedef vector <list<HashNode> > Table;
Table *table; // size of table is stored in the Table data structure
size_t num; // number of entries in the HashTable;
this is the class I'm trying to make constructors for and I have been given.
public:
HashTable(); // constructor, initializes table of size 1;
HashTable(size_t num); // constructor, requires size of table as arg
I don't really understand as to what am supposed to do. So first I'm to initialize the size of the table to 1. Which since I'm using a vector, I'm assuming I'm to use the reserve function? So I went ahead and got this:
this->table->resize(num);
which compiled fine and I had no issue with and I think is correct. The second constructor is where I had an issue understanding, mainly due to the use of size_t. By my understanding here is where I set the size of the table to size_t? But what is size_t supposed to be? I think its a data type but why would I set the Hashtable size to a datatype? Unless I'm missing something obvious here I can't see.
c++ hashtable size-t
So I am making my first attempt in making a hash table and am completely stumped as to what to do for my constructors?
class HashTable
typedef vector <list<HashNode> > Table;
Table *table; // size of table is stored in the Table data structure
size_t num; // number of entries in the HashTable;
this is the class I'm trying to make constructors for and I have been given.
public:
HashTable(); // constructor, initializes table of size 1;
HashTable(size_t num); // constructor, requires size of table as arg
I don't really understand as to what am supposed to do. So first I'm to initialize the size of the table to 1. Which since I'm using a vector, I'm assuming I'm to use the reserve function? So I went ahead and got this:
this->table->resize(num);
which compiled fine and I had no issue with and I think is correct. The second constructor is where I had an issue understanding, mainly due to the use of size_t. By my understanding here is where I set the size of the table to size_t? But what is size_t supposed to be? I think its a data type but why would I set the Hashtable size to a datatype? Unless I'm missing something obvious here I can't see.
c++ hashtable size-t
c++ hashtable size-t
edited Nov 15 '18 at 7:17
Shrikanth N
524212
524212
asked Nov 15 '18 at 3:27
JohnJohn
54
54
It is possible you are looking for the under-taught and very-important Member Initiallizer List. EgHashTable::HashTable(size_t size): table(new Table(size)), num(0).
– user4581301
Nov 15 '18 at 3:43
2
Note:Table *tableborders on being an incredibly stupid thing to do. The entire point ofstd::vectoris to remove the burden of manual memory management from the programmer. Dynamically allocating avectorthrusts this responsibility back onto them, defeating the point. You almost never want to dynamically allocate avector. I don't think I ever have.
– user4581301
Nov 15 '18 at 3:46
2
In addition to having todeletethevectorat some point, one bit of nastiness this pointer drops in your lap is the Rules of Three and Five.vectorobserves the Rule of Five for you, allowing your structures to letvectordo the dirty work and observe the Rule of Zero--do nothing. With a pointer to avector, you're now responsible for making certain that thevectoris properly handled whenHashTableis copied, moved, or assigned.
– user4581301
Nov 15 '18 at 3:50
1
Your instructor has probably set this up to teach you a difficult lesson. The alternative is the instructor should not be allowed to teach C++. Pray for the first and get yourself some alternate learning materials in case they're the second.
– user4581301
Nov 15 '18 at 3:51
1
Make sure you understand the difference betweenstd::vector::reserveandstd::vector::resize. I suspect you would prefer the second for this case, but you're better off constructing thevectorwith the correct size and save yourself the trouble of resizing later.
– user4581301
Nov 15 '18 at 4:05
|
show 4 more comments
It is possible you are looking for the under-taught and very-important Member Initiallizer List. EgHashTable::HashTable(size_t size): table(new Table(size)), num(0).
– user4581301
Nov 15 '18 at 3:43
2
Note:Table *tableborders on being an incredibly stupid thing to do. The entire point ofstd::vectoris to remove the burden of manual memory management from the programmer. Dynamically allocating avectorthrusts this responsibility back onto them, defeating the point. You almost never want to dynamically allocate avector. I don't think I ever have.
– user4581301
Nov 15 '18 at 3:46
2
In addition to having todeletethevectorat some point, one bit of nastiness this pointer drops in your lap is the Rules of Three and Five.vectorobserves the Rule of Five for you, allowing your structures to letvectordo the dirty work and observe the Rule of Zero--do nothing. With a pointer to avector, you're now responsible for making certain that thevectoris properly handled whenHashTableis copied, moved, or assigned.
– user4581301
Nov 15 '18 at 3:50
1
Your instructor has probably set this up to teach you a difficult lesson. The alternative is the instructor should not be allowed to teach C++. Pray for the first and get yourself some alternate learning materials in case they're the second.
– user4581301
Nov 15 '18 at 3:51
1
Make sure you understand the difference betweenstd::vector::reserveandstd::vector::resize. I suspect you would prefer the second for this case, but you're better off constructing thevectorwith the correct size and save yourself the trouble of resizing later.
– user4581301
Nov 15 '18 at 4:05
It is possible you are looking for the under-taught and very-important Member Initiallizer List. Eg
HashTable::HashTable(size_t size): table(new Table(size)), num(0) .– user4581301
Nov 15 '18 at 3:43
It is possible you are looking for the under-taught and very-important Member Initiallizer List. Eg
HashTable::HashTable(size_t size): table(new Table(size)), num(0) .– user4581301
Nov 15 '18 at 3:43
2
2
Note:
Table *table borders on being an incredibly stupid thing to do. The entire point of std::vector is to remove the burden of manual memory management from the programmer. Dynamically allocating a vector thrusts this responsibility back onto them, defeating the point. You almost never want to dynamically allocate a vector. I don't think I ever have.– user4581301
Nov 15 '18 at 3:46
Note:
Table *table borders on being an incredibly stupid thing to do. The entire point of std::vector is to remove the burden of manual memory management from the programmer. Dynamically allocating a vector thrusts this responsibility back onto them, defeating the point. You almost never want to dynamically allocate a vector. I don't think I ever have.– user4581301
Nov 15 '18 at 3:46
2
2
In addition to having to
delete the vector at some point, one bit of nastiness this pointer drops in your lap is the Rules of Three and Five. vector observes the Rule of Five for you, allowing your structures to let vector do the dirty work and observe the Rule of Zero--do nothing. With a pointer to a vector, you're now responsible for making certain that the vector is properly handled when HashTable is copied, moved, or assigned.– user4581301
Nov 15 '18 at 3:50
In addition to having to
delete the vector at some point, one bit of nastiness this pointer drops in your lap is the Rules of Three and Five. vector observes the Rule of Five for you, allowing your structures to let vector do the dirty work and observe the Rule of Zero--do nothing. With a pointer to a vector, you're now responsible for making certain that the vector is properly handled when HashTable is copied, moved, or assigned.– user4581301
Nov 15 '18 at 3:50
1
1
Your instructor has probably set this up to teach you a difficult lesson. The alternative is the instructor should not be allowed to teach C++. Pray for the first and get yourself some alternate learning materials in case they're the second.
– user4581301
Nov 15 '18 at 3:51
Your instructor has probably set this up to teach you a difficult lesson. The alternative is the instructor should not be allowed to teach C++. Pray for the first and get yourself some alternate learning materials in case they're the second.
– user4581301
Nov 15 '18 at 3:51
1
1
Make sure you understand the difference between
std::vector::reserve and std::vector::resize. I suspect you would prefer the second for this case, but you're better off constructing the vector with the correct size and save yourself the trouble of resizing later.– user4581301
Nov 15 '18 at 4:05
Make sure you understand the difference between
std::vector::reserve and std::vector::resize. I suspect you would prefer the second for this case, but you're better off constructing the vector with the correct size and save yourself the trouble of resizing later.– user4581301
Nov 15 '18 at 4:05
|
show 4 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311985%2fhow-using-size-t-as-a-parameter-works%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311985%2fhow-using-size-t-as-a-parameter-works%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It is possible you are looking for the under-taught and very-important Member Initiallizer List. Eg
HashTable::HashTable(size_t size): table(new Table(size)), num(0).– user4581301
Nov 15 '18 at 3:43
2
Note:
Table *tableborders on being an incredibly stupid thing to do. The entire point ofstd::vectoris to remove the burden of manual memory management from the programmer. Dynamically allocating avectorthrusts this responsibility back onto them, defeating the point. You almost never want to dynamically allocate avector. I don't think I ever have.– user4581301
Nov 15 '18 at 3:46
2
In addition to having to
deletethevectorat some point, one bit of nastiness this pointer drops in your lap is the Rules of Three and Five.vectorobserves the Rule of Five for you, allowing your structures to letvectordo the dirty work and observe the Rule of Zero--do nothing. With a pointer to avector, you're now responsible for making certain that thevectoris properly handled whenHashTableis copied, moved, or assigned.– user4581301
Nov 15 '18 at 3:50
1
Your instructor has probably set this up to teach you a difficult lesson. The alternative is the instructor should not be allowed to teach C++. Pray for the first and get yourself some alternate learning materials in case they're the second.
– user4581301
Nov 15 '18 at 3:51
1
Make sure you understand the difference between
std::vector::reserveandstd::vector::resize. I suspect you would prefer the second for this case, but you're better off constructing thevectorwith the correct size and save yourself the trouble of resizing later.– user4581301
Nov 15 '18 at 4:05