473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

how to get ostream_iterator to work

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;
}

Nov 9 '05 #1
4 1975
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

Nov 9 '05 #2

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> >]'
........
.......

Nov 9 '05 #3
the real problem (besides the forgotten return value in operator<<) is
that your operator overload is not visible for
ostream_iterator::operator=(...).

this function only honors operators in its local namespace (std::) and
the argument-dependend lookup (std:: again because of the std::pair
argument).

you would have to put the operator into the std:: namespace, which you
should not do.

better build a small wrapper class for std::pair or
std::ostream_iterator in the global namespace and use that instead,
then the operator should be found...

-- peter

Nov 9 '05 #4
Peter Steiner wrote:
the real problem (besides the forgotten return value in operator<<) is
that your operator overload is not visible for
ostream_iterator::operator=(...).

this function only honors operators in its local namespace (std::) and
the argument-dependend lookup (std:: again because of the std::pair
argument).

you would have to put the operator into the std:: namespace, which you
should not do.

better build a small wrapper class for std::pair or
std::ostream_iterator in the global namespace and use that instead,
then the operator should be found...

-- peter


This is usally considerd a defect in the C++ language. Although Peter is
right that you should not put operator<< in the std namespace I wouldn't
lose too much sleep over it.

john
Nov 9 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Chris Mantoulidis | last post by:
Hello all... I get an error message for the following program by the compiler: #include <iostream> #include <string> using namespace std; ostream_iterator<string> oo(cout);
12
by: Fraser Ross | last post by:
a std::ostream_iterator can be constructed from a ostream but not a fstream. If the fstream is opened with the out mode how can I get a ostream_iterator? I am not using a pointer to any base class....
8
by: Jeff | last post by:
std::copy to std::ostream_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...
5
by: Wiseguy | last post by:
consider the following: ostream_iterator<int> i(cout,""); cout << setw(3); copy(int_list.begin(),int_list.end(),i); the problem is that the field width restriction is only active for the...
3
by: Steven T. Hatton | last post by:
There's probably something obvious I'm missing here, but I can't seem to figure out how to get this to work: ostream_iterator<wstring, wchar_t>(wcout,"\n")); When I try to compile it, I get an...
2
by: seforo | last post by:
Hi Guys, I have a problem with ostream_iterator. When I compile my code I get the error message that ostream_iterator is not declared. Is n't it predefined iterator for iostream. I am using...
2
by: subramanian100in | last post by:
Consider the following piece of code: #include <iostream> #include <fstream> #include <vector> #include <string> #include <utility> #include <iterator> #include <algorithm> int main()
1
by: johnbrown105 | last post by:
I think that ideally, ostream_iterator's operator=() should write the delimiter *between* elements and not after them. One way of doing this: Add a default int argument = 0 to...
0
by: ShaunJ | last post by:
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.