2 arguments in push_back - vector

I am trying to put 2 arguments inside a vector using push_back but its giving me an error since the function is allowed to take only one argument. How can I pass 2 arguments??
Vertex Class:
template <class VertexType, class EdgeType> class Vertex{
public:
std::vector<std::pair<int, EdgeType>> VertexList;
};
Outside Vertex Class inside Main():
project3::Vertex<string, string> v1("v1");
v1.VertexList.push_back(1,"e1");
Error is :
error C2661: 'std::vector<_Ty>::push_back' : no overloaded function takes 2 arguments
IntelliSense: too many arguments in function call

You need to do
v1.VertexList.push_back(std::pair<int, EdgeType>(1,"e1"));

Try push_back(make_pair(1, string("e1")));

Related

Kotlin function reference

Let records be stream/collection and extract function which transforms data form an element of such collection.
Is there a way in Kotlin to write
records.map {extract(it)}
without explicitely applying(it) ?
E.g. records.map(extract) or records.map {extract}
If extract is a value (local variable, property, parameter) of a functional type (T) -> R or T.() -> R for some T and R, then you can pass it directly to map:
records.map(extract)
Example:
val upperCaseReverse: (String) -> String = { it.toUpperCase().reversed() }
listOf("abc", "xyz").map(upperCaseReverse) // [CBA, ZYX]
If extract is a top-level single argument function or a local single argument function, you can make a function reference as ::extract and pass it to map:
records.map(::extract)
Example:
fun rotate(s: String) = s.drop(1) + s.first()
listOf("abc", "xyz").map(::rotate) // [bca, yzx]
If it is a member or an extension function of a class SomeClass accepting no arguments or a property of SomeClass, you can use it as SomeClass::extract. In this case, records should contain items of SomeType, which will be used as a receiver for extract.
records.map(SomeClass::extract)
Example:
fun Int.rem2() = this % 2
listOf("abc", "defg").map(String::length).map(Int::rem2) // [1, 0]
Since Kotlin 1.1, if extract is a member or an extension function of a class SomeClass accepting one argument, you can make a bound callable reference with some receiver foo:
records.map(foo::extract)
records.map(this::extract) // to call on `this` receiver
Example:
listOf("abc", "xyz").map("prefix"::plus) // [prefixabc, prefixxyz]
(runnable demo with all the code samples above)
you could use method reference (similar to Java).
records.map {::extract}
take a look at the function references examples on kotlin docs
https://kotlinlang.org/docs/reference/reflection.html#function-references

Converting object of class function into function

In R, assume an object of type 'closure' called my_object that contains both a likelihood function and the associated parameters.
Assume further that I would now like to extract specifically the likelihood function from said object and pass it on to a different R command, which needs a likelihood function as its first argument. I can extract said function via command unenclose in library pryr.
> library(pryr)
> lik_func = unenclose(my_object)
> lik_func
function (pars, condition.surv = TRUE, root = ROOT.OBS, root.p = NULL,
intermediates = FALSE)
{ #function_specifics_here# }
<environment: namespace:diversitree>
However, what I apparently extract is just an object of class 'function', and not the likelihood function itself, as the next R command complains that it does not actually receive a function, but an object:
> asr.marginal(lik=lik_func, pars=my_pars)
Error in UseMethod("make.asr.marginal") :
no applicable method for 'make.asr.marginal' applied to an object of class "function"
How do I convert this object of class 'function' into a plain function, assuming such a distinction actually exists?
Note: I am uncertain if or why a distinction between an object of class 'function' and a plain function actually exists. Maybe someone answering this question could share some light on this too.
“Objects of class "function"” are, generally1, functions. In particular, they are objects of type "closure" and class "function".
Using pryr::unenclose doesn’t have any useful effect here. Its only effect is to take a function and replace all references to objects in an enclosing environment with their value. So if I have, say:
x = 1
f = function () x
… then unenclose(f) will yield:
function () 1
This doesn’t make f any more or less of a function.
Your error message seems to be fundamentally unrelated to that. Instead, asr.marginal specifically expects a likelihood function, which apparently needs to be created by one of the make.* functions in the package. A likelihood function in the context of ‹diversitree› is a function of class "dtlik".
1 The exception is if you are prone to shenanigans:
x = 42
class(x) = "function"
Now x has class function but obviously isn’t a function.

S4 class constructor and validation

I present a short code to create a S4 class myclass and ensure that objects are created if they verify a condition given by a parameter param
setClass("myclass", slot = c(x = "numeric"))
#constructor
ValidmyClass<- function(object, param = 1)
{
if(object#x == param) return(TRUE)
else return("problem")
}
setValidity("myclass", ValidmyClass)
setMethod("initialize","myclass", function(.Object,...){
.Object <- callNextMethod()
validObject(.Object,...)
.Object
})
For which I get the following error message Error in substituteFunctionArgs(validity, "object", functionName = sprintf("validity method for class '%s'", :
trying to change the argument list of for validity method for class 'myclass' with 2 arguments to have arguments (object)
I understand the issue with the arguments but I cannot find a way to solve this. The document about setValidity mentions that the argument method should be "validity method; that is, either NULL or a function of one argument (object)". Hence from my understanding excluding more than one argument.
Nevertheless, the idea behind this example is that I want to be able to test the construction of a myclass object based on the value of an external given parameter. If more conditions were to be added, I would like enough flexibility so only the function ValidmyClass needs to be updated, without necessarily adding more slots.
The validity function has to have one argument named object. When I need to create one argument functions but really have more arguments or data to pass in I often fall back to using closures. Here the implementation of your ValidmyClass changes in that it will now return the actual validity function. The argument of the enclosing function is then the set of additional arguments you are interested in.
setClass("myclass", slot = c(x = "numeric"))
#constructor
ValidmyClass <- function(param) {
force(param)
function(object) {
if (object#x == param) TRUE
else "problem"
}
}
setValidity("myclass", ValidmyClass(1))
Also the validity function is called automatically on init; however not when the slot x is changed after the object is created.
setMethod("initialize", "myclass", function(.Object,...) {
.Object <- callNextMethod()
.Object
})
new("myclass", x = 2)
new("myclass", x = 1)
For more information on closures see adv-R. Although I think this answers your question, I do not see how this implementation is actually helpful. When you define your class, you basically also fix the additional parameters which the validity function knows about. If you have several classes for which you can abstract the validity function then I would use the closure. If you have one class with changing parameters at runtime, I would consider to add a slot to the class. If you do not want to alter the class definition you can add a slot of class list where you the can pass in an arbitrary number of values to test against.

qRegisterMetaType usage

#include<QMetaType>
typedef QList<int> IntList;
qRegisterMetaType<IntList>("IntList");
error C2909: 'qRegisterMetaType': explicit instantiation of function template requires return type
C2909 says I need to define
template int qRegisterMetaType<IntList>("IntList");
If I define like I mentioned above then I get the below error
error C2059: syntax error : 'string'
warning C4667: 'int qRegisterMetaType(void)' : no function template defined that matches forced instantiation
why do I get this error ?
"qRegisterMetaType" is a function. It must appear in a code block.
int metatype_id = qRegisterMetaType<IntList>("IntList");
You need to add Q_DECLARE_METATYPE(IntList) before you can register it.

How to implement generic Max<TSource>(Func<TSource,TSource> func)

I am busy writing my own collection type and need to have a function
Max that returns the value in the collection, where one of the value attributes is a max or some condition holds.
So I am trying to call Max(Func<...) on one of the underlying .net collections, but
I can't seem to get it to work:
public TValue MaxValue(Func<TValue,TValue> func)
{
return this.valueSet.Max<TValue>(func);
}
but I am getting 2 errors:
Argument 2: cannot convert from 'System.Func<TValue,TValue>' to System.Func<TValue,int>'
and
'System.Collections.Generic.SortedSet<TValue>' does not contain a definition for 'Max'
and the best extension method overload 'System.Linq.Enumerable.Max<TSource>(System.Collections.Generic.IEnumerable<TSource>,
System.Func<TSource,int>)' has some invalid arguments
I just can't seem to figure out what I should be doing here...
When you call:
this.valueSet.Max<TValue>(func);
the compiler interprets this as one of the Max overloads with one generic type. Either explicitly point out that the return value also should be a TValue type:
this.valueSet.Max<TValue, TValue>(func);
or use implicit typing and let the compiler sort out the types itself:
this.valueSet.Max(func);

Resources