G
Generic Usenet Account
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
}
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
}