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

Home Posts Topics Members FAQ

Using an ostream_iterato r over pairs

I'm trying to use an ostream_iterato r to iterate over a set of pairs.
I've used an ostream_iterato r 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 match for
'operator<<' in '*((std::ostrea m_iterator<std: :pair<unsigned int,
std::basic_stri ng<char, std::char_trait s<char>, std::allocator< char
>, char, std::char_trait s<char*)this)-
std::ostream_i terator<std::pa ir<unsigned int, std::basic_stri ng<char,
std::char_trait s<char>, std::allocator< char >, char,
std::char_trait s<char::_M_stre am << __value'

Thanks,
Shaun

#include <iostream>
#include <iterator>
#include <set>
#include <string>

using namespace std;

ostream& operator <<(ostream& o, const pair<unsigned, string>& age)
{
return o << age.second << ": " << age.first;
}

int main()
{
string a("Alice");
string b("Bob");
string c("Carol");
pair<unsigned, stringpa(19, a);
pair<unsigned, stringpb(17, c);
pair<unsigned, stringpc(23, b);
set<pair<unsign ed, string ages;
ages.insert(pa) ;
ages.insert(pb) ;
ages.insert(pc) ;
ostream_iterato r<pair<unsigned , string oi(cout, ", ");
copy(ages.begin (), ages.end(), oi);
cout << '\n';
return 0;
}
Jan 21 '08 #1
0 1380

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

Similar topics

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 --------------------
2
1760
by: Eric Lilja | last post by:
Hello, I have a std::vector storing pointers to objects that are dynamically allocated. When I am done with the vector I want to delete all pointers. Can I use std::for_each() or something to do that in one line? I am so tired of seeing the following in my code: for(vector<some_type>::size_type i = 0; i < myvec.size(); ++i) do_something_with_each_element; / Eric
2
1967
by: alzforu | last post by:
The code in which i m facing the problem is: //file stream.cc #include <vector> #include <algorithm> #include <iostream> using namespace std;
9
2449
by: Paulo Matos | last post by:
Hi all, Given a list of int, it's rather easy to output them line by line: std::copy(mylist.begin(), mylist.end(), std::ostream_iterator<int>(cout, "\n"); Now, there's two ways in which I would like to generalize this: First, I would like to be able to print a list of pointers to int. However, I'm guessing this is not possible with copy, right? I would have to:
5
3656
by: krzysztof.konopko | last post by:
I cannot compile the code which defines a std::map type consisting of built in types and operator<< overload for std::map::value_type. See the code below - I attach a full example. Note: if I define map type with my new type (structure) everything is OK. All compileres I've cheked report an error so I think it is a problem with my code. #include <algorithm> #include <cstddef>
6
5713
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a * vector<string>. Now, use istringstream to read read each line * from the vector a word at a time.
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
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,...
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...
0
7973
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5967
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
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...
0
4738
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2539
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2118
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.