J
Jeff
std::copy to std:stream_iterator doesn't seem to
work with maps and pairs.
Shouldn't I expect something like this to work?
The G++ error message when USE_COPY_TO_PRINT is
defined as a non-zero number follows.
---------------- error message --------------------
<snip lower 'instantiated from' template chain>
constCorrectness.cpp:99: instantiated from here
/usr/include/c++/3.3.1/bits/stream_iterator.h:141: e
rror: no match for
'operator<<' in '*this->std:stream_iterator<Pair
II, char,
std::char_traits<char> >::_M_stream << __value'
<snip candidate templates list>
-------------------- environment -------------------
Windows 2K, CygWin, g++
--------------------- makefile ---------------------
#!/usr/bin make
printmaps.exe : \
printmaps.o
printmaps.o : \
printmaps.cpp
%.exe : %.o
g++ -g -pedantic -Os -fexceptions -o $@ $^
%.o : %.cpp
g++ -g -pedantic -Os -fexceptions -o $@ -c $<
# EOF
------------------- compiler -----------------------
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying co
nditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FO
R A PARTICULAR PURPOSE.
------------------- source code --------------------
#include <map>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <sstream>
// -------------------------------------------------
// PROBLEM NOTE:
// Everything compiles with USE_COPY_TO_PRINT
// set to 0. Set to 1 to show the problem.
// -------------------------------------------------
#define USE_COPY_TO_PRINT 1
typedef std:air<int ,int> PairII;
std::string
to_s(PairII const & pii) {
std::stringstream ss;
ss << "(" << pii.first << ","
<< pii.second << ")";
return ss.str();
}
std:stream &
operator << (
std:stream & os,
PairII const & pii) {
os << to_s(pii);
return os;
}
#if !USE_COPY_TO_PRINT
// -------------------------------------------------
// PROBLEM NOTE:
// This is a helper for the work-around.
// -------------------------------------------------
void
print(PairII const & pii) {
std::cout << to_s(pii) << "\n";
}
#endif
int main() {
size_t const N(5);
PairII a[N] = {
PairII(1,2),
PairII(3,4),
PairII(5,6),
PairII(7,8),
PairII(9,10)
};
std::map<int,int> m(
a,
a + N
);
std::cout << a[0] << "\n";
std::cout << *m.begin() << "\n";
#if USE_COPY_TO_PRINT
// -------------------------------------------------
// PROBLEM NOTE:
// This is the part that doesn't work.
// -------------------------------------------------
std::copy(
a,
a + N,
std:stream_iterator<PairII>(
std::cout,
"\n"
)
);
std::copy(
m.begin(),
m.end(),
std:stream_iterator<PairII>(
std::cout,
"\n"
)
);
#else
// -------------------------------------------------
// PROBLEM NOTE:
// This is the work around.
// -------------------------------------------------
std::for_each(
a,
a + N,
print
);
std::for_each(
m.begin(),
m.end(),
print
);
#endif
}
// EOF
work with maps and pairs.
Shouldn't I expect something like this to work?
The G++ error message when USE_COPY_TO_PRINT is
defined as a non-zero number follows.
---------------- error message --------------------
<snip lower 'instantiated from' template chain>
constCorrectness.cpp:99: instantiated from here
/usr/include/c++/3.3.1/bits/stream_iterator.h:141: e
rror: no match for
'operator<<' in '*this->std:stream_iterator<Pair
II, char,
std::char_traits<char> >::_M_stream << __value'
<snip candidate templates list>
-------------------- environment -------------------
Windows 2K, CygWin, g++
--------------------- makefile ---------------------
#!/usr/bin make
printmaps.exe : \
printmaps.o
printmaps.o : \
printmaps.cpp
%.exe : %.o
g++ -g -pedantic -Os -fexceptions -o $@ $^
%.o : %.cpp
g++ -g -pedantic -Os -fexceptions -o $@ -c $<
# EOF
------------------- compiler -----------------------
g++ (GCC) 3.3.1 (cygming special)cmd /c g++ --version
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying co
nditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FO
R A PARTICULAR PURPOSE.
------------------- source code --------------------
#include <map>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <sstream>
// -------------------------------------------------
// PROBLEM NOTE:
// Everything compiles with USE_COPY_TO_PRINT
// set to 0. Set to 1 to show the problem.
// -------------------------------------------------
#define USE_COPY_TO_PRINT 1
typedef std:air<int ,int> PairII;
std::string
to_s(PairII const & pii) {
std::stringstream ss;
ss << "(" << pii.first << ","
<< pii.second << ")";
return ss.str();
}
std:stream &
operator << (
std:stream & os,
PairII const & pii) {
os << to_s(pii);
return os;
}
#if !USE_COPY_TO_PRINT
// -------------------------------------------------
// PROBLEM NOTE:
// This is a helper for the work-around.
// -------------------------------------------------
void
print(PairII const & pii) {
std::cout << to_s(pii) << "\n";
}
#endif
int main() {
size_t const N(5);
PairII a[N] = {
PairII(1,2),
PairII(3,4),
PairII(5,6),
PairII(7,8),
PairII(9,10)
};
std::map<int,int> m(
a,
a + N
);
std::cout << a[0] << "\n";
std::cout << *m.begin() << "\n";
#if USE_COPY_TO_PRINT
// -------------------------------------------------
// PROBLEM NOTE:
// This is the part that doesn't work.
// -------------------------------------------------
std::copy(
a,
a + N,
std:stream_iterator<PairII>(
std::cout,
"\n"
)
);
std::copy(
m.begin(),
m.end(),
std:stream_iterator<PairII>(
std::cout,
"\n"
)
);
#else
// -------------------------------------------------
// PROBLEM NOTE:
// This is the work around.
// -------------------------------------------------
std::for_each(
a,
a + N,
);
std::for_each(
m.begin(),
m.end(),
);
#endif
}
// EOF