N
Nan Li
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;
}
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;
}