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 backward - Copy a range of elements backwards to another range.
Algorithm copy backward - Copy a range of elements backwards to another range. copy_backward Header <algorithm> template<class BidirectionalIterator1, class BidirectionalIterator2> BidirectionalIterator2 copy_backward(BidirectionalIterator1 first1, BidirectionalIterator1 last1, BidirectionalIterator2 last2) Copy a range of elements backwards to another range. copy_backward copies the elements from the range [first1, last)into a range of the same size ending immediately before last2 using the assignment operator to replace existing elements. It returns an iterator of type BidirectionalIterator2, positioned at the start of the newly created sequence. This algorithm proceeds backwards, copying elements in the order last1 - 1, last1 - 2, ... first1. The copying works fine as long as the source range doesn't contain last2. Samples #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