I was looking into separate chaining using hash_map and that is kind of what I need but not entirely. A hash_map will automatically delete duplicate values even in a chain. I dont want that.
Consider I have a vector of size 4. I want to basically attach a linked list to each one of the indexes of the vector. the linked list can have duplicate elements as well.
I researched about this but could not find a STL for this. Is there a way to do this?
I want to do this to create a database table. I have a vector of size 4. The 4 spots are columns in the table and the linked list attached to each index are the rows for the table. Hope this makes sense.
It sounds like you just want a Vector of Lists. Please specify the type for the data that you want to store in your table. The declaration will depend on that... If you're storing ints, it would look like this:
std::vector<std::list<int> > vecOfLists;
You mean something like this?
std::vector<std::list<std::string> > > myweirdlist;
Then you can add more lists to myweirdlist. And myweirdlist[0] for example will be a linked list which you can operate one.
Encapsulate the row in a class. Store a vector of objects of that class.
If you have a lot of deletions as well as insertions, maybe a list would be better, instead of the vector.
struct MyRecord {
// fields
};
std::vector<MyRecord> myRecordTable;
Related
PROBLEM:
I have a Hashtable in which I store some names. My problem is to print the content at each index in alphabetic order.
NOTE:
I'm working in C++, and it's required not to use any predefined C++ functions. (I defined the Hashtable using a struct, then declared all the usual functions for Hashtable).
MY IDEA:
I thought of using an array, such that I could copy all the records, stored in the linked list at index "i" of Hashtable, in it (array), then sort the array and, finally, print it's content. So, in such a way for all indexes, where the head of respective linked list in not null pointer.
I think this idea is to primitive. Can you, please, give me any suggestion on this topic ?
Thank you in advance !
If your data structure is an associative container but it's not sorted (eg std::map vs std::unordered_map) then you have no way to keep them ordered while managing the structure only.
Many hash table implementation are not even able to provide random access operators, since it makes no sense for such data structure: although indices are internally required to address buckets for values they are not meaningful from a client point of view.
Your primitive idea seems reasonable, copy the keys into a vector / linked list / array and then sort the array.
For certain reasons, I have a linked list of objects, with the Object containing a string.
I might be required to search for a particular string, and in doing so, retrieve the object, based on that string.
The starting header of the list is the only input I have for the list.
Though the number of objects I have, is capped at 3000, and that's not so much, I still wondered if there was an efficient way to do this, instead of searching the objects one by one for a matching string.
The Objects in the list are not sorted in any way, and I cannot expect them to be sorted, and the entry point to the linked list is the only input I have.
So, could anyone tell me if there's an efficient way ( search algorithm perhaps ) to achieve this?
Separately for this kind of search, if required, what would be the sort of data structure recommended, assuming that this search be the most data intensive function of the object?
Thanks..
Use a std::map<std::string, YourObjectType>. You can still iterate all objects. But they are sorted by the string now.
If you might have multiple objects with the same string, use a multimap instead.
If you can't switch to any different structure/container, then there is no way to do this better than linear to size of list.
Having 3000 you would like to use a unordered map instead of a linked list, which will give you average O(1) lookup, insertion, and deletion time.
In my project I have a vector wit some relational data (a struct that holds two similar objects which represent a relationship between them) and I need to check for relationships combinations between all data in the vector.
What I am doing is iterating over the vector and inside the first for loop I am iterating again to look for relationships between data.
This is a simplified model of what I am doing
for(a=0; a<vec.size(); a++)
{
for(b=0; b<vec.size(); b++)
{
if(vec[a].something==vec[b].something) {...}
}
}
My collection has 2800 elements which means that I will be iterating 2800*2800 times...
What kind of data structure is more suitable for this kind of operation?
Would using for_each be any faster then traversing the vector like this?
Thanks in advance!
vec has two structs which are made up of two integers and nothing is ordered.
no, for_each still does the same thing.
Using a hash map could make your problem better. Start with an empty hash and iterate through the list. For each element, see if it's in the hash. If it's not, add it. If it is, then you have a duplicate and you run your code.
In C++, you can use std::map. In C, there is no built in map datastructure, so you'd have to make your own.
The high-level pseudo code would look something like this
foreach (element in array)
if map.has_key(element)
do_stuff(element)
else
map.add_key(element)
The easiest way to improve the efficiency of this operation would be to sort the vector and then look for duplicates. If sorting the vector isn't an option, you could create another vector of pointers to the elements of this vector and sort that. Both of those will take you from an N**2 complexity to an N*log(N) complexity (assuming, of course, that you use an N*log(N) sort). This does mean using more space, but often using a bit of space for significant time improvements is very reasonable.
assuming your vector contains a "relation" structure like:
class Entity;
struct Relation {
Entity* something;
Entity* relative;
};
and you have a vector of "relations":
std::vector<Relation> ties;
So if I understood it correctly, you want to segment ties and have a list of Relations for each Entity. This may be represented by a map:
std::map<Entity*,std::vector<Relation*>> entityTiesIndex;
Then you could just scan once through all ties and collect the relations for each entity:
for (int i=0; i < ties.size(); ++i ) {
Relation* relation = &ties[i];
entityTiesIndex[relation->something].push_back(relation);
}
Mind here the usual disclaimer about references to container elements, as these may change when container is altered.
Hope this makes sense.
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).
Well I'm making a small phone book application and I've decided that using maps would be the best data structure to use but I don't know where to start. (Gotta implement the data structure from scratch - school work)
Tries are quite efficient for implementing maps where the keys are short strings. The wikipedia article explains it pretty well.
To deal with duplicates, just make each node of the tree store a linked list of duplicate matches
Here's a basic structure for a trie
struct Trie {
struct Trie* letter;
struct List *matches;
};
malloc(26*sizeof(struct Trie)) for letter and you have an array. if you want to support punctuations, add them at the end of the letter array.
matches can be a linked list of matches, implemented however you like, I won't define struct List for you.
Simplest solution: use a vector which contains your address entries and loop over the vector to search.
A map is usually implemented either as a binary tree (look for red/black trees for balancing) or as a hash map. Both of them are not trivial: Trees have some overhead for organisation, memory management and balancing, hash maps need good hash functions, which are also not trivial. But both structures are fun and you'll get a lot of insight understanding by implementing one of them (or better, both :-)).
Also consider to keep the data in the vector list and let the map contain indices to the vector (or pointers to the entries): then you can easily have multiple indices, say one for the name and one for the phone number, so you can look up entries by both.
That said I just want to strongly recommend using the data structures provided by the standard library for real-world-tasks :-)
A simple approach to get you started would be to create a map class that uses two vectors - one for the key and one for the value. To add an item, you insert a key in one and a value in another. To find a value, you just loop over all the keys. Once you have this working, you can think about using a more complex data structure.