I'm forming a very primitive hash table. How do I create a vector where each individual index can lengthen into its own list? Is it as simple as, for example, vector<list<int>> ?
Also, if I want each node of the linked list to hold two datatypes (i.e. a string word and the integer line numbers of the file it can be found in), is it possible? I imagine not.
vecotr<list<int> > seems fine to me.
For you second question, you can use
typedef std::pair<std::string, int> Item;
std::vector<std::list<Item> >
The following works:
std::map<std::string, Animal*> animalMap;
animalMap["KillerRabbit"] = new KillerRabit;
But what if I wanted to do this?
animalMap["KillerRabbit"]["White"] = new KillerRabit;
I have no idea what the 'official' name for the indices brackets are, knowing them would help immensely while Googling =p
What you are looking for is a map of maps:
std::map<std::string, std::map<std::string, Animal*>> animalMap;
Now each value stored in animalMap is itself a std::map. The key type for both the outer and inner maps are std::string.
The [...] syntax is the subscript operator. More specifically, though, you subscript a map with keys. Keys are mapped to values.
sftrabbit gives the canonical way to do it. If you don't want multiple map look ups per key you could also use std::pair as a map key.
Here is an example of doing it that way.
Map is a container class that is used to store the aggregate data... Its very easy to retreive the datas stored in it as it uses hash algorithm for retrieval.
map is a key value pair...The data can be retrieved with the corresponding key...
Here in this declaration below I'm defining that the key has to be integer(4 bytes) and data as the string value...
typedef map<INT32U,string> EventMapType;
I searched for the example program of using map in wikipedia... But i could not understand the example given over there..I need to know how the datas and keys are stored in the map and how it is retreived through the key...I am new to MFC...
Beata,
I just did a quick google and came up with http://erunways.com/c-using-the-standard-template-library-stl-map-example/ I won't just copy paste that code here... it's only about 50 lines.
I suggest you read through that code, and then compile and run it (as is). If you run into problems or just stuff that doesn't make sense to you, then ask specific questions here. K?
Cheers. Keith.
map does not use hashing. It can't, because the constraints do not require hashable keys. It is ordinarily implemented as a binary search tree, sorted by key. Thus, it requires keys be <-comparable
In contrast, C++0x will provide an unordered_map, which does use hashing.
If you want specific help, you should tell us what code you've tried so far, and which examples you don't understand.
the STL's map class allows you to store data by any type of key instead of simply by a numerical key, the way you must access an array or vector. So instead of having to compute a hash function and then access an array, you can just let the map class do it for you.
typedef map<INT32U,string> MyEventMapType;
MyEventMapType EventMapType;
Use below as reference code.
To Store values :
EventMapType[key1] = string1 ;
EventMapType[key2] = string2 ;
EventMapType[key3] = string3 ;
To check the value at key1 ...
if(EventMapType.find("key1") == EventMapType.end())
{
std::cout<<"string1 is not in the map!"<<endl;
}
For more read the documentation
Iterators can also be used as a general means for accessing the data stored in a map; you can use the basic technique from before of getting an iterator:
I'm trying to dynamically add elements to a vector that is contained within a map, to store multiple arrays of "Particle" objects that are mapped to different ids. I'm new to the language and so I'm having trouble understanding if this can only be done with iterators? In this case it feels like overkill. Is it possible to directly access a vector inside a map? Since I can access the map elements by key, and because there is only one vector per key, it seems like it should be possible. I don't really have exact code as an example but it would look something like this:
int currentId = 1;
map <int, vector<Particle> > particleMap;
Particle p;
particleMap[currentId] <access to vector somehow here?> push_back(p);
I'm sure I'm missing some larger concept here, but I find myself needing this type of data structure a lot, so it would be great to know the proper way to access these kinds of "tables."
particleMap[currentId].push_back(p);
will work just fine.
There is only one vector per id; this is what you are referring to with particleMap[currentId]. Then you just continue with the expression as if you were writing myVector.push_back(p).
Let's say we have read these values:
3
1241
124515
5322353
341
43262267234
1241
1241
3213131
And I have an array like this (with the elements above):
a[0]=1241
a[1]=124515
a[2]=43262267234
a[3]=3
...
The thing is that the elements' order in the array is not constant (I have to change it somewhere else in my program).
How can I know on which position does one element appear in the read document.
Note that I can not do:
vector <int> a[1000000000000];
a[number].push_back(all_positions);
Because a will be too large (there's a memory restriction). (let's say I have only 3000 elements, but they're values are from 0 to 2^32)
So, in the example above, I would want to know all the positions 1241 is appearing on without iterating again through all the read elements.
In other words, how can I associate to the number "1241" the positions "1,6,7" so I can simply access them in O(1) (where 1 actually is the number of positions the element appears)
If there's no O(1) I want to know what's the optimal one ...
I don't know if I've made myself clear. If not, just say it and I'll update my question :)
You need to use some sort of dynamic array, like a vector (std::vector) or other similar containers (std::list, maybe, it depends on your needs).
Such data structures are safer and easier to use than C-style array, since they take care of memory management.
If you also need to look for an element in O(1) you should consider using some structures that will associate both an index to an item and an item to an index. I don't think STL provides any, but boost should have something like that.
If O(log n) is a cost you can afford, also consider std::map
You can use what is commonly refered to as a multimap. That is, it stores Key and multiple values. This is an O(log) look up time.
If you're working with Visual Studios they provide their own hash_multimap, else may I suggest using Boost::unordered_map with a list as your value?
You don't need a sparse array of 1000000000000 elements; use an std::map to map positions to values.
If you want bi-directional lookup (that is, you sometimes want "what are the indexes for this value?" and sometimes "what value is at this index?") then you can use a boost::bimap.
Things get further complicated as you have values appearing more than once. You can sacrifice the bi-directional lookup and use a std::multimap.
You could use a map for that. Like:
std::map<int, std::vector<int>> MyMap;
So everytime you encounter a value while reading the file, you append it's position to the map. Say X is the value you read and Y is the position then you just do
MyMap[X].push_back( Y );
Instead of you array use
std::map<int, vector<int> > a;
You need an associative collection but you might want to associated with multiple values.
You can use std::multimap< int, int >
or
you can use std::map< int, std::set< int > >
I have found in practice the latter is easier for removing items if you just need to remove one element. It is unique on key-value combinations but not on key or value alone.
If you need higher performance then you may wish to use a hash_map instead of map. For the inner collection though you will not get much performance in using a hash, as you will have very few duplicates and it is better to std::set.
There are many implementations of hash_map, and it is in the new standard. If you don't have the new standard, go for boost.
It seems you need a std::map<int,int>. You can store the mapping such as 1241->0 124515->1 etc. Then perform a look up on this map to get the array index.
Besides the std::map solution offered by others here (O(log n)), there's the approach of a hash map (implemented as boost::unordered_map or std::unordered_map in C++0x, supported by modern compilers).
It would give you O(1) lookup on average, which often is faster than a tree-based std::map. Try for yourself.
You can use a std::multimap to store both a key (e.g. 1241) and multiple values (e.g. 1, 6 and 7).
An insert has logarithmic complexity, but you can speed it up if you give the insert method a hint where it can insert the item.
For O(1) lookup you could hash the number to find its entry (key) in a hash map (boost::unordered_map, dictionary, stdex::hash_map etc)
The value could be a vector of indices where the number occurs or a 3000 bit array (375 bytes) where the bit number for each respective index where the number (key) occurs is set.
boost::unordered_map<unsigned long, std::vector<unsigned long>> myMap;
for(unsigned long i = 0; i < sizeof(a)/sizeof(*a); ++i)
{
myMap[a[i]].push_back(i);
}
Instead of storing an array of integer, you could store an array of structure containing the integer value and all its positions in an array or vector.