Reputation: 5554
I want to implement a hashmap of vectors in C++. Here is my code:
#include <cstdlib>
#include <string>
#include <iostream>
#include <vector>
using namespace std;
#include <ext/hash_map>
using namespace __gnu_cxx;
int main (int argc, char * const argv[]) {
std::vector<int> v;
hash_map<int, std::vector<int> > months;
v.push_back(28);
v.push_back(28);
v.push_back(28);
v.push_back(29);
months["february"] = v; //error = invlalid conversion from const char* to int
return 0;
}
The above code fails to compile. Errors included next to relevant lines. Does it have anything to do with me leaving out values for the optional arguments (hash and comparison)?
Upvotes: 1
Views: 12154
Reputation: 106
Initialise a integer vector (v) first. and Intialize a map (months) of string and vector. Then try appending the values to the vector. And assign the int vector to the "February" key of the map months.
map // Instead of hash_map
string // Instead of int
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
using namespace std;
int main()
{
std::vector<int> v;
map<std::string, std::vector<int> > months;
// Must mention the datatype of elements in the map
v.push_back(28);
v.push_back(28);
v.push_back(28);
v.push_back(29);
// v = { 28, 28, 28, 29 }
months["February"] = v;
// months = { "February" : { 28, 28, 29, 29 } }
for(int i=0; i<months["February"].size(); i++){
cout << months["February"][i] << " ";
}
return 0;
}
Upvotes: 0
Reputation: 1501
Different than the 5 other answers that are all the same. There are a fixed number of months. It would be unnecessary to store them by a string. An enum
could help you refer to them by name. This would be much more efficient, but not a pre-optimization, because it also makes more sense. A typo like "feburary" in the string version would add a new month! If it were an enum
you would get a compile error, plus if your editor uses auto-completion, you won't have to look up how to spell februrary.
enum { JANUARY=0, FEBRUARY,MARCH,APRIL,MAY,JUNE,JULY,AUGUST,SEPTEMBER,OCTOBER,NOVEMBER,DECEMBER};
months[FEBRUARY] = v;
You could place it in a namespace if you didn't want to pollute the global one. And it would be easier to make them lowercase at this point.
namespace Month {
enum { January=0, February,March,April,May,June,July,August,September,October,November,December};
}
months[Month::February] = v; // equivalent to months[1]=v if you prefer number
You could also start the enum
at 1 if you wanted to keep January being 1 instead of February. That may make more sense to you. I think the general public should refer to January as 0, but maybe that's just me. The 0 could be changed to NULL_MONTH or INVALID_MONTH, or whatever fits your design.
Upvotes: 3
Reputation: 32661
The issue is that months is a hash_map whose key is an int. in months["february"]
you are passing a char const *
You probably want to be using a syd::string as the key.
e.g.
hash_map<std::string, std::vector<int> > months;
Upvotes: 1
Reputation: 42597
hash_map<int, std::vector<int> > months;
The key to this hashmap is an int, but you're trying to put the string "february" in it.
Upvotes: 1
Reputation: 20272
The key is int
, and you're giving a string. Change the declaration to:
hash_map<std::string, std::vector<int> > months;
Upvotes: 1
Reputation: 272467
You've specified the key type as int
, but you're trying to supply a string literal...
Upvotes: 3
Reputation: 44093
Your hashmap is using integer keys rather than strings. This should fix it:
hash_map<std::string, std::vector<int> > months;
Upvotes: 4