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 swap ranges - Swap elements in the range [first1, last1) with elements
Algorithm swap ranges - Swap elements in the range [first1, last1) with elements swap_ranges Header <algorithm> template<class ForwardIterator1, class ForwardIterator2> ForwardIterator2 swap_ranges(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2) Swap elements in the range [first1, last1) with elements in the range of the same size starting at first2. Return an interator positioned immediately after the last element in the second range. Sample #pragma warning(disable : 4786) #include <algorithm> #include <iostream> #include <string> int main() { std::string s1("Betty Botter bought some butter") ; std::string s2("But she said the butter was bitter") ; std::string s3("So she got some better butter") ; std::string s4("to make the bitter butter better") ; std::cout << "s1 = " << s1 << std::endl ; std::cout << "s2 = " << s2 << std::endl ; std::cout << "s3 = " << s3 << std::endl ; std::cout << "s4 = " << s4 << std::endl ; std::cout << "\nJumble them up!!!" << std::endl ; //swap_ranges std::swap_ranges(s1.begin(), s1.begin()+15, s2.begin()) ; std::swap_ranges(s3.begin(), s3.begin()+15, s4.begin()) ; std::cout << "s1 = " << s1 << std::endl ; std::cout << "s2 = " << s2 << std::endl ; std::cout << "s3 = " << s3 << std::endl ; std::cout << "s4 = " << s4 << std::endl ; return 0 ; } Program Output s1 = Betty Botter bought some butter s2 = But she said the butter was bitter s3 = So she got some better butter s4 = to make the bitter butter better Jumble them up!!! s1 = But she said thught some butter s2 = Betty Botter boe butter was bitter s3 = to make the bit better butter s4 = So she got someter butter better
Privacy Policy
|
Link to Us
|
Links