S
ShaunJ
I'm trying to use an ostream_iterator to iterate over a set of pairs.
I've used an ostream_iterator over other types and had no problem.
However, with the following code snippet, I'm getting a compiler
error, and I'm stymied as to why. It's seems to be related to the
string type, because iterating over pairs of other types has worked
fine for me. The (rather verbose) error is:
/usr/include/c++/4.2/bits/stream_iterator.h:196: error: no match for
'operator<<' in '*((std:stream_iterator<std:air<unsigned int,
std::char_traits<char> >::_M_stream << __value'
Thanks,
Shaun
#include <iostream>
#include <iterator>
#include <set>
#include <string>
using namespace std;
ostream& operator <<(ostream& o, const pair<unsigned, string>& age)
{
return o << age.second << ": " << age.first;
}
int main()
{
string a("Alice");
string b("Bob");
string c("Carol");
pair<unsigned, string> pa(19, a);
pair<unsigned, string> pb(17, c);
pair<unsigned, string> pc(23, b);
set<pair<unsigned, string> > ages;
ages.insert(pa);
ages.insert(pb);
ages.insert(pc);
ostream_iterator<pair<unsigned, string> > oi(cout, ", ");
copy(ages.begin(), ages.end(), oi);
cout << '\n';
return 0;
}
I've used an ostream_iterator over other types and had no problem.
However, with the following code snippet, I'm getting a compiler
error, and I'm stymied as to why. It's seems to be related to the
string type, because iterating over pairs of other types has worked
fine for me. The (rather verbose) error is:
/usr/include/c++/4.2/bits/stream_iterator.h:196: error: no match for
'operator<<' in '*((std:stream_iterator<std:air<unsigned int,
std::char_traits<char>, std::allocator<char> > >, char,, char, std::char_traits<char> >*)this)-
std:stream_iterator<std:air<unsigned int, std::basic_string<char,
std::char_traits<char> >::_M_stream << __value'
Thanks,
Shaun
#include <iostream>
#include <iterator>
#include <set>
#include <string>
using namespace std;
ostream& operator <<(ostream& o, const pair<unsigned, string>& age)
{
return o << age.second << ": " << age.first;
}
int main()
{
string a("Alice");
string b("Bob");
string c("Carol");
pair<unsigned, string> pa(19, a);
pair<unsigned, string> pb(17, c);
pair<unsigned, string> pc(23, b);
set<pair<unsigned, string> > ages;
ages.insert(pa);
ages.insert(pb);
ages.insert(pc);
ostream_iterator<pair<unsigned, string> > oi(cout, ", ");
copy(ages.begin(), ages.end(), oi);
cout << '\n';
return 0;
}