STL algorithms and output iterators

  • Thread starter Generic Usenet Account
  • Start date
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
}
 
V

Victor Bazarov

Generic said:
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

The rest of the code presumes we uncommented this. I redacted
unnecessary code
template<typename T1, typename T2>
void
executeSTLAlgo(const string& label)
{ [..]
}


main()

int main()
{
executeSTLAlgo<list,int>("List");
executeSTLAlgo<set, int>("Set");
}

That's not going to work because 'list' is NOT a typename. It's
a template. In order to use 'list' as is (or 'std::list') you need
to declared your 'executeSTLAlgo' to have a _template_ template
argument (no, I didn't make a mistake, two "templates"). Find
a good book ("C++ Templates" by Vandevoorde and Josuttis is good)
and read about template template arguments.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top