simple sorting a Range program can not run(work or printout)

E

eric

Dear advanced c/g++ programers:

I copy and test a piece simple Sorting a range program from page 268
of (c++ cookbook)
you can get its source code from
http://examples.oreilly.com/9780596007614/
-----------------------
// Example 7-6. sorting
#include <iostream>
#include <istream>
#include <string>
#include <list>
#include <vector>
#include <algorithm>
#include <iterator>
#include "utils.h" // for printContainer(): see 7.10

using namespace std;

int main() {

cout << "Enter a series of strings: ";
istream_iterator<string> start(cin);
istream_iterator<string> end; // This creates a "marker"
vector<string> v(start, end);

// thie sort standard altorithm will sort elements ina range. it
// requires a random-access iterator, so it works for a vector.
sort(v.begin(), v.end());
printContainer(v);

random_shuffle(v.begin(), v.end()); // See 7.2

string* arr = new string[v.size()];
// Copy the elements into the array
copy(v.begin(), v.end(), &arr[0]);

// sort works on any kind of range, so long as its arguments
// behave like random-access iterators
sort(&arr[0], &arr[v.size()]);
printRange(&arr[0], &arr[v.size()]);

// Create a list with the same elements
list<string> lst(v.begin(), v.end());

lst.sort(); // the standalone version of sort won't work; you have
// to use list::sort. Note, consequently, that you
// can't sort only parts of a list.

printContainer(lst);
}
--------------------------------------------------------------------------------------------------------------
my utils.h(this is based on Example 7-12 at same book page 283) in
same directory is
-------------------------------------------------------------------------------------------------------------
// Example 7-12. Writing your own printing function
#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
#include <vector>

using namespace std;

template<typename C>
void printContainer(const C& c, char delim = ',', ostream& out = cout)
{
printRange(c.begin(), c.end(), delim, out);
}

template<typename Fwd>
void printRange(Fwd first, Fwd last, char delim = ',', ostream& out =
cout) {
out << "{";
while (first != last) {
out << *first;
if (++first != last)
out << delim << ' ';
}
out << "}" << endl;
}
 
V

Victor Bazarov

[...] a z b y c x d w

I took your code and moved the utils.h into the same source where 'main'
was, then compiled it and ran it, and got:
Enter a series of strings: a z b y c x d w
^Z
{a, b, c, d, w, x, y, z}
{a, b, c, d, w, x, y, z}
{a, b, c, d, w, x, y, z}

Seems to work fine...

V
 
E

eric

[...] a z b y c x d w

I took your code and moved the utils.h into the same source where 'main'
was, then compiled it and ran it, and got:
Enter a series of strings: a z b y c x d w
^Z
{a, b, c, d, w, x, y, z}
{a, b, c, d, w, x, y, z}
{a, b, c, d, w, x, y, z}

Seems to work fine...

V

my system need to use control D
rather than control Z
that's why I don't see output
Thanks anyway
 
V

Victor Bazarov

[...] a z b y c x d w

I took your code and moved the utils.h into the same source where 'main'
was, then compiled it and ran it, and got:
Enter a series of strings: a z b y c x d w
^Z
{a, b, c, d, w, x, y, z}
{a, b, c, d, w, x, y, z}
{a, b, c, d, w, x, y, z}

Seems to work fine...

V

my system need to use control D
rather than control Z
that's why I don't see output

Well, don't put control Z then. My system ignores control D and needs
control Z to indicate the *end of the input*. Put your numbers in a
file and read from the file instead of from the keyboard (use
command-line redirection, "< filename", to make the file your "standard
input"). And if your problem is in the control-Z versus control-D, then
learn your damn system. Doesn't it say in the book what the hell that
^Z is? Be inquisitive FCOL.
Thanks anyway

Whavever.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top