D - Could you show me the equivalent in D?
#include <vector> #include <algorithm> #include <iterator> #include <iostream> #include <functional> int main () { vector<int> v; copy(istream_iterator<int>(cin), istream_iterator<int>(), back_inserter()); sort(v.begin(), v.end(), greater<int>()); copy(v.begin(), v.end(), ostream_iterator<int>(Cout, " ")); return 0; // to make MSVC happy } I haven't tested it, so the might be some typos. This Program reads numer from the user until he presses End of file (^Z in windows). Than this numbers are sorted in a descanding order. Than the program shows this umbers seprerated by a white space.
Aug 10 2003
import stream;
import c.stdio;
int main( char[][] args ) {
int [] ilist;
try {
int i;
while ( true ) {
stdin.read( i ); // I think this throws an exception at eof ...
you'll have to check this.
ilist ~= i; // append i to array;
}
}catch ( Exception ignore ) {
}
ilist.sort; // sort the array (not sure if there is a way to set the
sort algo)
for ( int idx = 0; idx < ilist.length; idx ++ ) {
printf( "%d ", ilist[i] );
}
return 0;
}
or look about at the templated foreach classes and how to use nested
functions and you get something like
template MyAlgoTemplateClass( T :T[] ){
// I believe these have to be delegates to allow the inner functions to
work.
alias bit delegate( out T ) getFptr;
alias void delegate(T) putFptr;
void fill( T[] list, getFptr ) { T v; while ( getFptr( v ) ) { list ~=
v; } }
void foreach( T[] list, putFptr ) { for( int i = 0; i < list.length; i
++ ) { putFptr( list[i] ); } }
}
int main( char[][] args ) {
int [] ilist;
Stream input = stdin;
// WARNING this function is only valid within this scope of this method
as it pulls input from the
// the parent stack frame rather than creating a closure by shallow
copying the stack frames it needs
// so only allowable to pass it to a function that uses it and never
stores is, (as we're in main its not
// a problem; but the callee can not return a fp to a inner function for
the caller to use.
// and any methods/function that is potentially passed an inner function
pointer
// you could just use stdin direct but I wanted to show that inner
functions can see the parent stack frame.
bit get_next( out int i ) {
try { input.read(i); } catch (Exception e ) { return false }
return true;
}
void put_next( int i ) { printf("%d ", i ); }
instance MyAlgoTemplateClass( int[] ) algo;
try {
alogo.fill( ilist, get_next );
}catch ( Exception ignore ) {
}
ilist.sort;
alogo.foreach( ilist, put_next );
return 0;
}
this is untested code, but I'm sure you get the idea.
Stream.read( out int ) reads 4 bytes (int )
you may want to change the code to read a string and do atoi instead.
not sure if there is a scanf( "%d", &i ); anywhere .... happy hunting.
"Helium" <Helium_member pathlink.com> wrote in message
news:bh56u2$183c$1 digitaldaemon.com...
#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <functional>
int main ()
{
vector<int> v;
copy(istream_iterator<int>(cin), istream_iterator<int>(),
back_inserter());
sort(v.begin(), v.end(), greater<int>());
copy(v.begin(), v.end(), ostream_iterator<int>(Cout, " "));
return 0; // to make MSVC happy
}
I haven't tested it, so the might be some typos. This Program reads numer
from
the user until he presses End of file (^Z in windows). Than this numbers
are
sorted in a descanding order. Than the program shows this umbers
seprerated by a
white space.
Aug 10 2003








"Mike Wynn" <mike.wynn l8night.co.uk>