473,320 Members | 2,048 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,320 software developers and data experts.

How to use std::copy with std::map?

Should the following work? It does on some compilers. How can I get it to
work on g++ 3.2?
On g++ 3.2 it keeps telling me that there is no such function operator <<
for ostream and pair<string,string>. But there is!
And I can call it! And it works! So why cannot std::copy find it?

thanks,
Siegfried

template<typename K, typename V>
ostream& operator<<(ostream& os, std::pair<K,V>& p){ return os << p.first <<
p.second; }

std::map<string,string> m; m[(string)"hello"] = (string)"there";
std::copy(m.begin(), m.end(), ostream_iterator<std::pair<string,string>
(cout, ",");

May 3 '06 #1
1 2857
Siegfried Heintze wrote:
Should the following work? It does on some compilers. How can I get
it to work on g++ 3.2?
Compiler-specific questions should be asked in the newsgroup dedicated
to that compiler. Try gnu.g++.help.
On g++ 3.2 it keeps telling me that there is no such function
operator << for ostream and pair<string,string>. But there is!
Where?
And I can call it! And it works! So why cannot std::copy find it?

thanks,
Siegfried

template<typename K, typename V>
ostream& operator<<(ostream& os, std::pair<K,V>& p){ return os <<
p.first << p.second; }
Is that what you wrote? Where does this definition appear? In
the global scope?
std::map<string,string> m; m[(string)"hello"] = (string)"there";
std::copy(m.begin(), m.end(),
ostream_iterator<std::pair<string,string>
(cout, ",");


Argument-dependent lookup (ADL) does not look in all the namespaces.
It limits its search to the types involved and the namespaces where
those types are defined. You need to place your operator in 'std'
namespace.

Next time please follow the recommendations of FAQ 5.8.

V
--
Please remove capital As from my address when replying by mail
May 4 '06 #2

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

Similar topics

7
by: sks_cpp | last post by:
map<int, Drive*> dMap; list<Drive*> dList; copy( dMap.begin(), dMap.end(), back_inserter(dList) ); // incorrect The above will not because value_type of dMap is different than dList. Now, I...
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...
2
by: Serengeti | last post by:
Hello, in my class I have a map that translates strings to pointers to some member functions. The code goes like this: class F { typedef void (Function::*MathFuncPtr)(); std::map<std::string,...
1
by: Saeed Amrollahi | last post by:
Dear All C++ Programmers Hello I am Saeed Amrollahi. I am a software engineer in Tehran Sewerage Company. I try to use std::map and map::find member function. I use Visual Studio .NET. my...
19
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not...
1
by: Avery Fong | last post by:
The following program will result in a compile error when building under Debug but will compile under Release. Why does is work under Release mode but not under Debug This program is developed...
13
by: kamaraj80 | last post by:
Hi I am using the std:: map as following. typedef struct _SeatRowCols { long nSeatRow; unsigned char ucSeatLetter; }SeatRowCols; typedef struct _NetData
6
by: Chris Johnson | last post by:
Greetings all. I am really stuck on this one as I can't seem to grok if I am abusing the C++ language or if I am simply using components of the C++ Standard Library incorrectly. Here is the code:...
8
by: mveygman | last post by:
Hi, I am writing code that is using std::map and having a bit of an issue with its performance. It appears that the std::map is significantly slower searching for an element then a sequential...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.