CTTL on
SourceForge |
Download
Latest |
Documentation
Index |
Library
News |
CVS
Repository |
Other
Links |
Demonstrates string-like behavior of CTTL edge objects. The program applies STL sort algorithm to a vector of edges.
// sample code: edge_sort.cpp // demonstrates STL sort algorithm applied to a vector of edges #include <iostream> #include "cttl/cttl.h" #include <vector> #include <algorithm> #include <iterator> // needed for ostream_iterator int main() { using namespace cttl; input<> inp( "one\ntwo\nthree\nfour\nfive" ); std::cout << "--------------------------------" << std::endl; std::cout << "\t Input data:" << std::endl; std::cout << "--------------------------------" << std::endl; std::cout << inp.text() << std::endl; edge<> universe = new_edge( inp ); int line_count = universe.second.line(); std::vector< edge<> > vector_sorted_edges; // each edge corresponds to a line in the input file for ( int ln = 1; ln <= line_count; ++ln ) { edge<> tmp = new_edge( universe ); // create new edge tmp.first.go_line( ln ); // navigate edge to the beginning/ending of line ln tmp.second.go_line_end( ln ); vector_sorted_edges.push_back( tmp ); // add edge to the vector to be sorted } std::cout << "--------------------------------" << std::endl; std::cout << "\t Unsorted vector of edges:" << std::endl; std::cout << "--------------------------------" << std::endl; std::copy( vector_sorted_edges.begin(), vector_sorted_edges.end(), std::ostream_iterator< std::string >( std::cout, "\n" ) ); std::sort( vector_sorted_edges.begin(), vector_sorted_edges.end() ); std::cout << "--------------------------------" << std::endl; std::cout << "\t Sorted vector of edges:" << std::endl; std::cout << "--------------------------------" << std::endl; std::copy( vector_sorted_edges.begin(), vector_sorted_edges.end(), std::ostream_iterator< std::string >( std::cout, "\n" ) ); std::cout << "--------------------------------" << std::endl; return 0; }
-------------------------------- Input data: -------------------------------- one two three four five -------------------------------- Unsorted vector of edges: -------------------------------- one two three four five -------------------------------- Sorted vector of edges: -------------------------------- five four one three two --------------------------------
Permission to copy, use, modify, sell and distribute this document is granted provided this copyright notice appears in all copies. This document is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.