Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Test Swap Functions
#include <iostream> using namespace std; void swap0(int x, int y); void swap1(int& x, int& y); void swap2(int* x, int* y); void swap3(int* x, int* y); void show0(int&a, int&b); void show1(int&a, int&b); void show2(int&a, int&b); void show3(int&a, int&b); int main() { void (*pFunc[])(int &a,int& b) = {show0,show1,show2,show3}; std::cout << "Below sizeof(*pFunc)="<< sizeof(*pFunc)<<" functions will be called to test!" <<std::endl; for (int i(0); i < sizeof(*pFunc); i++) { int i1 = 1111, j1 = 2222; std::cout << "calling function show"<<i+1<<"("<<i1<<","<<j1<<"):--------------------------------" << std::endl; pFunc[i](i1,j1); } return 0; } void swap0(int x, int y){ int temp; temp = x; x = y; y = temp; return; } void swap1(int& x, int& y){ int temp; temp = x; x = y; y = temp; return; } void swap2(int* x, int* y){ int* temp; temp = x; x = y; y = temp; return; } void swap3(int* x, int* y) { int temp = *x; *x = *y; *y = temp; return; } void show0(int &a, int &b) { cout << "Before Swap:a(" << std::hex << &a << ")="<<std::dec<< a << ",b(" << std::hex << &b << ")=" << std::dec << b << endl; swap0(a, b); cout << "After Swap:a(" << std::hex << &a << ")=" <<std::dec<<a << ",b(" << std::hex << &b << ")=" << std::dec << b << endl; } void show1(int& a, int& b) { cout << "Before Swap:a=" << a << ",b=" << b << endl; swap1(a, b); cout << "After Swap:a=" << a << ",b=" << b << endl; } void show2(int& a, int& b) { cout << "Before Swap:a=" << a << ",b=" << b << endl; swap2(&a, &b); cout << "After Swap:a=" << a << ",b=" << b << endl; } void show3(int& a, int& b) { cout << "Before Swap:a(" << std::hex << &a << ")=" << std::dec << a << ",b(" << std::hex << &b << ")=" << std::dec << b << endl; swap3(&a, &b); cout << "After Swap:a(" << std::hex << &a << ")=" << std::dec << a << ",b(" << std::hex << &b << ")=" << std::dec << b << endl; }
run
|
edit
|
history
|
help
0
StrTok
copy_if c++98
combine c++ string with dynamically allocated c array of chars through overloaded add operator
WeekD
blad-z-obcinaniem-koncowek
DailyExchRate2
constructing object on first use as return value of (pointer to) object-returning function
12hours Clock c++
C++ Solar eclipse program(Shoushi integrated) 1644 - 1785 from Ideone( Date: August 14, 2014 )
dsu on tree(264E BLOOD COUSIN RETURNS)