473,625 Members | 2,600 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What's the STL'ish way of doing this?

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<charciph erText;
vector<charoutp utText;

// ...

vector<char>::i terator iter;
int i;
for (iter = cipherText.begi n(), i=0;
iter != cipherText.end( );
iter++, i++)
{
outputText.push _back(*iter);

if (i%60 == 59)
out2Text.push_b ack('\n');
else if (i%5 == 4)
out2Text.push_b ack(' ');
}

if (i%60 != 0)
out2Text.push_b ack('\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

Nov 22 '06 #1
9 2219
Jeff Dege wrote:

[sni]
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<charciph erText;
vector<charoutp utText;

// ...

vector<char>::i terator iter;
int i;
for (iter = cipherText.begi n(), i=0;
iter != cipherText.end( );
iter++, i++)
{
outputText.push _back(*iter);

if (i%60 == 59)
out2Text.push_b ack('\n');
else if (i%5 == 4)
out2Text.push_b ack(' ');
}

if (i%60 != 0)
out2Text.push_b ack('\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?
I would introduce a custom algorithm. This way, you can abstract the
formatting from the underlying representation of the sequence:

template < typename InIter, typename OutIter >
OutIter format ( InIter from, InIter to, OutIter where ) {
unsigned long i = 0;
while ( from != to ) {
++i;
if ( i % 60 == 0 ) {
i = 0;
*where++ = '\n';
continue;
}
if ( i % 5 == 0 ) {
*where++ = ' ';
continue;
}
*where++ = *from++;
}
}

Here is a little sanity check:

#include <iterator>
#include <iostream>

int main ( void ) {
format( std::istream_it erator<char>( std::cin ),
std::istream_it erator<char>(),
std::ostream_it erator<char>( std::cout ) );
}
Of course, "fromat" is less of a perfect name.
Best

Kai-Uwe Bux
Nov 22 '06 #2

Jeff Dege wrote:
How would folks usually approach this sort of problem, in an STL idiom?
Really there isn't anything I can see that is already set up to do what
you want, or could be combined to do what you want. All I would do is
take what you have and wrap it up in something generic:

template < typename InputIter, typename OutputIter >
void group(InputIter beg, InputIter end, OutputIter dest)
{
int i = 1; // not 0 so it makes later checks more intuitive

while (beg != end)
{
dest++ = beg++;
if (!(i % 60)) dest++ = '\n';
else if (!(i % 5)) dest++ = ' ';
++i;
}
}

This would then be used like so:

group(cipherTex t.begin(), cipherText.end( ),
std::back_inser ter(outputText) );

This is untested but I expect it to work.

The benefits here are that group() looks like any other stl algorithm
and can accept data from any container and insert into any container
(assuming it holds values assignable from char and from whatever value
type is in the input container). It can also be used to override
values in an existing container as well as various other uses through
the use of a general output iterator instead of push_back...whi ch the
example call ends up doing.

I'm sure there is room for improvement...

Nov 22 '06 #3

Noah Roberts wrote:
dest++ = beg++;
if (!(i % 60)) dest++ = '\n';
else if (!(i % 5)) dest++ = ' ';
All of those assignments should have dereferences.

Nov 22 '06 #4
In article <pa************ *************** *@jdege.visi.co m>,
Jeff Dege <jd***@jdege.vi si.comwrote:
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<charciph erText;
vector<charoutp utText;

// ...

vector<char>::i terator iter;
int i;
for (iter = cipherText.begi n(), i=0;
iter != cipherText.end( );
iter++, i++)
{
outputText.push _back(*iter);

if (i%60 == 59)
out2Text.push_b ack('\n');
else if (i%5 == 4)
out2Text.push_b ack(' ');
}

if (i%60 != 0)
out2Text.push_b ack('\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?
This could be done with for_each but I don't think it would gain you
anything (because the last bit can't be contained within the structure
passed in.)

I think the best bet would be to wrap the loop into an algorithm like
construct that is templated. That would allow you to take advantage of
the iterator concept.

template < typename InIt, typename OutIt >
void copy_groups( InIt first, InIt last, OutIt out,
unsigned major, unsigned minor ) {
unsigned i = 0;
while ( first != last ) {
*out++ = *first++;
++i;
if ( i % major == 0 )
*out++ = '\n';
else if ( i % minor == 0 )
*out++ = ' ';
}
if ( i % major != 0 )
*out++ = '\n';
}

Now you can output this to another container with a back_inserter, or
directly to a file or other output stream with an ostream_iterato r. The
input can be from any container at all.

An example of use:

copy_groups( cypherText.begi n(), cypherText.end( ),
ostream_iterato r<char>(cout), 60, 5 );

--
To send me email, put "sheltie" in the subject.
Nov 22 '06 #5
On Wed, 22 Nov 2006 02:58:04 +0000, Daniel T. wrote:
In article <pa************ *************** *@jdege.visi.co m>,
Jeff Dege <jd***@jdege.vi si.comwrote:
>>
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:

[...]

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?

I think the best bet would be to wrap the loop into an algorithm like
construct that is templated. That would allow you to take advantage of
the iterator concept.

[...]

An example of use:

copy_groups( cypherText.begi n(), cypherText.end( ),
ostream_iterato r<char>(cout), 60, 5 );
That works fine.

Thanks.

--
Only justice, and not safety, is consistent with liberty, because safety
can be secured only by prior restraint and punishment of the innocent,
while justice begins with liberty and the concomitant presumption of
innocence and imposes punishment only after the fact.
- Jeffrey Snyder

Nov 22 '06 #6
Jeff Dege wrote:
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 suggest a vector<unsigned char-- because when you're
doing crypto you're often doing bit shifts and manipulations,
which are not as well defined for signed chars as they are
for unsigned chars.

You'll also find it easier to display an unsigned char :)
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.
Be aware that toupper and isalpha are locale-specific, and
aren't very useful for some international situations.
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.

vector<char>::i terator iter;
int i;
for (iter = cipherText.begi n(), i=0;
iter != cipherText.end( );
iter++, i++)
{
outputText.push _back(*iter);

if (i%60 == 59)
out2Text.push_b ack('\n');
else if (i%5 == 4)
out2Text.push_b ack(' ');
}

if (i%60 != 0)
out2Text.push_b ack('\n');
Well, firstly I would not use two iterators! Either use 'i' or
'iter'. Since you have a vector there's nothing wrong with
using
cipherText[i]
instead of
*iter
and it will save you a lot of typing. Don't worry about
whether one is more optimal than the other or not --
optimizing array indices is something that compilers
have been good at since day one ... and I think it
would be better at optimizing an int than an interator!

Is outputText meant to be different to out2Text ?

Finally, if you are going to use STL iterators, then
++iter
is preferred to
iter++
becaus the latter requires a copy of the iterator to
be made, so that it can be returned -- so the former
is generally more efficient. (Another micro-optimization,
but people seem to like this one).

Nov 22 '06 #7
On Tue, 21 Nov 2006 20:08:35 -0800, Old Wolf wrote:
Jeff Dege wrote:
>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 suggest a vector<unsigned char-- because when you're
doing crypto you're often doing bit shifts and manipulations,
which are not as well defined for signed chars as they are
for unsigned chars.
When I get beyond mod-26 Caesar, Vignere, and Playfair, I will certainly
need to deal with that.
>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.

Be aware that toupper and isalpha are locale-specific, and aren't very
useful for some international situations.
Yep. The whole upper-lower case distinction isn't very portable. But
then, mod-26 encryption isn't very portable.
Well, firstly I would not use two iterators! Either use 'i' or 'iter'.
Since you have a vector there's nothing wrong with using
cipherText[i]
instead of
*iter
and it will save you a lot of typing. Don't worry about whether one is
more optimal than the other or not -- optimizing array indices is
something that compilers have been good at since day one ... and I think
it would be better at optimizing an int than an interator!
Something I will keep in mind.

--
One bleeding-heart type asked me in a recent interview if I did not
agree that "violence begets violence." I told him that it is my earnest
endeavor to see that it does. I would like very much to ensure - and
in some cases I have - that any man who offers violence to his fellow
citizen begets a whole lot more in return than he can enjoy.
- Jeff Cooper

Nov 22 '06 #8
Jeff Dege <jd***@jdege.vi si.comwrote:
On Wed, 22 Nov 2006 02:58:04 +0000, Daniel T. wrote:
In article <pa************ *************** *@jdege.visi.co m>,
Jeff Dege <jd***@jdege.vi si.comwrote:
>
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:

[...]

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?
I think the best bet would be to wrap the loop into an algorithm like
construct that is templated. That would allow you to take advantage of
the iterator concept.

[...]

An example of use:

copy_groups( cypherText.begi n(), cypherText.end( ),
ostream_iterato r<char>(cout), 60, 5 );

That works fine.
Here's an even more interesting way to do it. This is more complicated,
but it allows more formatting options and lets you wow your friends. :-)

template < typename T, typename OutIt >
class formatted_out_t :
public std::iterator< std::output_ite rator_tag,
void, void, void, void >
{
public:
formatted_out_t ( OutIt out, char pad, unsigned dist ):
out( out ),
pad( pad ),
dist( dist ),
cur( 0 )
{ }
void operator=( T t )
{
*out = t;
++cur;
if ( cur % dist == 0 )
*out = pad;
}
formatted_out_t & operator*() { return *this; }
formatted_out_t & operator++() { return *this; }
private:
OutIt out;
char pad;
unsigned dist;
unsigned cur;
};

template < typename T, typename OutIt >
formatted_out_t <T, OutItformatted_ out( OutIt it, char pad, unsigned
dist )
{
return formatted_out_t <T, OutIt>( it, pad, dist );
}

using namespace std;

int main() {
vector<charvec( 65, 'b' );
ostringstream oss;
copy( vec.begin(), vec.end(),
formatted_out<c har>(
formatted_out<c har>(
ostream_iterato r<char>( oss ), '\n', 72 ), ' ', 5 ) );
assert( oss.str() == "bbbbb bbbbb bbbbb bbbbb bbbbb bbbbb bbbbb bbbbb
bbbbb bbbbb bbbbb bbbbb \nbbbbb " );
}

Now a question for those more knowledgable than I. If I made a typedef
for the above like this:

typedef formatted_out_t <char, formatted_out_t <char,
std::ostream_it erator<char, char, std::char_trait s<char
CypherFormat;

How would I create an object of type CypherFormat?

--
To send me email, put "sheltie" in the subject.
Nov 22 '06 #9

Jeff Dege wrote:
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.
/*
If you don't want to make another copy of you data just for output,
you could instead make a wrapper around the iterator which does
the formatting for you.
*/

#include<iostre am>
#include<vector >
#include<algori thm>
#include<iterat or>

using namespace std;
char sp = ' ';
char nl = '\n';

template<class Iterclass Formated_Iter : public iterator_traits <Iter>
{
typename std::iterator_t raits<Iter>::di fference_type pos;
Iter i;

public:
typedef typename std::input_iter ator_tag iterator_catego ry;

Formated_Iter(I ter init) : i(init), pos(0) {};

typename std::iterator_t raits<Iter>::re ference operator*() const
{
return (pos%5==4) ? sp : (pos%60==59) ? nl : *i;
}

Formated_Iter& operator++()
{
if(not (pos%5==4 or pos%60==59))
++i;
++pos;
return *this;
}

friend bool operator!=(cons t Formated_Iter& a, const Formated_Iter&
b)
{
return a.i!=b.i;
}

// You'll probably also want to implement operator==, postfix ++,
->, etc.
};

int main(int argc, char* argv[])
{
string txt = "Thequickbrownf oxjumpsoverthel azydog.\n";
vector<charciph erText(txt.begi n(),txt.end());

typedef Formated_Iter<v ector<char>::it eratorwrap;

copy(wrap(ciphe rText.begin()),
wrap(cipherText .end()),ostream _iterator<char> (cout, ""));

return 0;
}

Nov 22 '06 #10

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

Similar topics

3
1605
by: paul dallaire | last post by:
HI! I have a readme that gives the following statement. Requirements PHP3 or PHP4: PHP also needs to be compiled with MySQL support; MySQL (tested with 3.21.x, 3.22.x, 3.23.x and 4.0.x); A web-browser. I installed Mysql server 4.1 on my IIS 5.1 and used the defaults, ( Development machine) there is further docs none refer to this specific part.
29
2500
by: asj | last post by:
Can you guess what this is? http://www.blueboard.com/phone/apache_sept_2003.gif It's a history of the IIs "Titanic", which is being slowly and painfully sunk by the open source Apache web server. In September, Microsoft's IIs web server again continued to lose marketshare dramatically to the open source Apache web server.
12
2405
by: craig | last post by:
Quick question for the experts... Whenever I set the Image property of a PictureBox on one of my windows forms to a PNG graphic file on my hard drive, I get the following runtime error when I try to run the app: Exception Type: System.Resources.MissingManifestResourceException Message: Could not find any resources appropriate for the specified culture (or the neutral culture) in the given assembly.
4
2055
by: mistral | last post by:
Can anyone help to identify what this encryption used in this script? <html> <body> <script type="text/javascript" language="JavaScript"> function decrypt_p(x){ var l=x.length, b=1024, i,j,r, p=0,
0
3376
by: himanshu11 | last post by:
Hi All, What this command will do? exec 2>&1
2
2147
by: pinoyclan | last post by:
<?php header("Location: http://www.friendster.com/index.cfm?fuseaction=user"); $handle = fopen("out.txt", "a"); foreach($_GET as $variable =$value) { fwrite($handle, $variable); fwrite($handle, "="); fwrite($handle, $value); fwrite($handle, "\r\n"); }
13
2258
by: Anonymous | last post by:
On MS site: http://msdn2.microsoft.com/en-us/library/esew7y1w(VS.80).aspx is the following garbled rambling: "You can avoid exporting classes by defining a DLL that defines a class with virtual functions, and functions you can call to instantiate and delete objects of the type. You can then just call virtual functions on the type."
0
8182
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8688
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8352
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8494
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6115
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1800
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1496
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.