Find function in STL C++ for unordered multimap? - stl

The find function only returns one pointer for duplicate elements in multimap. How can we return pointer to other duplicate element in multimap.

You could use equal_range, which returns a pair of iterators to the range of elements matching a key.

Related

Fortran elemental function - is this behavior normal?

Consider the following function
ELEMENTAL FUNCTION int2str(i) RESULT(s)
INTEGER, INTENT(IN) :: i
CHARACTER(LEN = 50) :: appo
CHARACTER(LEN = :), ALLOCATABLE :: s
WRITE(appo, '(I0)') i
ALLOCATE(s, source = TRIM(appo))
END FUNCTION int2str
The function, being elemental, is a scalar function (in my case it takes one scalar integer and gives back one scalar character, even though allocatable in length) that applies elementwise to arrays.
Why the output of
print *, '>>>'//int2str([1, 3, 5, 7])//'<<<'
is (unexpectedly to me)
>>>1<<<>>>3<<<>>>5<<<>>>7<<<
whereas the output of
print *, '>>>', int2str([1, 3, 5, 7]), '<<<'
is (expectedly)
>>>1357<<<
?
What I mean is that the function should apply to every one of the four scalar integers composing the array, thus returning a length-4 array each element of which being a string, but it seems to me that its elementwise-ness applies to the whole concatenation of three strings, as though the // operator has precedence over the function's result.
For character concatenation, the expression
'>>>'//['1','3','5','7']
depends on the scalar '>>>' and the array ['1','3','5','7']. As with other intrinsic binary operations where the operands are a scalar and a rank-1 array, the expression is a rank-1 array.
In determining the value of the expression, the scalar operand is treated as an array like
['>>>','>>>','>>>','>>>']
and the expression is equivalent to
['>>>','>>>','>>>','>>>'] // ['1','3','5','7']
Finally, the expression has value where the elements are operands pairwise:
['>>>1','>>>3','>>>5','>>>7']
You can see the parallels with the expression
9+[1,3,5,7] ! [9,9,9,9]+[1,3,5,7] --> [10,12,14,16]
When there are two concentation operations going on, the obvious value is the result.
Note that I didn't express this in terms of an elemental function's result. This is partly because the fact the array comes from a function isn't significant. Also, your elemental function is not actually allowed: an elemental function result may not be allocatable.
On the invalid function, Vladimir F has submitted a bug report covering gfortran not detecting violation of the numbered constraint C1290 of Fortran 2008. In that report you can see that if you remove the result(s) and declare int2str as having the allocatable attribute the function is rejected. Some other compilers do already detect violation.
I've surprisingly experienced that result which I didn't expect (>>>1<<<>>>3<<<>>>5<<<>>>7<<<) is indeed the same even if I manually implement the ELEMENTALity of the function, namely
print *, '>>>'//[num2str(1), num2str(3), num2str(5), num2str(7)]//'<<<'
so I was possibly misunderstanding my own words thus RETURNing a length-4 array each element of which being a string. The function is indeed returning an array of strings, and the concatenation applies to every element of the array (the element of the array being stick together without space in between just because they're strings of characters).

Find the smallest function in a c file

I have a c(java,c++) file with t functions in it. Is there any way, I can find out the function with smallest no. of words.
Given a function, I can calculate the no.of words in it using yylex().
Since you haven't specified the language for implementation of the solution, I will propose a description of the algorith I would use.
You can iterate through the file and use regex to detect function prototipes (such as "^(\w+( )?){2,}\([^!##$+%^]+?\)" for C functions). Once you've detected you have a function, you iterate through the next lines and count the words. Once you detect a line containing another function definition, you will store the function with its word count and get to the next until you reach the end of the file.
If we consider you need only the function with the least number of words, you can just store the function name only if it has fewer words than the previous one.

NumaPro Cuda Device Function - Return multiple Arrays and local memory

Does anyone know what the correct syntax for the cuda.jit decorator is if you want to write a device function that returns multiple arrays?
If my device function should return one float and had two integer parameters my decorator would be:
#cuda.jit('float64(int64,int64)', device=True, inline=True)
Now I want my function to take two integer paramters and two floats and return 2 arrays of floats and 2 arrays of integers, all of the same length (between 3 and 5) which depends on the input arguments. How do I do that?
Would that be correct:
#cuda.jit(restype=[float64[:], int64[:], float64[:], int64[:]], argtypes=[int64, int64, float64, float64], device=True, inline = True)
Also in my function I would create the arrays I want to return by using: cuda.local.array()
Since I use inline=True I would suspect that this will work and the arrays will be only accessable by the respective thread, right?
Now I want my function to take two integer parameters and two floats
and return 2 arrays of floats and 2 arrays of integers
What you are really saying there is you want your JIT kernel to return a tuple (of two arrays). Unfortunately, in the nopython frontend, I don't believe that is legal. There is no object support in nopython, so you can't instantiate and return a tuple object.
Also in my function I would create the arrays I want to return by using: cuda.local.array()
Unfortunately that isn't supported either. It is only legal to return an array which was passed as an argument to the function.

XQuery - calculating number of elements

I'm trying to declare a user-defined function in XQuery, that would be passed an element and would return the total number of elements in its tree (meaning itself plus its subtree).
Is this even possible to do in XQuery with a recursive function or will I need another approach?
Yes, this is possible. As this has a smell of homework, I'm not giving a full answer, but the idea on how to do it.
For both cases, you'll have to consider which children to consider while counting. Reading your question, it looks like you're looking for elements only and can safely ignore attributes, comments, text nodes and processing instructions.
Using a Recursive Function
Define a function, which sums up the size of each individual subtree (which you determine by a recursive function call). Something like (this is not XQuery code!):
function subtree_size {
sum(
for each element
return subtree_size(current element)
)
}
Passing all Elements to the count Function
XQuery has a count function, which returns the number of elements passed. There is a very short and rather easy to find XPath expression to return all descendant nodes (including the node itself). Have a look at the axis steps available in XPath.

Delete and return in a single verb?

Imagine a function that deletes a cookie and returns its value.
What would you call it if you have to use a single verb?
I called it clear but I'm not very fond of the name.
It sounds similar to Pop, except Pop typically acts on the last element in a collection. Perhaps Extract could be a suitable name?
In the Ruby doc for hsh.delete(key), their doc is:
"Deletes and returns a key-value pair from hsh whose key is equal to key. If the key is not found, returns the default value."
So deleteCookie would probably be acceptable, the key is to just document the behavior properly.
I'd do something like this:
public String GetAndDeleteCookie(String cookieName); // C#
function getAndDeleteCookie(cookieName); // JavaScript
get_and_delete_cookie(cookieName); // php (forget exact syntax)