C
CoreyWhite
/* 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++*/
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++*/