This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


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;
}
---------------------------------------------------------------------------------------------------------------------------
it can compile but when I run it
-----------
eric@eric-laptop:~/cppcookbook/ch7$ ./a.out
Enter a series of strings: a z b y c x d w^Z
[1]+  Stopped                 ./a.out
----------------------------------
it sould suppose to show
-----
a b c d w x y z

etc.
----------------------
I am using g++ 4.5.2 and linux kernel 2.6.35-25
thanks your help a lot in advance, Eric


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]