|
Some background
The first one I really love is the programming language C. I spent a lot of time to find it: When I was a child, I began to use Z80 assembler on precious ZX Spectrum. These days you can really master your computer, you do not need Apple, Google, Microsoft, or any other person will be able to write a program to allow. I studied in the CPU address space beyond memory paging blocks on the pretty 128K ZX Spectrum +2. Until I entered college, I only have an IBM PC machine. I Wanbian spreadsheet, trying to repair the existing Fortran program with Turkish characters to patch the keyboard driver, but also to learn a language Pascal. After that, I worked in the Central Bank of Turkey during which I learned SQL and APL.
I have been on C language have heard, but I did not come into contact with a C language compiler. Until I went to Cornell University has a Unix account, I compiled my life first hello.c file, shortly after I had my first computer ...... on the DOS partition installed DJGPP compiler It is constructed in my life first Linux kernel (my first release is a Debian), and start learning C language. Plauger the "Standard C Library" is my favorite book.
When I began to enjoy with C programming, C ++ has been widely used for over a decade. So, I use C ++ seems to be the next natural step.
In addition to ...... Well, in addition to C ++ is a mess. During that time, everyone was fascinated inheritance hierarchy, and everyone in the preparation of well-designed string class. Most hard drives are too slow, compile reliable C ++ library (Okay, I was a bit exaggerated) is no longer within the limited time, most of the CPU are trying to instantiate the template melted, the majority of people who pretend to C ++ program members of C programmers, almost put the return value of malloc to throw.
At that time, I was busy trying to build customized network economy experiment, it seems Java seems to have the edge. At least, it does not bother to put together a box. Small amounts of socket connections, and make your application compiles and runs on a wide variety of systems. Of course, AWT and Swing are ugly and bulky, but for my purposes, it does not matter.
However, just because I can not run experiments (because the laboratory has been equipped with all computers, java applications run without any problems) outside of the laboratory. So I quickly deployed to put FreeBSD on a Pentium processor has a 100Mhz, 16Mb memory, collecting dust in the corner of the machine, and has built a perl module (mod_perl) the Apache server, and then be able to work. That's when I fell in love Perl.
I love that originated in entirely practical reasons, I do not think particularly perfect Perl, and then I think many other languages are not perfect, they each have their own flaws.
Perl always reduce the amount I have to solve the particular problem of work, some because of language features, but mostly because CPAN.
For example, as a Perl programmer, as HTML parsing HTML is a solution. I had to decide either to construct the entire tree, or fluidized mode. In some cases, the former is an advantage, but the advantage is that the latter can make the memory requirements to a minimum, even in this day and age, if you're dealing with HTML documents in megabytes way still effective. Either way, these tools will not be stuck on invalid HTML, XML and non-valid HTML worked well.
There, Perl provides portability. If I do not need an operating system-specific functionality, without any modification where my perl code to run.
When I wrote some classes and package them do not have a complex structure.
C ++ Nirvana
In the past few years, C ++ as eligible students. Many smart people have begun to realize the need to cover both C ++ programmers work of the ISO committee and boost building blocks.
In a real environment, there are still 90% of the rookie generate C ++ programmers do not realize is new is a legitimate symbol of C programmers. In this regard, C ++ and Perl are very similar: Most people wrote Perl code Perl people do not realize that is not a C, Java, Python, shell, Awk or any other language you may be enumerated.
But when you see the new C ++ standard in new things, and continue to achieve western Zhejiang compiler features news, we can not restrain our excitement and curiosity.
English-calculated Exercise
This is a simple exercise, using C ++ or Perl and does not rely on external libraries, so this is a good starting point.
This is the Perl version for your reference:
#! / Usr / bin / env perl
use strict;
use warnings;
run (\ @ ARGV);
sub run {
my $ argv = shift;
my @counts;
for my $ file (@ $ argv) {
my $ count = -1;
eval {
$ Count = word_count ($ file);
1;
} Or warn "$ @";
push @counts, {
file => $ file,
word_count => $ count,
};
}
for my $ result (@counts) {
printf "% s:% d words \ n", $ result -> {file}, $ result -> {word_count};
}
}
sub word_count {
my $ file = shift;
my% words;
open my $ fh, '< ', $ file
or die "Can not open '$ file': $!";
while (my $ line = < $ fh>) {
my @words = split '', $ line;
$ Words {$ _} + = 1 for @words;
}
close $ fh;
my $ word_count;
$ Word_count + = $ _ for values% words;
return $ word_count;
}
And this is my biggest pay Perl conversion to modern C ++ above. I did not try to write special code to funny: just and Perl, I focus on writing code for it, so I feel very natural, while ensuring that the two programs are doing roughly the same thing.
#include < cerrno>
#include < cstdio>
#include < cstdlib>
#include < fstream>
#include < iostream>
#include < numeric>
#include < unordered_map>
#include < string>
#include < vector>
using std :: accumulate;
using std :: cerr;
using std :: cout;
using std :: endl;
using std :: ifstream;
using std :: make_pair;
using std :: pair;
using std :: strerror;
using std :: string;
using std :: unordered_map;
using std :: vector;
int word_count (const char * const file) noexcept (false);
int main (int argc, char * argv []) {
vector < pair < string, int>> counts {};
for (auto i = 1; i < argc; i + = 1) {
try {
counts.push_back (make_pair (argv [i], word_count (argv [i])));
} Catch (const string & e) {
cerr < < e < < endl;
counts.push_back (make_pair (argv [i], -1));
}
}
for (auto & result: counts) {
cout < < result.first < < ":" < < result.second < < "words" < < endl;
}
return 0;
}
int
word_count (const char * const file) noexcept (false) {
errno = 0;
ifstream fp (file);
{
// Does fp.fail () preserve errno?
int save_errno = errno;
if (fp.fail ()) {
throw ( "Can not open '" + string (file) + "':" + strerror (save_errno));
}
}
unordered_map < string, int> word_count {};
string word;
while (fp >> word) {
word_count [word] + = 1;
}
fp.close ();
return accumulate (
word_count.cbegin (),
word_count.cend (),
0
[] (Int sum, auto & el) {return sum + = el.second;}
);
}
20 lines of code and using #include statement may seem a bit much, but I looked up using namespace std, hate constantly input std :: ... because I like more short lines of code.
First thing to note is not visible explicit memory allocation. Container Container manage their own memory.
Second, this is a big problem: we have auto-import (autovivification)!
unordered_map < string, int> word_count {};
string word;
while (fp >> word) {
word_count [word] + = 1;
}
Third, we have lambda expressions:
return accumulate (
word_count.cbegin (),
word_count.cend (),
0
[] (Int sum, auto & el) {return sum + = el.second;}
);
In this, accumulate the internal variable is initialized to 0, and call an anonymous function, its last argument as the current value, and the next element word_count.
Now, I have to admit, I do not know is how to achieve these features, but Microsoft Visual C ++ 2015 RC runs successfully, Microsoft seems to have finally caught up with the latest developments in the field.
Current situation
However, all is not optimistic. Despite the boost libraries to fill many of the gaps, and the standard library provides an impressive member, but it is difficult to beat Perl and CPAN combination that brings convenience to write portable code anywhere in perfect running.
For example, I can find a platform-independent library that allows me to parse or create an Excel file in Excel without the need for it? This library can use clang, g ++ compiler and cl easily out of it? It seems unlikely.
I'm really very grateful to the people of the Standards Committee for their hard work, and those who develop compilers, libraries of many people. They let me do not have the time to write a C ++ program hard thinking.
It makes me really in control of my computer able to feel comfortable.
Here, I really appreciate.
You can comment on this article. |
|
|
|