Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) std::copy

 

STL algorithm to copy container elements.

 

std::copy assumes that the memory data is copied to is valid. For example, if you copy a std::vector of size 10 to another std::vector, the latter must have a size of 10 at least. But if the size is unknown, use an inserter like std::back_inserter.

 

Related algorithms are:

 

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

 

 

 

 

 

Example without using an inserter

 

#include <algorithm
#include <cassert>
#include <iostream>
#include <map>
#include <vector>
 
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()==size);
  //Display the std::vector
  for (int i=0; i!=size; ++i)
  {
    std::cout
      << myVector[i].first
      << " "
      << myVector[i].second
      << std::endl;
  }
}

 

 

 

 

 

Example with using an inserter

 

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

 

#include <algorithm
#include <cassert>
#include <iostream>
#include <map>
#include <vector>
 
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;
 
  //Copy the std::map to the std::vector
  std::copy(myMap.begin(),myMap.end(),std::back_inserter(myVector));
 
  //Display the std::vector
  for (int i=0; i!=size; ++i)
  {
    std::cout
      << myVector[i].first
      << " "
      << myVector[i].second
      << std::endl;
  }
}

 

 

 

 

 

Example: Append

 

#include <algorithm

//From http://www.richelbilderbeek.nl
template <class Container>
void Append(Container& toWhat, const Container& whatToAppend)
{
  std::copy(whatToAppend.begin(),whatToAppend.end(),std::back_inserter (toWhat));
}

 

 

 

 

 

Example: CoutVector

 

#include <iostream>
#include <iterator>
#include <ostream>
#include <vector>

//From http://www.richelbilderbeek.nl/CppCoutVector.htm
template <class T>
void CoutVector(const std::vector<T>& v)
{
  std::copy(v.begin(),v.end(),std::ostream_iterator<T>(std::cout,"\n"));
}

 

 

 

 

 

Example: CoutContainer

 

#include <iostream>
#include <iterator>
#include <ostream>
#include <vector>

//From http://www.richelbilderbeek.nl/CppCoutContainer.htm
template <class Container>
void CoutContainer(const Container& c)
{
  std::copy(c.begin(),c.end(),
    std::ostream_iterator<typename Container::value_type>(std::cout,"\n"));
}

 

 

 

 

 

External links

 

 

 

 

 

 

References

 

  1. Bjarne Stroustrup. The C++ Programming Language (3rd edition). ISBN: 0-201-88954-4. Chapter 18.12.1: 'Prefer algorithms to loops.
  2. Scott Meyers. Effective STL. ISBN: 0-201-74962-9. Item 43: 'Prefer algorithm calls over hand-written loops'

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict