Programming algorithms with strings in C++, and transition from C by example!

Discussion in 'C++' started by CoreyWhite, Mar 30, 2007.

  1. CoreyWhite

    CoreyWhite Guest

    /* WORKING WITH STRINGS IN C++ IS THE BEST WAY TO LEARN THE LANGUAGE
    AND TRANSITION FROM C. C++ HAS MANY NEW FEATURES THAT WORK TOGETHER
    AND WHEN YOU SEE THEM DOING THE IMPOSSIBLE AND MAKING COMPACT COHERENT
    CODE THAT WORKS WITH STRINGS, IT ALL BEGINS TO MAKE SINCE*/

    /* The basics of C++ are Classes, that build Types. Which are used to
    create quick and dirty routines in the smallest possible space. The
    Classes & Routines uses the standard template library which defines a
    number of functions, that use special types (such as strings), and
    defines a number of containers, (The most popular being a vector).
    The language also looks at everything in terms of low level binary
    information that involves understanding binary math, using binary
    operators, and pointers which reference directly into memory.
    Containers also have a partner of their own to work with them called
    Iterators, which are a special type of container that act as a pointer
    to the infromation in a since. You will get it all once we get moving
    in this program. But you need to read the books */

    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>

    using namespace std;

    // (c) 2007 Steven Pigeon
    // released under GPL 2.0


    // introduces special operator for suffix compares

    /* Classes & Structures are Meta-containers, which contain containers
    and can build there own types, having constructors and destructors.
    They are new to C++ and you can find many uses for them once you learn
    what the other new tools in C++ are. Classes & Structures let us take
    advantage of containers, iterators, and pointers, to right tight
    compact object oriented code. */

    /*This first class, is a structure which makes it always pubic. It is
    constructed as a public binary_function. That means it borrows the
    variables that are past along with it to whatever function calls it.
    That is a novel and unusual way of constructing a structure. Usually
    classes are constructed simply with their own functions.*/

    struct less_rev: public binary_function<string, string, bool>
    {

    // reverses a string

    /*This structure has two functions inside of it, the last one calls
    the first one, and it runs automatically when the structure is
    created, using the variables origonal past during the creation of the
    structure. I think you can see how this makes for some quick and
    dirty code.*/

    inline string _reverse(const string & a) const
    {
    string temp=a;
    reverse(temp.begin(), temp.end() );
    return temp;
    }

    /*The above function just quickly calls the "reverse" function in the C
    ++ Standard Template Library, and does all the work.*/

    /*While the below function does some quick and dirty low level math,
    to evaluate the two reversed strings once they are called by the above
    function. Strings are all binary numbers, so you can manipulate and
    examine them on the lowest level. In this case it orders them by
    finding out which string is smaller.*/

    // compares two strings, in reverse

    inline bool operator()(const string & a, const string & b) const
    {
    return _reverse(a) < _reverse(b);
    }
    };

    /*Notice right above this comment you can see a semi-colon ending the
    structure. You don't have to do that anywhere else in C++ with a
    closing braket, but you have to remember to do it with structures. By
    now you should realize how complicated and involved C++ is. But it is
    really as essential as it gets, and made to create the tightest
    simplest code you can imagine, as long as you are able to understand
    the complexities of the language. Within the programming language
    itself and the Standard Template Library, are miles and miles of code
    that you will never have to think about.*/

    //////////////////////////////////////
    //
    // Reads words from stdin/cin and reprint
    // them suffix-sorted
    //
    int main()
    {

    /*Here is our first real container. It is a vector! Vectors are the
    friendliest most primitive universal containers in the language, and
    unless you know of a good reason to use another container you can use
    them. They can even hold costom types built from complex classes. In
    this case we have a String.*/

    vector<string> words; // vector because sort needs random access
    iterators
    string read;

    ////////////////////////////
    //
    // reads from stdin/cin until
    // eof
    //

    while ( getline(cin,read) )
    words.push_back(read);

    /*Here you can see the std::cin function is using some low level
    controlls, to push onto the vectors stack. Containers have their own
    controls, like pushing and popping on and off the stack. Some of them
    have special controlls to sort through them, insert into the middle,
    and search and compare 2 dimensional containers almost like regex
    matching.*/

    /*Can you imagine what would happen if we wanted to fill an array of
    memory with an endless list? When we make arrays we have to either
    start building them on the free store, which can spill out into memory
    with pointers, or we have to declare them. Either way it is going to
    be a lot more work, and restrict us to working with a specific number
    of characters.*/

    ////////////////////////////
    //
    // uses standard sort algorithm
    // but with less_rev
    //
    sort(words.begin(), words.end(), less_rev() );

    /*Here we are using the standard sort algorithm, but we add our own
    class (or actually a structure to it), that takes everything sort
    returns, and sends it into the structure that automatically calls its
    own function. So that saved us miles of code! Just like our little
    container saved us a lot of work, and prevents us from worrying about
    going out of bounds in memory.*/

    ////////////////////////////
    //
    // shows results
    //

    /*Here we create our own string vector, but we just create the
    iterator, that is referenced in memory to the beginning of our word
    list vector. This saves us tons of work as well that we would have to
    hardcode in C in order to flip through the list. What we have here is
    practically perl or php!*/

    for (vector<string>::const_iterator i=words.begin();
    i!=words.end();
    i++)

    /*Because our iterator is just a reference in memory to begin with, we
    use another low level control, called a pointer that will print off
    everything in the list, starting from the beginning.*/

    cout << (*i) << endl;


    return 0; // success!
    }

    /*COOL RIGHT!? That covers about everything in C++! You just need
    practice working with it, and to get some good books on the subject.
    The best reference I have on C++ in Bjarne Stroustrup's manuscript on
    his own language, but it is not easy to teach yourself from it. You
    have to buy all the other good books and work through the examples. I
    recommend all the C++ books from Orielly.

    SOMEONE NEEDS TO PUBLISH A BOOK ON WORKING WITH STRINGS IN C++*/
     
    CoreyWhite, Mar 30, 2007
    #1
    1. Advertising

  2. In article <>,
    CoreyWhite <> wrote:
    >/* The basics of C++ are Classes, that build Types. Which are used to
    >create quick and dirty routines in the smallest possible space.



    "quick and dirty" and "smallest possible space" went out
    of fashion about the time that Apple ][ BASIC did. (Smallest
    space is still a concern for embedded programmers, but they
    generally want something carefully controlled, not something
    "quick and dirty.)

    Classes and types exist to support Abstraction, which is
    usually not quick to work out well. And Abstraction is dear
    friends with Program Correctness and Program Maintainability,
    both of whom are generous with whitespace and comments
    in persuit of their causes.

    I don't think I would much trust examples created by someone
    who expressed such a strange view of the role of classes
    and types.


    Now, as I see that this is posted to comp.lang.c: did you have
    a C point? comp.lang.c is *not* comp.lang.c++.advocacy -- heck,
    comp.lang.c is not even comp.lang.c.advocacy . Use the tool
    that is appropriate for your job.
    --
    Is there any thing whereof it may be said, See, this is new? It hath
    been already of old time, which was before us. -- Ecclesiastes
     
    Walter Roberson, Mar 30, 2007
    #2
    1. Advertising

  3. CoreyWhite

    CoreyWhite Guest

    On Mar 30, 3:30 pm, -cnrc.gc.ca (Walter Roberson)
    wrote:
    > In article <>,
    >
    > CoreyWhite <> wrote:
    > >/* The basics of C++ are Classes, that build Types. Which are used to
    > >create quick and dirty routines in the smallest possible space.

    >
    > "quick and dirty" and "smallest possible space" went out
    > of fashion about the time that Apple ][ BASIC did. (Smallest
    > space is still a concern for embedded programmers, but they
    > generally want something carefully controlled, not something
    > "quick and dirty.)
    >
    > Classes and types exist to support Abstraction, which is
    > usually not quick to work out well. And Abstraction is dear
    > friends with Program Correctness and Program Maintainability,
    > both of whom are generous with whitespace and comments
    > in persuit of their causes.
    >
    > I don't think I would much trust examples created by someone
    > who expressed such a strange view of the role of classes
    > and types.
    >
    > Now, as I see that this is posted to comp.lang.c: did you have
    > a C point? comp.lang.c is *not* comp.lang.c++.advocacy -- heck,
    > comp.lang.c is not even comp.lang.c.advocacy . Use the tool
    > that is appropriate for your job.
    > --
    > Is there any thing whereof it may be said, See, this is new? It hath
    > been already of old time, which was before us. -- Ecclesiastes


    When I learned C, we coded up our algorithms using C++ but only the
    very basics. My professor was trying to teach me math, and
    algorithms. But C++ can handle strings just as easily as C can do
    complicated calculations. That's the real advantage to C++. Perhaps
    there is also an advantage as far as coding libraries to handle GUI
    programming, because I don't see much of that with C either. When
    dealing with graphics you have to look at graphics in terms of strings!
     
    CoreyWhite, Mar 30, 2007
    #3
  4. "CoreyWhite" <> wrote
    >
    > SOMEONE NEEDS TO PUBLISH A BOOK ON WORKING WITH
    > STRINGS IN C++*/
    >

    I don't want to discourage people from using C++. In places it is the best
    language to use. However there are also many powerful reasons for not going
    the object-oriented route. Proselytisation for another language in
    comp.lang.c is not appropriate.

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
     
    Malcolm McLean, Mar 30, 2007
    #4
  5. CoreyWhite

    Jack Klein Guest

    On Fri, 30 Mar 2007 19:30:23 +0000 (UTC), -cnrc.gc.ca
    (Walter Roberson) wrote in comp.lang.c:

    > In article <>,
    > CoreyWhite <> wrote:
    > >/* The basics of C++ are Classes, that build Types. Which are used to
    > >create quick and dirty routines in the smallest possible space.


    **** PLEASE DON'T FEED TROLLS *****

    Many of us kill-filtered this troll long ago, and would not see him at
    all if people like you and Nick Keighley did not rise to the bait and
    reply.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Mar 31, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Petr Prikryl
    Replies:
    3
    Views:
    278
    Dieter Maurer
    Sep 21, 2005
  2. aum
    Replies:
    1
    Views:
    350
  3. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    802
    Malcolm
    Jun 24, 2006
  4. CoreyWhite
    Replies:
    5
    Views:
    333
    Jack Klein
    Mar 31, 2007
  5. Vicent Giner

    Using Python for programming algorithms

    Vicent Giner, May 17, 2008, in forum: Python
    Replies:
    50
    Views:
    1,623
    sturlamolden
    May 21, 2008
Loading...

Share This Page