C++ Codes
Algorithms
Algorithm Analysis in C++
Beginners
Code Snippets
Graphics
Data Structures
File Manipulation
Games
Mathematics
Miscellaneous
Visual C++ Library
C++ > Visual C++ 5.0 Standard C++ Library sample source codes
Algorithm copy - Copies elements from one range to another.
Algorithm copy - Copies elements from one range to another. copy Header <algorithm> template<class InputIterator, class OutputIterator> OutputIterator copy (InputIterator first1, InputIterator last1, OutputIterator first2) ; Copies elements from one range to another. copy copies elements from the range [first1, last1) into a range of the same size starting at first2. It uses the assignment operator to replace existing elements. copy returns an iterator positioned immediately after the new last element. The algorithm proceeds in the forward direction copying source elements first1, first+1, ..., last1 - 1. The destination range can overlap with the source range provided it doesn't contain first2. Sample #pragma warning (disable: 4786) #include <algorithm> #include <vector> #include <deque> #include <iostream> int main() { std::vector<int> vint ; std::deque<int> dint(25) ; typedef std::ostream_iterator <int> IntOstreamIt; std::deque<int>::iterator dit ; int i ; for(i = 0 ; i < 25; i++) vint.push_back(i*i) ; std::cout << "copy contents of vector to deque using copy\n" << std::endl ; //copy contents of vector to deque. dit = std::copy(vint.begin(), vint.end(), dint.begin()) ; std::cout << "last element of dint = " << *(dit - 1) << "\n" << std::endl ; IntOstreamIt OstreamIt(std::cout,", "); std::cout << "vint = " ; std::copy(vint.begin(), vint.end(), OstreamIt); std::cout << "\n" << std::endl ; std::cout << "dint = " ; std::copy(dint.begin(), dint.end(), OstreamIt); std::cout << std::endl ; std::cout << "copy contents of vector to deque using copy_backward\n" << std::endl ; //copy contents of vector to deque, //copy_backward starts copying elements from the end of the vector //and progresses to the start of the vector. //It returns pointer to the last element in the deque dit = std::copy_backward(vint.begin(), vint.end(), dint.begin()) ; std::cout << "first element of dint = " << *dit << "\n" << std::endl ; std::cout << "vint = " ; std::copy(vint.begin(), vint.end(), OstreamIt); std::cout << "\n" << std::endl ; std::cout << "dint = " ; std::copy(dint.begin(), dint.end(), OstreamIt); std::cout << std::endl ; return 0 ; } Program Output copy contents of vector to deque using copy last element of dint = 576 vint = 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 28 9, 324, 361, 400, 441, 484, 529, 576, dint = 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 28 9, 324, 361, 400, 441, 484, 529, 576, copy contents of vector to deque using copy_backward first element of dint = 0 vint = 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 28 9, 324, 361, 400, 441, 484, 529, 576, dint = 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 28 9, 324, 361, 400, 441, 484, 529, 576,
Privacy Policy
|
Link to Us
|
Links