Kristo wrote:
Nan Li wrote: Hello,
Can any one get the 'copy' statement below to work? I want it to
do the same thing as the 'for' loop does. But I got a lot of STL error
during compile.
Thanks,
Nan
#include <map>
#include <string>
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;
typedef pair<int,string> IntStringPair;
template < typename F, typename S >
ostream& operator<< ( ostream& os, const pair<F,S>& p )
{
os << p.first << " -> " << p.second ;
}
int main()
{
map<int,string> m;
m[1] = "foo";
m[2] = "bar";
for ( map<int,string>::const_iterator it = m.begin();
it != m.end();
++it ) {
cout << *it << endl;
}
//copy( m.begin(), m.end(), ostream_iterator<IntStringPair>( cout,
"\n" ) );
//why this does not work ??
return 0;
}
I can't be certain without seeing the list of compiler errors that you
got, but I suspect the copy() line won't compile because of the type of
IntStringPair. std::map uses a std::pair<const Key, Data> type to
store its elements. In your case, you need IntStringPair to be of type
std::pair<const int, std::string>.
Kristo
I tried 'const int', but the compiler is still not happy. Here are
just the first few lines that g++ gave me and I don't get much from
those errors.
/usr/include/c++/3.2.3/bits/stream_iterator.h: In member function
`std::ostream_iterator<_Tp, _CharT, _Traits>&
std::ostream_iterator<_Tp,
_CharT, _Traits>::operator=(const _Tp&) [with _Tp = IntStringPair,
_CharT =
char, _Traits = std::char_traits<char>]':
/usr/include/c++/3.2.3/bits/stl_algobase.h:228: instantiated from
`_OutputIter std::__copy(_InputIter, _InputIter, _OutputIter,
std::input_iterator_tag) [with _InputIter =
std::_Rb_tree_iterator<std::pair<const int, std::string>,
std::pair<const int, std::string>&, std::pair<const int,
std::string>*>, _OutputIter = std::ostream_iterator<IntStringPair,
char, std::char_traits<char> >]'
/usr/include/c++/3.2.3/bits/stl_algobase.h:260: instantiated from
`_OutputIter std::__copy_aux2(_InputIter, _InputIter, _OutputIter,
__false_type) [with _InputIter = std::_Rb_tree_iterator<std::pair<const
int, std::string>, std::pair<const int, std::string>&, std::pair<const
int, std::string>*>, _OutputIter = std::ostream_iterator<IntStringPair,
char, std::char_traits<char> >]'
/usr/include/c++/3.2.3/bits/stl_algobase.h:303: instantiated from
`_OutputIter std::__copy_ni2(_InputIter, _InputIter, _OutputIter,
__false_type) [with _InputIter = std::_Rb_tree_iterator<std::pair<const
int, std::string>, std::pair<const int, std::string>&, std::pair<const
int, std::string>*>, _OutputIter = std::ostream_iterator<IntStringPair,
char, std::char_traits<char> >]'
/usr/include/c++/3.2.3/bits/stl_algobase.h:323: instantiated from
`_OutputIter std::__copy_ni1(_InputIter, _InputIter, _OutputIter,
__false_type) [with _InputIter = std::_Rb_tree_iterator<std::pair<const
int, std::string>, std::pair<const int, std::string>&, std::pair<const
int, std::string>*>, _OutputIter = std::ostream_iterator<IntStringPair,
char, std::char_traits<char> >]'
........
.......