473,715 Members | 3,751 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to get ostream_iterato r 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_iterato r it = m.begin();
it != m.end();
++it ) {
cout << *it << endl;
}
//copy( m.begin(), m.end(), ostream_iterato r<IntStringPair >( cout,
"\n" ) );
//why this does not work ??
return 0;
}

Nov 9 '05 #1
4 1993
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_iterato r it = m.begin();
it != m.end();
++it ) {
cout << *it << endl;
}
//copy( m.begin(), m.end(), ostream_iterato r<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_iterato r it = m.begin();
it != m.end();
++it ) {
cout << *it << endl;
}
//copy( m.begin(), m.end(), ostream_iterato r<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_i terator<_Tp, _CharT, _Traits>&
std::ostream_it erator<_Tp,
_CharT, _Traits>::opera tor=(const _Tp&) [with _Tp = IntStringPair,
_CharT =
char, _Traits = std::char_trait s<char>]':
/usr/include/c++/3.2.3/bits/stl_algobase.h: 228: instantiated from
`_OutputIter std::__copy(_In putIter, _InputIter, _OutputIter,
std::input_iter ator_tag) [with _InputIter =
std::_Rb_tree_i terator<std::pa ir<const int, std::string>,
std::pair<const int, std::string>&, std::pair<const int,
std::string>*>, _OutputIter = std::ostream_it erator<IntStrin gPair,
char, std::char_trait s<char> >]'
/usr/include/c++/3.2.3/bits/stl_algobase.h: 260: instantiated from
`_OutputIter std::__copy_aux 2(_InputIter, _InputIter, _OutputIter,
__false_type) [with _InputIter = std::_Rb_tree_i terator<std::pa ir<const
int, std::string>, std::pair<const int, std::string>&, std::pair<const
int, std::string>*>, _OutputIter = std::ostream_it erator<IntStrin gPair,
char, std::char_trait s<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_i terator<std::pa ir<const
int, std::string>, std::pair<const int, std::string>&, std::pair<const
int, std::string>*>, _OutputIter = std::ostream_it erator<IntStrin gPair,
char, std::char_trait s<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_i terator<std::pa ir<const
int, std::string>, std::pair<const int, std::string>&, std::pair<const
int, std::string>*>, _OutputIter = std::ostream_it erator<IntStrin gPair,
char, std::char_trait s<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_iterato r::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_it erator 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_iterato r::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_it erator 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
2038
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
2494
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. Fraser.
8
4615
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 non-zero number follows. ---------------- error message --------------------
5
2550
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 first element of int_list. after the first element is copied to cout the stream setw(3) no longer has any affect.
3
4745
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 error telling me it doesn't like the char. wcout.widen('\n') doesn't work here because it wants a string. Is there a way to make this work? -- NOUN:1. Money or property bequeathed to another by will. 2. Something handed
2
1888
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 GCC(GNU Compiler Collection) on rat hat linux. My code which I copied from some book is as follow: #include <set> #include <iostream> using namespace std; int main() {
2
7280
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
2220
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 ostream_iterator's constructor and save the value in a member variable counter. Then, in operator=(),
0
1380
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 compiler error, and I'm stymied as to why. It's seems to be related to the string type, because iterating over pairs of other types has worked fine for me. The (rather verbose) error is: /usr/include/c++/4.2/bits/stream_iterator.h:196: error: no...
0
8821
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8718
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9340
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9196
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9103
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9047
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6646
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4477
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3175
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.