J
Jeff Dege
I've been programming in C++ for a good long while, but there are aspects
of the language I've never needed, and hence never bothered to really
learn.
It's the curse of working on a developed product - many fundamental issues
were set long ago, and there's no reason to go back and revisit them just
because the language has come out with a new set of tools.
Case in point - the Standard Template Library. We fixed on a set of
collection classes long before the STL was available for the compilers we
were using. I've read about it, played around a bit with it, but haven't
used it on the day-to-day basis to completely internalize its capabilities
and the normal idioms with which it is used.
So I've decided to spend some time on learning the STL.
As a testbed, I'm writing some crypto programs - encrypt, decrypt,
calculate statistics to help in breaking, etc.
I'm keeping the text, both plain- and cipher-, in vector<char>'s. I've
figured out how to use algorithm copy() with stream iterator adapters to
read and write the text vectors from stdio and/or files.
I've figured out how to use algorithm transform() and ::toupper() to
change everything to uppercase, and how to use algorithm remove_if() and
::isalpha() to remove everything that isn't a letter.
What I'm trying to do next is to output the text in cipher groups, five
characters to a group, 12 groups to a line.
This works:
vactor<char> cipherText;
vector<char> outputText;
// ...
vector<char>::iterator iter;
int i;
for (iter = cipherText.begin(), i=0;
iter != cipherText.end();
iter++, i++)
{
outputText.push_back(*iter);
if (i%60 == 59)
out2Text.push_back('\n');
else if (i%5 == 4)
out2Text.push_back(' ');
}
if (i%60 != 0)
out2Text.push_back('\n');
But this seems a very non-STL'ish way of doing things.
How would folks usually approach this sort of problem, in an STL idiom?
--
The citizen sees nothing wrong, in the sense of robbing a neighbor
is wrong to him, in turning the tables upon...[government] whenever
the opportunity offers. When he steals anything from it he is only
recovering his own, with fair interest and a decent profit. Two gangs
stand thus confronted: on the one hand the gang of drones and exploiters
constituting the government, and on the other hand the body of prehensile
and enterprising citizens...The difference between the two gangs - of
professionals and amateurs - is that the former has the law on its side,
and so enjoys an unfair advantage.
- H. L. Mencken
of the language I've never needed, and hence never bothered to really
learn.
It's the curse of working on a developed product - many fundamental issues
were set long ago, and there's no reason to go back and revisit them just
because the language has come out with a new set of tools.
Case in point - the Standard Template Library. We fixed on a set of
collection classes long before the STL was available for the compilers we
were using. I've read about it, played around a bit with it, but haven't
used it on the day-to-day basis to completely internalize its capabilities
and the normal idioms with which it is used.
So I've decided to spend some time on learning the STL.
As a testbed, I'm writing some crypto programs - encrypt, decrypt,
calculate statistics to help in breaking, etc.
I'm keeping the text, both plain- and cipher-, in vector<char>'s. I've
figured out how to use algorithm copy() with stream iterator adapters to
read and write the text vectors from stdio and/or files.
I've figured out how to use algorithm transform() and ::toupper() to
change everything to uppercase, and how to use algorithm remove_if() and
::isalpha() to remove everything that isn't a letter.
What I'm trying to do next is to output the text in cipher groups, five
characters to a group, 12 groups to a line.
This works:
vactor<char> cipherText;
vector<char> outputText;
// ...
vector<char>::iterator iter;
int i;
for (iter = cipherText.begin(), i=0;
iter != cipherText.end();
iter++, i++)
{
outputText.push_back(*iter);
if (i%60 == 59)
out2Text.push_back('\n');
else if (i%5 == 4)
out2Text.push_back(' ');
}
if (i%60 != 0)
out2Text.push_back('\n');
But this seems a very non-STL'ish way of doing things.
How would folks usually approach this sort of problem, in an STL idiom?
--
The citizen sees nothing wrong, in the sense of robbing a neighbor
is wrong to him, in turning the tables upon...[government] whenever
the opportunity offers. When he steals anything from it he is only
recovering his own, with fair interest and a decent profit. Two gangs
stand thus confronted: on the one hand the gang of drones and exploiters
constituting the government, and on the other hand the body of prehensile
and enterprising citizens...The difference between the two gangs - of
professionals and amateurs - is that the former has the law on its side,
and so enjoys an unfair advantage.
- H. L. Mencken