473,385 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

/* 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<stringwords; // 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++*/

Mar 30 '07 #1
5 2139
In article <11*********************@n59g2000hsh.googlegroups. com>,
CoreyWhite <Co********@gmail.comwrote:
>/* 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
Mar 30 '07 #2
On Mar 30, 3:30 pm, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)
wrote:
In article <1175282138.852333.11...@n59g2000hsh.googlegroups. com>,

CoreyWhite <CoreyWh...@gmail.comwrote:
/* 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!

Mar 30 '07 #3
Walter Roberson wrote, On 30/03/07 20:30:
In article <11*********************@n59g2000hsh.googlegroups. com>,
CoreyWhite <Co********@gmail.comwrote:
>/* 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
Mar 30 '07 #4
"CoreyWhite" <Co********@gmail.comwrote
>
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

Mar 30 '07 #5
On Fri, 30 Mar 2007 19:30:23 +0000 (UTC), ro******@ibd.nrc-cnrc.gc.ca
(Walter Roberson) wrote in comp.lang.c:
In article <11*********************@n59g2000hsh.googlegroups. com>,
CoreyWhite <Co********@gmail.comwrote:
/* 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
Mar 31 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bo Xu | last post by:
Object of Combination By Bo Xu Introduction A combination of n things, taken s at a time, often referred as an s-combination out of n, is a way to select a subset of size s from a given set of...
134
by: evolnet.regular | last post by:
I've been utilising C for lots of small and a few medium-sized personal projects over the course of the past decade, and I've realised lately just how little progress it's made since then. I've...
6
by: @(none) | last post by:
Hi, I need to learn the necessary and sufficient C programming knowledge in order to be able to implement number theory and graph theory algorithms (as RSA or Dijkstra algorithms). No system...
42
by: Kevin Spencer | last post by:
Is it just me, or am I really observing a trend away from analysis and probem-solving amongst programmers? Let me be more specific: It seems that every day, in greater numbers, people are coming...
78
by: wkehowski | last post by:
The python code below generates a cartesian product subject to any logical combination of wildcard exclusions. For example, suppose I want to generate a cartesian product S^n, n>=3, of that...
1
by: roxorsoxor2345 | last post by:
I am very sorry if this is not the appropriate group to post this question. I currently have a program that tests strings to see if they are palindromes. My input file looks something like...
4
by: CoreyWhite | last post by:
/* 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...
9
by: | last post by:
I am interested in scanning web pages for content of interest, and then auto-classifying that content. I have tables of metadata that I can use for the classification, e.g. : "John P. Jones" "Jane...
46
by: Chris Stewart | last post by:
I've always had an interest in Python and would like to dabble in it further. I've worked on a few very small command line programs but nothing of any complexity. I'd like to build a really...
53
by: Vicent Giner | last post by:
Hello. I am new to Python. It seems a very interesting language to me. Its simplicity is very attractive. However, it is usually said that Python is not a compiled but interpreted programming...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.