Discussion:
STL algorithms and output iterators
(too old to reply)
Generic Usenet Account
2007-10-05 02:36:18 UTC
Permalink
Hi,

I am trying to play with STL algorithms that have an output iterator
type as one of their arguments (copy, transform, replace_copy,
replace_copy_if. fill_n, generate_n, remove, recome_copy,
remove_copy_if, unique_copy, reverse_copy, rotate_copy, merge,
set_union, set_intersection, set_difference, set_symmetric_difference,
partial_sum, adjacent_difference).

All these algorithms operate on a "container" entity (e.g. list<int>
or set<string>). I am having a tough time getting a "wrapper"
function to compile if I pass both the container type and the
contained datatype as template arguments. For example, the compiler
likes <list<int> > but not <list, int>.

I am attaching some sample code to get my point across. Is there a
way to avoid the compilation error if I uncomment the line
#define BREAK_COMPILATION

Thanks,
Ramesh

///////////////////////////////////
//
// Sample program to test usage of STL algorithms
//
///////////////////////////////////

#include <iostream>
#include <iterator>
#include <string>
#include <list>
#include <set>

#include <algorithm>

using namespace std;

//#define BREAK_COMPILATION


#ifdef BREAK_COMPILATION
template<typename T1, typename T2>
#else
template<typename T>
#endif // BREAK_COMPILATION
void
executeSTLAlgo(const string& label)
{
int c1[] = { 1, 2, 2, 4, 6, 7, 7, 9 };
int num1 = sizeof(c1) / sizeof(int);

int c2[] = { 2, 2, 2, 3, 6, 6, 8, 9 };
int num2 = sizeof(c2) / sizeof(int);

#ifdef BREAK_COMPILATION
T1<T2 > resultColl;
#else
T resultColl;
#endif // BREAK_COMPILATION


#ifdef BREAK_COMPILATION
insert_iterator<T1<T2> > iter(resultColl,resultColl.begin());
#else
insert_iterator<T> iter(resultColl,resultColl.begin());
#endif // BREAK_COMPILATION

cout << "\n\n" << label << ":\n";

/*
set_union (c1, c1+num1,
c2, c2+num2, ostream_iterator<int>(cout," "));


set_union (c1, c1+num1,
c2, c2+num2, inserter(resultColl,resultColl.begin());
*/

set_union (c1, c1+num1,
c2, c2+num2, iter);

cout << "\nSize: " << resultColl.size() << "\n";
copy(resultColl.begin(), resultColl.end(),
ostream_iterator<int>(cout," "));
cout << endl;
}


main()
{
#ifdef BREAK_COMPILATION
executeSTLAlgo<list,int>("List");
executeSTLAlgo<set, int>("Set");
#else
executeSTLAlgo<list<int> >("List");
executeSTLAlgo<set<int> >("Set");
#endif // BREAK_COMPILATION
}
Generic Usenet Account
2007-10-05 15:58:33 UTC
Permalink
Talking of STL algorithms, I have found some of the best descriptions
of STL algorithms in Josuttis' Summary of STL Algorithms, prepared by
Scott Meyers for his Effective STL Course, http://www.aristeia.com/estl/

1. accumulate() >>-->> Combines all element values (processes sum,
product, and so forth)
2. adjacent_difference() >>-->> Combines each element with its
predecessor; converts absolute values to relative values
3. adjacent_find() >>-->> Searches for two adjacent elements that are
equal (by some criterion)
4. binary_search() >>-->> Returns whether the range contains an
element
5. copy() >>-->> Copies a range starting with the first element
6. copy_backward() >>-->> Copies a range starting with the last
element
7. count() >>-->> Returns the number of elements
8. count_if() >>-->> Returns the number of elements that match a
criterion
9. equal() >>-->> Returns whether two ranges are equal
10. equal_range() >>-->> Returns the range of elements equal to a
given value
11. fill() >>-->> Replaces each element with a given value
12. fill_n() >>-->> Replaces n elements with a given value
13. find() >>-->> Searches for the first element with the passed value
14. find_end() >>-->> Searches for the last occurrence of a subrange
15. find_first_of() >>-->> Searches the first of several possible
elements
16. find_if() >>-->> Searches for the first element that matches a
criterion
17. for_each() >>-->> Performs an operation for each element
18. generate() >>-->> Replaces each element with the result of an
operation
19. generate_n() >>-->> Replaces n elements with the result of an
operation
20. includes() >>-->> Returns whether each element of a range is also
an element of another range
21. inner_product() >>-->> Combines all elements of two ranges
22. inplace_merge() >>-->> Merges two consecutive sorted ranges
23. lexicographical_compare() >>-->> Returns whether a range is
lexicographically less than another range
24. lower_bound() >>-->> Finds the first element greater than or equal
to a given value
25. make_heap() >>-->> Converts a range into a heap
26. max_element() >>-->> Returns the element with the largest value
27. merge() >>-->> Merges the elements of two ranges
28. min_element() >>-->> Returns the element with the smallest value
29. mismatch() >>-->> Returns the first elements of two sequences that
differ
30. next_permutation() >>-->> Permutes the order of the elements
31. nth_element() >>-->> Sorts according to the nth position
32. partial_sort() >>-->> Sorts until the first n elements are correct
33. partial_sort_copy() >>-->> Copies elements in sorted order
34. partial_sum() >>-->> Combines each element with all of its
predecessors; converts relative values to absolute values
35. partition() >>-->> Changes the order of the elements so that
elements that match a criterion are at the front
36. pop_heap() >>-->> Removes an element from a heap
37. prev_permutation() >>-->> Permutes the order of the elements
38. push_heap() >>-->> Adds an element to a heap
39. random_shuffle() >>-->> Brings the elements into a random order
40. remove() >>-->> Removes elements with a given value
41. remove_copy() >>-->> Copies elements that do not match a given
value
42. remove_copy_if() >>-->> Copies elements that do not match a given
criterion
43. remove_if() >>-->> Removes elements that match a given criterion
44. replace() >>-->> Replaces elements that have a special value with
another value
45. replace_copy() >>-->> Replaces elements that have a special value
while copying the whole range
46. replace_copy_if() >>-->> Replaces elements that match a criterion
while copying the whole range
47. replace_if() >>-->> Replaces elements that match a criterion with
another value
48. reverse() >>-->> Reverses the order of the elements
49. reverse_copy() >>-->> Copies the elements while reversing their
order
50. rotate() >>-->> Rotates the order of the elements
51. rotate_copy() >>-->> Copies the elements while rotating their
order
52. search() >>-->> Searches for the first occurrence of a subrange
53. search_n() >>-->> Searches for the first n consecutive elements
with certain properties
54. set_difference() >>-->> Processes a sorted range that contains all
elements of a range that are not part of another
55. set_intersection() >>-->> Processes the sorted intersection of two
ranges
56. set_symmetric_difference() >>-->> Processes a sorted range that
contains all elements that are in exactly one of two ranges
57. set_union() >>-->> Processes the sorted union of two ranges
58. sort() >>-->> Sorts all elements
59. sort_heap() >>-->> Sorts the heap (it is no longer a heap after
the call)
60. stable_partition() >>-->> Same as partition(), but preserves the
relative order of matching and nonmatching elements
61. stable_sort() >>-->> Sorts while preserving order of equal
elements
62. swap_ranges() >>-->> Swaps elements of two ranges
63. transform() >>-->> Modifies (and copies) elements; combines
elements of two ranges
64. unique() >>-->> Removes adjacent duplicates (elements that are
equal to their predecessor)
65. unique_copy() >>-->> Copies elements while removing adjacent
duplicates
66. upper_bound() >>-->> Finds the first element greater than a given
value

Loading...