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 - Swaps the elements a and b.
Algorithm swap - Swaps the elements a and b. swap Header <algorithm> template<class T> void swap(const T& a, constT& b) Swaps the elements a and b. 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 std::swap(s1, s2) ; std::swap(s3, s4) ; 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 some more!!!" << std::endl ; //swap std::swap(s1, s3) ; std::swap(s4, s2) ; 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 the butter was bitter s2 = Betty Botter bought some butter s3 = to make the bitter butter better s4 = So she got some better butter Jumble some more!!! s1 = to make the bitter butter better s2 = So she got some better butter s3 = But she said the butter was bitter s4 = Betty Botter bought some butter
Privacy Policy
|
Link to Us
|
Links