[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?
[Edit this page] [Page history] [What links here] [Printer Friendly]
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?
- include <map>
- include <vector>
- include <algorithm>
- include <cassert>
- include <iostream>
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); }
- include <iterator>
- include <fstream>
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;
}
[Edit this page] [Page history] [What links here] [Printer Friendly]
