|
I have to write a function template max_generic() that takes as input an array of either int, float, or double, and returns the maximum element in the array. My professor's ppt slides are less than clear on this, and so far everything I've found on the internet is way over my head. I know how to find the max value of an array, but I don't know how to make it so that it can take multiple types. Going by what I read, I guess if you goC++ code:
|
# ? Apr 15, 2014 00:31 |
|
|
# ? Jun 1, 2024 16:48 |
|
Tusen Takk posted:I have to write a function template max_generic() that takes as input an array of either int, float, or double, and returns the maximum element in the array. My professor's ppt slides are less than clear on this, and so far everything I've found on the internet is way over my head. I know how to find the max value of an array, but I don't know how to make it so that it can take multiple types. Going by what I read, I guess if you go You want to - declare and initialize temp (be careful how you choose the initial value; imagine an array with all negative values) - make const T[] array and then it looks like it should work.
|
# ? Apr 15, 2014 00:38 |
|
Also it doesn't have to be named T. You can put any valid identifier there you want. I found it helpful to do that when I was first learning templates.
|
# ? Apr 15, 2014 00:43 |
|
Subjunctive posted:You want to xgalaxy posted:Also it doesn't have to be named T. You can put any valid identifier there you want. I found it helpful to do that when I was first learning templates. Yeah we've always used T in Java, and I saw it being used on other example code so I just went with it
|
# ? Apr 15, 2014 00:48 |
|
Tusen Takk posted:Is there a reason why you replaced < and > with < and >? No, it's a bug in the latest Awful.app. Sorry!
|
# ? Apr 15, 2014 00:53 |
|
Subjunctive posted:No, it's a bug in the latest Awful.app. Sorry! I thought that may be an XML/PHP thing, no worries!
|
# ? Apr 15, 2014 00:55 |
|
Illusive gently caress Man posted:I'm a little confused on 'recv()' and how it behaves. Let's say the following happens I'm not a networking expert, but IIRC it depends on the kind of socket. Specifically, on a SOCK_STREAM, a bunch of bytes sent in one write/send can get broken up across multiple reads/receives for a multitude of reasons; for example, TCP implementations often combine multiple writes into one packet that can end up larger than your buffer, and the internet is permitted to basically arbitrarily split TCP packets. A SOCK_DGRAM is different, and you should never get a partial recv as long as the buffer is large enough for the protocol's maximum message size; UDP's maximum message size is actually 65507 bytes, although of course your application-layer protocol can promise to never send a message that large, and the chance of random packet loss goes up with larger packets. SSL is a stream protocol and AFAIK makes no guarantees about messages being received completely. Among other things, the need to encrypt payloads makes it more likely that messages won't fit into tidy packet sizes.
|
# ? Apr 15, 2014 01:04 |
|
It doesn't seem to like my code at allC++ code:
Two more weeks and I'm done with this lovely class
|
# ? Apr 15, 2014 01:14 |
|
The parser is getting super confused because that's not how you declare arrays. Do const T array[]. Also you have no idea how arrays work in C; you need to be using something like a vector if the class allows it. Also I have no idea what language that thing you're doing with -> is even supposed to be from, but it ain't C++.
|
# ? Apr 15, 2014 01:17 |
|
rjmccall posted:The parser is getting super confused because that's not how you declare arrays. Do const T array[]. I wanted to use a vector but the professor wants an array, and the professor has ppt slides that say you can use -> to insert things into arrays. I have no idea why I thought taking this class would be a good idea, honestly edit: gently caress it i'm switching to vectors
|
# ? Apr 15, 2014 01:21 |
|
I love C++.
|
# ? Apr 15, 2014 01:30 |
|
Tusen Takk posted:the professor has ppt slides that say you can use -> to insert things into arrays. i cant even with this. its like the hypest thing
|
# ? Apr 15, 2014 01:42 |
|
Tusen Takk posted:I wanted to use a vector but the professor wants an array, and the professor has ppt slides that say you can use -> to insert things into arrays. I have no idea why I thought taking this class would be a good idea, honestly Are you sure that -> thing isn't just pseudocode? Sorry about the array-decl error, got distracted by a bug I introduced into Awful that the [] tripped, and stopped paying attention.
|
# ? Apr 15, 2014 01:52 |
|
Subjunctive posted:Are you sure that -> thing isn't just pseudocode? Heh, it may be. and no worries
|
# ? Apr 15, 2014 01:57 |
|
I mean, you can use a normal array if you're allowed to make assumptions about the maximum number of elements will go in it, and that's probably what your professor wants you to do, because professors are all about teaching terrible style in the process of introducing basic language concepts. It's very C-ish, but you can do it. You just need to declare something like T array[100];, then assign each value you read into the next element of the array. There will be extra elements of the array that you don't use, but that's not a problem — it's a minor performance problem if T ends up being some complex type like std::string.
|
# ? Apr 15, 2014 02:02 |
|
rjmccall posted:Also I have no idea what language that thing you're doing with -> is even supposed to be from, but it ain't C++. Subjunctive posted:Are you sure that -> thing isn't just pseudocode? C++ code:
Star War Sex Parrot posted:I love C++. Star War Sex Parrot fucked around with this message at 02:51 on Apr 15, 2014 |
# ? Apr 15, 2014 02:23 |
|
Sure. You could even do it without arithmetic and act directly on the first element. It's just... so unlikely of a thing to do intentionally that there probably ought to be a warning for it, but there isn't because it's too unlikely to even come up as a mistake.
|
# ? Apr 15, 2014 02:52 |
|
I'm having trouble with a priority queue. I have one of Vertex, and the comparator isC++ code:
Here's my traversal function: C++ code:
code:
|
# ? Apr 15, 2014 03:01 |
|
also tusen you can obviate the dependency on array vs vector by using iterators and iterator_traitscode:
|
# ? Apr 15, 2014 03:24 |
|
Star War Sex Parrot posted:Seeing all of this -> discussion mixed with his array hopelessness made me curious if you can actually use it with pointer arithmetic to dereference an array of object's members. I never really use pointer arithmetic with arrays anyway, but -> seems to work. It did FamDav posted:also tusen you can obviate the dependency on array vs vector by using iterators and iterator_traits That helps, I think. Ah well I somehow got 90's and 100's on all my previous projects so low score on this is something I can accept worst case scenario
|
# ? Apr 15, 2014 03:45 |
|
Tusen Takk posted:It did Basically, when you have an array in C++, it allocates an area in memory of (size of array type) * (number of array elements). The variable name for your array is actually just a pointer to the first element in that array, so you can use pointer arithmetic to move around in the array. The (test+2) line is equivalent to &test[2] (although it would appear that isn't actually a legal statement), and you can then dereference that pointer and call the method. I can't see when you would ever need to use that particular method to access an array of objects, but there you have it.
|
# ? Apr 15, 2014 04:15 |
|
Tusen Takk posted:It doesn't seem to like my code at all There is no type T in main, you're supposed to be using whatever type you want (make an int array probably), then the template function is designed to figure out what the input type is, substitute it for T, and thus compile a function that will worth with it.
|
# ? Apr 15, 2014 04:15 |
|
roomforthetuna posted:I think nobody's mentioned yet that the big problem your code has is that type T is only valid inside your template function printMax (the double line-break between the template<> and the function declaration is pretty confusing, those things generally go on the same line if they fit, or adjacent lines otherwise, they certainly belong together). I actually just figured this out, it works, and now I will hopefully never bother you guys again because that was the last lab of the semester. Thanks to everyone for being incredibly patient with me and my terrible, awful, horrible C++ warcrime code and I promise I will [hopefully] never use C++ professionally (or under any circumstances whatsoever, actually).
|
# ? Apr 15, 2014 04:29 |
|
Tusen Takk posted:I actually just figured this out, it works, and now I will hopefully never bother you guys again because that was the last lab of the semester. Thanks to everyone for being incredibly patient with me and my terrible, awful, horrible C++ warcrime code and I promise I will [hopefully] never use C++ professionally (or under any circumstances whatsoever, actually). thats the wrong attitude. just because you've had to deal with a weird professors weird requirements doesnt mean c++ is a bad thing.
|
# ? Apr 15, 2014 05:18 |
|
FamDav posted:thats the wrong attitude. just because you've had to deal with a weird professors weird requirements doesnt mean c++ is a bad thing. Yeah, C++ is a bad thing for many other reasons, not just a weird professor
|
# ? Apr 15, 2014 05:44 |
|
Suspicious Dish posted:Yeah, C++ is a bad thing for many other reasons, not just a weird professor Basically this. Coming from Java, it was an especially curvy curveball thrown at me and I'm honestly glad that I'm done with it.
|
# ? Apr 15, 2014 07:10 |
|
Tusen Takk posted:Basically this. Coming from Java, it was an especially curvy curveball thrown at me and I'm honestly glad that I'm done with it.
|
# ? Apr 15, 2014 07:16 |
|
Star War Sex Parrot posted:Learn C# I'm taking Python next semester and I was considering C# as well. I've heard it's basically the best parts of Java and C++, c/d?
|
# ? Apr 15, 2014 07:18 |
|
Tusen Takk posted:I've heard it's basically the best parts of Java and C++, c/d? Either way, yes C# is related to both Java and C++ in some ways and I find it to be a very nice language and the .NET Library to be well put together and all have their places in the world. We now return you to C++ chat.
|
# ? Apr 15, 2014 07:46 |
|
Thanks, Star Wars Sex Parrot. Next up on C++ chat: class template friend class template member templates, and are they right for your children?
|
# ? Apr 15, 2014 09:14 |
|
rjmccall posted:Thanks, Star Wars Sex Parrot. Next up on C++ chat: class template friend class template member templates, and are they right for your children? some children. better children.
|
# ? Apr 15, 2014 14:58 |
|
rjmccall posted:Thanks, Star Wars Sex Parrot. Next up on C++ chat: class template friend class template member templates, and are they right for your children?
|
# ? Apr 15, 2014 15:20 |
|
rjmccall posted:Thanks, Star Wars Sex Parrot. Next up on C++ chat: class template friend class template member templates, and are they right for your children? No. Next?
|
# ? Apr 15, 2014 15:48 |
|
|
# ? Apr 15, 2014 16:03 |
|
Tusen Takk posted:It did This is legal syntax in C/C++: code:
|
# ? Apr 15, 2014 17:22 |
|
If you're using C++11, you can even check your bounds at compile time so you don't have those pesky out-of-bounds access issues.C++ code:
C++ code:
|
# ? Apr 15, 2014 21:55 |
|
If anyone can point me in the right direction on this I'd appreciate it. I'm learning C and having a problem removing duplicates from a char array. My code works fine for reading two .txt files content into a char array and my function works for sorting it in alphabetical order. The problem is with removing duplicates from the list. Thank you in advance for any direction. Here is what I have: code:
|
# ? Apr 16, 2014 02:14 |
|
Tusen Takk posted:I've heard it's basically the best parts of Java and C++, c/d? roomforthetuna posted:On the subject of templates full of templates, the most annoying (C++11) error message is the one that goes something like "you're not allowed to specialize your function template inside the template class definition". gently caress you compiler, you know what I wanted, you could totally figure out how to do it, and some fucker just put an arbitrary rule there that you won't do it so I have to type my insane outer-template-class declaration a bunch of extra times? It's not even like you're encouraging me to separate my declaration and definition between header files and cpp files because this poo poo still involves templates so it's necessarily going in a header file.
|
# ? Apr 16, 2014 02:50 |
|
Scrapez posted:If anyone can point me in the right direction on this I'd appreciate it. I'm learning C and having a problem removing duplicates from a char array. Your algorithm for removing duplicates from a sorted list is O(n3), which tells me it's probably wrong. It looks like you're trying to find the duplicates and then shift everything left, but even that should only be two loops deep. It turns out, this can be done in a single pass with some insight. The key is to keep track of the last unique element in your list. Initially, you can only assume the first string is unique. Then, you look at every other string, from second to last, and do one of two things:
Here's the above in C. Maybe reconsider the whole listunsorted name, considering you never actually use listsorted. It was a bit awkward writing this, considering it depends on listunsorted being sorted. C code:
|
# ? Apr 16, 2014 03:11 |
|
|
# ? Jun 1, 2024 16:48 |
|
OneEightHundred posted:I'm more annoyed by template function partial specialization not being possible, and I'm still not sure why it isn't. I'm not sure what you mean by not possible. Templated functions can be partially specialized. For example: C++ code:
code:
code:
My personal pet peeve is the lack of literal operator templates for user-defined-char- and user-defined-string-literals. Maybe I belong in the coding horrors thread, but I want to be able to build data structures at compile-time from a string using template metaprogramming. For example, compile-time format strings with portable type-safety: C++ code:
|
# ? Apr 16, 2014 03:49 |