Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Test Swap Functions
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
#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; }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 0.62 sec, absolute running time: 0.18 sec, cpu time: 0.01 sec, memory peak: 5 Mb, absolute service time: 0,91 sec
edit mode
|
history
|
discussion