Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ builtin to swap two numerical values?

Tags:

c++

stl

swap

Does C++ have a built in such as part of STL to swap two numerical values instead of doing:

int tmp = var1;

var1 = var2;
var2 = tmp;

Something like this:

std::swapValues(var1, var2);

Where swapValues is a template.

like image 818
WilliamKF Avatar asked Nov 28 '22 10:11

WilliamKF


1 Answers

Use std::swap

std::swap(var1, var2);
like image 65
Stephen Avatar answered Dec 05 '22 16:12

Stephen