[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
talk:CppCopy
This page is to discuss "CppCopy". You can ask questions or make comments.
What is a talkpage?

1th March 2007, Bilderbikkel

I hoped to show the copying of the std::map to std::cout, but failed. Anybody got an idea?

  1. include <map>
  2. include <vector>
  3. include <algorithm>
  4. include <cassert>
  5. include <iostream>
  1. include <iterator>
  2. include <fstream>
int main() { const int size = 10; //Generate a std::map std::map<int,int> myMap; for (int i=0; i!=size; ++i) myMap[i] = i*i; //Generate a std::vector std::vector<std::pair<int,int> > myVector; myVector.resize(size); //Copy the std::map to the std::vector std::copy(myMap.begin(),myMap.end(),myVector.begin()); assert(myVector.size()==10); //Display the std::vector for (int i=0; i!=10; ++i) { std::cout << myVector[i].first << " " << myVector[i].second << std::endl; } //Copy to file //Code below does not work! std::ofstream myFile("MyFile.txt"); std::ostream_iterator< std::pair<int, int> > fileIterator(myFile); std::copy(myMap.begin(), myMap.end(), fileIterator); }


01-02-2007 Bilderbikkel

Quick note: I have to add

std::ostream& operator<< (std::ostream& os, const std::pair<int,int>& myPair) 
{
  os << myPair.first << '\t' << myPair.second << '\t';
  return os;
}


last edited (March 1, 2007) by bilderbikkel, Number of views: 763, Current Rev: 5 (Diff)

[Edit this page]  [Page history]  [What links here]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.