C++

Discussion in 'Scripting & Programming' started by Mof, Jul 10, 2008.

  1. Mof

    Mof Megabyte Poster

    526
    2
    49
    as I see it vector arrays have the advantage that the size can be changed as the program requires.
     
    WIP: C++ and A+
  2. dmarsh
    Honorary Member 500 Likes Award

    dmarsh Petabyte Poster

    4,305
    503
    259
    Vectors can grow dynamically to meet the needs of the application, a native C style array cannot.

    Aside from that they are very similar, except the vector fits better with the functional programming design of the STL.

    Theres also some subtle bugs people introduce with arrays like not using array delete :-

    Once you start using vectors (and the other STL collections) you will really wonder how you lived without them.

    There is the phrase 'standing on the shoulders of giants', frequently C++ developers are standing on each others toes. There is no real need to reinvent the wheel, its good to know datastructures and algorithms but the STL encompasses all that knowledge in a neat little package.

    If you don't like the look of the template syntax you can always use a few typedefs too!
     
  3. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,624
    117
    224
    While I like vectors, I used to dynamically grow arrays in old C code - realloc() was fine for this. Messy to do and great care was needed, but it could be done. Wrapping this in the way that STL does (and similar in other languages) makes this far more stable to use!

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  4. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    Vectors are part of the Standard Template Library (STL) which provides a suite of generic structures with associated functionality not seen for the likes of the standard C arrays that we all were introduced to. What does it mean to be called a 'Standard Template library'? What does this name actually mean? Well, in C++ a template is a generic sructure with associated routines that can be instantiated with any scalar defined type, for instance taking the most basic example of an STL container (being the vector), you can have:

    Code:
    vector<int> iVec;        // An integer vector.
    vector<float> fVec;     // A float vector.
    vector<char> CVect;   // A character vector.
    
    The vector is an example of a container that inherets specific features associated with its intended use from the STL. It is a one-dimentional array to which the programmer can dynamically extend, reduce, clear, check the status of, etc. Simply by calling templated functions associated with that container. If you are using the Visual Studio IDE you should be able to take an example program from this thread and view the list of iterators, functors, etc. associated with the container being the 'vector' in this case by typing the following code somewhere in your source:

    Code:
    vector<int> iVec;
    iVec.			// Intellisense will kick in with a pulldown list of options at the point of the '.'
    
    When the programmer creates a standard array all of these features are not available to him/her, so they must explicitly code such functionality into their code. Even so, it still remains that the STL is not accepted by many programmers as the most efficient way to perform its tasks.

    The STL is a great big story that requires its own book to have a full overview. I've only skimmed over the surface here, but feel free to ask if you need anything clarifying. :)
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  5. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    Couldn't agree more.

    We still use the old C-style way of doing things simply because the underlying implementation of the STL is unreliable for our applications, and we work with very specific amounts of memory. :)
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  6. dmarsh
    Honorary Member 500 Likes Award

    dmarsh Petabyte Poster

    4,305
    503
    259
    This is an excellent book on the STL :-

    http://www.amazon.com/C-Standard-Library-Tutorial-Reference/dp/0201379260

    Many people do not like to rely on the C standard library anymore. Many functions are non re-entrant.

    The C++ standard provides new and delete and array new[] and delete[] instead of the older C library malloc etc.
    The C library versions require pointer casts and do not call constructors, they do however provide realloc which C++ does not.

    http://www.codeproject.com/KB/tips/newandmalloc.aspx

    Dynamic memory is allocated off the heap, depending on the platform you could have multiple heaps with different properties.
    Depending on the context and what heap mangement you need will determine what library functions you should use.
    The STL vector abstracts the complexity for the common case and makes general code easier.

    Harry is correct in that wrapping common fucntionality in a tested library makes for a more reliable implementation. In fact this is exactly what the STL is, it was originally created at SGI for just this purpose.

    Most C++ programmers have written a variety of datastructures themselves but theres less need than ever now. I say that and I was an embedded programmer coding for devices with 1 MB RAM and 1 MB flash, we did not use the STL and created our own datastructures based largely on C primitives but this need is diminishing every year, people now have VM's and cut down SQL compliant databases on embedded devices !

    I also developed my own graph datastructure, again probably a waste of time these days. Look at Boost they provide many excellent extensions and some have been incorporated into TR1.
     
  7. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    If CF wants to have such an arrangement I'd be happy to do my bit. :)
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  8. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    In games it's still as strong as ever. We create our own data structures and define in great detail how these data structures are manipulated - even defining bit-level operations if they are justifiably more efficient. We don't have a use for SQL compliant databases, but our VMs are specifically created for our applications and have been optimised the hell out of. :biggrin
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  9. dmarsh
    Honorary Member 500 Likes Award

    dmarsh Petabyte Poster

    4,305
    503
    259
    I'd be more than happy to help out any way I could, my C++ skills are not what they once were ! :D

    Nowadays my focus is mainly on Java, currently learning Groovy and Grails, also do a little bit of C# and in the past I coded in various stuff but mainly C++.
     
  10. Mof

    Mof Megabyte Poster

    526
    2
    49
    Oh well worth a try looks like no one else is interested never mind back to the book.:cry:
     
    WIP: C++ and A+
  11. dmarsh
    Honorary Member 500 Likes Award

    dmarsh Petabyte Poster

    4,305
    503
    259
    Well it is a good idea ! :D

    Its just people live in different geographical areas and have different commitments making a regular real life class difficult.

    An online class could be possible but there is already a lot of information on the web, any proposals would ideally have to offer something more.

    Theres already been similar stuff muted by Trip and Math had a suggestion to form a BCS special interest group.

    I travel around a lot so I'd find it hard to make a regular commitment.
     
  12. Mof

    Mof Megabyte Poster

    526
    2
    49
    Wish I had thought of doing this when i was younger, My planned route is c++ then c# followed by .net shoud jarva be in there some where, seems to be so many.


    on enums at the moment,Just want to make sure I havn't gone off on a wrong tangent again.
    enum basically assigns a number to a constant name ie "enum name {paul,jack,harry}; would be paul =0 jack=1 harry=2"
    and if I added "enum name{paul=1,jack,harry};the numbers would then start at 1,2 &3.

    in this case
    typedef creates a data type called charge with constant variable enums assign to whats in the brackets in this case NEGATIVE,POSITIVE =0,1

    can anyone explain in simple terms how Modulus works? a=8 b=4 a &#37; b = 0 AHHHHHH
     
    WIP: C++ and A+
  13. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,624
    117
    224
    An example of modulo is the minutes figure on a clock. When it reaches 60 it goes back to 0.

    So if you started on the hour - and walked for 80 minutes, your clock would show 20 in the minutes figure.

    Or to put it another way - 80 mod 60 results in 20. So you can think of the modulo as the remainder from an integer division.

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  14. Mof

    Mof Megabyte Poster

    526
    2
    49
    thing can be so simple somtimes so (a % B) =0 so taking the clock theory and having 4 numbers when it gets to 8 it would have gone through the cycle twice ending up on 0.
     
    WIP: C++ and A+
  15. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,624
    117
    224
    Assuming your previous definition of a and b - then yes - that's right.

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  16. dmarsh
    Honorary Member 500 Likes Award

    dmarsh Petabyte Poster

    4,305
    503
    259
    Why how old are you ? Never too late to learn something new. :D

    You should try and learn just one language well to start with, otherwise you might just confuse yourself !
    Then maybe learn just enough to do what interests you and complete a small project.

    C# is .Net its one of the many languages that can run on the .Net CLR.
    If you meant followed by ASP .Net that makes more sense as ASP .Net is a web framework and templating language that can be used in conjunction with C# to create web sites.

    You need not learn C++, Java and C#, they are all general purpose languages, you could happilly get by with just one in many industries. Different languages and tools will have different relevance to different industry sectors and problem domains.

    You will need to pick up lots of other stuff eventually to be a professional programmer, various libraries, frameworks, build systems, Configuration Management systems, SQL/Databases, HTML/Internet, Design/Architecture, theres is a lot ! :D
     
  17. nellyp123

    nellyp123 Byte Poster

    213
    13
    0
    Just wanted to let people know of a good site that's got plenty of programming tutorials to choose from....

    www.programmingtutorials.com

    By the way...anyone on here learned Actionscript 3.0. I have been hard at it for the last three months and wondered if i could pick someone's brain???

    Neil
     
    Certifications: CIW Professional
  18. Mof

    Mof Megabyte Poster

    526
    2
    49
     
    WIP: C++ and A+
  19. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    You are aware that:

    Code:
    enum name {paul,jack,harry}; 
    
    and

    Code:
    typedef enum {paul,jack,harry} name;
    
    are identical in C++ but not C? In the old days C programmers were forced to use the second method because of the way types are linked to their type names. In any pure C compiler you should receive an error for the first bit of code.

    What hbroomhall said. :biggrin
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  20. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    Yeah, saw that site ages ago. Some good and some very bad tutorials in there.

    I don't know actionscript unfortunately. :(
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.

Share This Page

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.