Re: cannot write std::map via ostream_iterator? Organization: albasani.net
"James K. Lowden" <jklowden@schemamania.org> wrote in message
news:20101027221206.741eb283.jklowden@schemamania.org...
Can you tell me why the program below does not compile? GCC says:
$ make mapout.o
c++ -O2 -c mapout.cpp
/usr/include/g++/bits/stream_iterator.h: In member function `
std::ostream_iterator<_Tp, _CharT, _Traits>& std::ostream_iterator<_Tp,
_CharT, _Traits>::operator=(const _Tp&) [with _Tp = std::pair<const
std::string, std::string>, _CharT = char, _Traits =
std::char_traits<char>]
':
[...]
mapout.cpp:21: instantiated from here
/usr/include/g++/bits/stream_iterator.h:141: error: no match for
'operator<<'
in '*this->std::ostream_iterator<std::pair<const std::string,
std::string>,
char, std::char_traits<char> >::_M_stream << __value'
I understand that ostream_iterator::operator=(rhs) calls
operator<<(ostream&, rhs) and as far as I'm concerned that operator is
declared, right after the typedef. But when two compilers disagree with
me, I usually like to admit I'm wrong.
It smells like a const problem, but it must be possible to write
std::pair<const string, string> to an ostream via an iterator. (Or
perhaps it has to do with the context of the template instantiation?)
Am I expected to derive my own iterator? If so, why?
The workaround of course is std::transform with a function to convert the
pair to a string, but that's not efficient in terms of human or machine
cycles.
Many thanks,
--jkl
[snip]
#include <iostream>
#include <fstream>
#include <iterator>
#include <algorithm>
#include <map>
#include <string>
using namespace std;
typedef map<string,string> map_ss;
ostream&
operator<<( ostream& os, const map_ss::value_type& elem );
int
main( int argc, char *argv[] )
{
map_ss ml;
copy( ml.begin(), ml.end(), ostream_iterator<map_ss::value_type>(cout)
);
return 0;
}
[pins]
Remember, a standard map iterator has 2 parts, a ->first and a ->second. The
->first points to the key, the ->set points to the data. In your case, ->first
would be a std::string as would ->second. I don't see this reflected in your code.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]