RSS Forums RSS

std::copy a std::vector of std::pair to std::cout

Please support our C++ advertiser: Programming Forums
Thread Solved
Reply
Posts: 3,877
Reputation: Dave Sinkula is a splendid one to behold Dave Sinkula is a splendid one to behold Dave Sinkula is a splendid one to behold Dave Sinkula is a splendid one to behold Dave Sinkula is a splendid one to behold Dave Sinkula is a splendid one to behold Dave Sinkula is a splendid one to behold 
Solved Threads: 164
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

std::copy a std::vector of std::pair to std::cout

  #1  
Oct 28th, 2008
What am I missing here?
#include <iostream>
using std::cout;
#include <vector>
using std::vector;
#include <string>
using std::string;
#include <algorithm>
using std::copy;
using std::remove;
#include <iterator>
using std::ostream_iterator;
#include <utility>
using std::pair;

vector<string>& descend(const char *path, const char *pattern, vector<string> &filelist);
void search(const char *path, const char *pattern, vector<string> &filelist);

std::ostream& operator<< (std::ostream &o, const pair<string, string> &p)
{
   return o << p.first << ',' << p.second;
}

int main()
{
   vector<string> wavs, phrases;
   search("H:/projects/wavs/IT", "*.wav", wavs);
   search("H:/projects/wavs/IT", "*.phr", phrases);
   vector<pair<string, string> > renamer;
   for ( vector<string>::iterator it = wavs.begin(); it != wavs.end(); ++it )
   {
      string temp(*it);
      std::pair<string, string> entry;
      entry.first  = *it;
      temp.erase(remove(temp.begin(),temp.end(),' '),temp.end());
      temp.erase(remove(temp.begin(),temp.end(),'\''),temp.end());
      temp.erase(remove(temp.begin(),temp.end(),'_'),temp.end());
      temp.erase(remove(temp.begin(),temp.end(),'-'),temp.end());
      entry.second = temp;
      renamer.push_back(entry);
   }
   std::copy(wavs.begin(), wavs.end(), ostream_iterator<string>(cout, "\n"));
   std::copy(phrases.begin(), phrases.end(), ostream_iterator<string>(cout, "\n"));
   std::copy(renamer.begin(), renamer.end(), ostream_iterator<pair<string, string> >(cout, "\n"));
   return 0;
}
It's a dirty, ugly hack, yes. But what am I doing wrong on the copy to cout ?
*** {BD Software Proxy c++ v3.43a for gcc} STL Message Decryption is ON! ***
stream_iterator.h: In member function
`ostream_iterator<pair<string, string>, char, char_traits<char> > &
ostream_iterator<pair<string, string>, char, char_traits<char> >::operator=(
const pair<string, string> &
)':
[STL Decryptor: Suppressed 5 more STL standard header messages]
main.cpp:43: instantiated from here
stream_iterator.h:196: error: no match for 'operator<<' in '*(
(ostream_iterator<pair<string, string>, char, char_traits<char> > *)
this
)->ostream_iterator<pair<string, string>, char, char_traits<char> >
::_M_stream << __value'
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
AddThis Social Bookmark Button
Reply With Quote  
Posts: 3,877
Reputation: Dave Sinkula is a splendid one to behold Dave Sinkula is a splendid one to behold Dave Sinkula is a splendid one to behold Dave Sinkula is a splendid one to behold Dave Sinkula is a splendid one to behold Dave Sinkula is a splendid one to behold Dave Sinkula is a splendid one to behold 
Solved Threads: 164
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: std::copy a std::vector of std::pair to std::cout

  #2  
Oct 28th, 2008
http://std.dkuug.dk/JTC1/SC22/WG21/docs/lwg-active.html
J. C. van Winkel points out (in c++std-lib-9565) another unexpected fact: it's impossible to output a container of std::pair's using copy and an ostream_iterator, as long as both pair-members are built-in or std:: types. That's because a user-defined operator<< for (for example) std::pair<const std::string, int> will not be found: lookup for operator<< will be performed only in namespace std. Opinions differed on whether or not this was a defect, and, if so, whether the defect is that something is wrong with user-defined functionality and std, or whether it's that the standard library does not provide an operator<< for std::pair<>.

http://groups.google.com/group/comp....3e2b1d642f29e/
[...] it's Koenig lookup (aka argument dependent lookup, ADL) in action. Shockingly enough, MSVC actually has ADL implemented for operators (though not functions), and that is what causes the problem.

std::pair<int const, int> is from std
std::ostream is from std
inside the std::ostream_iterator, where operator<< is called, is in std.
-> only std is searched for the operator<<
But the operator<< is in global, and hence is not found.

The above fix is illegal in the strict sense since there are only certain things that you are allowed to add to std (e.g. specializations of std::swap).
A legal solution is to use a proxy class:
Last edited by Dave Sinkula : Oct 28th, 2008 at 5:25 pm.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Other Threads in the C++ Forum
Views: 2317 | Replies: 1 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 10:45 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC