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;
}
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;
}