Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Programming algorithms with strings in C++, and transition from C by example!

Reply
Thread Tools

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

 
 
CoreyWhite
Guest
Posts: n/a
 
      03-30-2007
/* 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++*/

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      03-30-2007
In article <(E-Mail Removed). com>,
CoreyWhite <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
CoreyWhite
Guest
Posts: n/a
 
      03-30-2007
On Mar 30, 3:30 pm, (E-Mail Removed)-cnrc.gc.ca (Walter Roberson)
wrote:
> In article <(E-Mail Removed). com>,
>
> CoreyWhite <(E-Mail Removed)> 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!

 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      03-30-2007
Walter Roberson wrote, On 30/03/07 20:30:
> In article <(E-Mail Removed). com>,
> CoreyWhite <(E-Mail Removed)> wrote:
>> /* The basics of C++ are Classes, that build Types. Which are used to
>> create quick and dirty routines in the smallest possible space.


<snip>

> 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.


I think you will find it is a troll. Ignore it or report it, although
whether Google take not of reports is another matter.

Cross posts removed, since I know you read this group.
--
Flash Gordon
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      03-30-2007
"CoreyWhite" <(E-Mail Removed)> 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

 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      03-31-2007
On Fri, 30 Mar 2007 19:30:23 +0000 (UTC), http://www.velocityreviews.com/forums/(E-Mail Removed)-cnrc.gc.ca
(Walter Roberson) wrote in comp.lang.c:

> In article <(E-Mail Removed). com>,
> CoreyWhite <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Python for programming algorithms Vicent Giner Python 50 05-21-2008 05:50 PM
Programming algorithms with strings in C++, and transition from C by example! CoreyWhite C++ 4 03-31-2007 03:40 AM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
ANN: pygene0.12 - Genetic Programming&Algorithms Library aum Python 1 12-11-2005 07:35 PM
Simpler transition to PEP 3000 "Unicode only strings"? Petr Prikryl Python 3 09-21-2005 06:39 PM



Advertisments