I want to insert each element in a vector into a hash table. I am doing that by for_each in <algorithm> header file, which calls insert() function, which inserts each element passed, into a hash table. Problem is, the hash table that insert() instantiates, would be local to that insert() function. How do I return the populated hash table back to the calling function?
struct myFunc
{
hash_table_type *ptrToMyHashTable;
myFunc ( hash_table_type *ptrToMyHashTable ) : ptrToMyHashTable (ptrToMyHashTable) {}
voidoperator() ( element_type el ) // Will be called from for_each
{
//insert el in your hash table
}
};
//...
hash_table_type MyHashTable;
for_each ( myVector.begin(), myVector.end(), myFunc ( &MyHashTable )/*create an object of myFunc*/ );
Can I have access to the iterator position for the element passed within myFunc above? For example, if I am passing value pointed by 1st iterator to myFunc, is there anyway I can locate next element to the 1st iterator passed, which is second element, within my Func?
What do you think is the best way to solve the problem:
- A vector of integers is given. Program should Reduce the vector of integers by cumulatively applying a function to every two digits, starting from the beginning and return final result.
For example: vector A = 1,2,3,4,5
Program calculates ((((1+2)+3)+4)+5) and returns final result
where + is a "function". It should work for any function.