I would not say that that is the C++ way to do it, considering that std::max_element makes use of a range denoted by an iterator pair, rather than a vector.Originally Posted by tabstop
YuckOriginally Posted by manasij7479
I would not say that that is the C++ way to do it, considering that std::max_element makes use of a range denoted by an iterator pair, rather than a vector.Originally Posted by tabstop
YuckOriginally Posted by manasij7479
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Why ? ...Is iterating better than popping out the last each time ?Originally Posted by laserlight
why not use variadic templates? it really seems like it would be the C++ way to do exactly this.
Variadic templates allow for a infinite amount of template arguments or types. So if it isn't types, then it must be static arguments (known at compile time).
For runtime arguments, std::initializer_list is exactly what was created for that purpose.
I'll agree that std::initializer_list is an excellent way to do this; however, there's no reason why you couldn't write the code (and its accompanying documentation) such that the function expects only int parameters, and assumes internally that they are integer values. you could then recursively run through the list and add each parameter to a vector, from which you could then determine the largest value, or whatever you like. from the perspective of the user of the function in question, the variadic template method might actually be a little quicker and more intuitive.
with regard to the initializer_list method, is there a reason why you can't make the function accept a vector and pass it an initializer list? I've tested the following code with g++ 4.6, and it compiles perfectly:
Code:int foo(std::vector<int> vInt) { int largest = vInt.front(); for (int i : vInt) { if (i > largest) largest = i; } return largest; } int bar() { return foo({1, 2, 3, 4, 5, 6, 7, 8, 9, 0}); }
There is nothing stopping you from doing that, except perhaps some extra overhead.
But then again, with an initializer list, there's no point in doing so.
It's good for C++03 code, though.
This isn't a variadic template method, and it wouldn't work properly.from the perspective of the user of the function in question, the variadic template method might actually be a little quicker and more intuitive.
You are contradicting yourself. What is the question?with regard to the initializer_list method, is there a reason why you can't make the function accept a vector and pass it an initializer list? I've tested the following code with g++ 4.6, and it compiles perfectly:
I'm not sure if you're understanding... we have 5 arguments: smax(5,1,4,8,6). va_arg will keep going on forever, creating an infinite loop, so we have to stop it at the sixth argument. So either it returns some an empty value at six, or it returns 0, which won't help us at all. If it returns an empty value, we can check for that. If that doesn't work... is there any way for us to be able to find the number of arguments without user input?Originally Posted by Elysia
EDIT: didn't see the second page there.
va_arg does neither. It simply returns whatever is written at that memory location. That is, if we can read that. If not, we'll likely get an access violation. In either case, it's undefined behavior.
Simply put, it's not possible without manual input to do this with va_arg, and this is because of its C nature. C does not hold your hand.
The C++ alternatives are more friendly, and that is why I recommend you look into those.
Okay, so since everyone is arguing, I just blindly choose one of them?