[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
CppCopy

(C++) std::copy

STL algorithm to copy containers.

Prefer algorithm calls over hand-written loops [1,2].

Example

Copies a std::map to a std::vector.

  1. include <map>
  2. include <vector>
  3. include <algorithm>
  4. include <cassert>
  5. 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; } }


References



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

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [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.