How To Use std::exchange In C++
C++ is a very precise programming language that allows you to use memory operations in a wide variety of ways. Mastering efficient memory handling will improve your app performance at run time and can result in faster applications that have optimal memory usage. One of the many useful features that come with the C++14 standard is the std::exchange
algorithm that is defined in the utility header. In this post, we explain how to use std::exchange
in C++.
What is std::exchange in C++?
The std::exchange
algorithm is defined in the
header and it is a built-in function that comes with C++14. The std::exchange
algorithm copies the new value to a given object and it will return the old value of that object.
Here is the template definition syntax (since C++14, until C++20):
template< class T, class U = T > T exchange( T& object, U&& new_value );
|
How to use std::exchange in C++?
We can use std::exchange
to exchange variables values as below, and we can obtain old value from the return value of std::exchange
.
int x = 500; int y = std::exchange(x, 333);
|
After these lines, x
is now 333 and y
is the old value of x , 500.
We can use std::exchange
to set values of object lists (i.e vectors) as shown below:
std::vector<int> vec; std::exchange( vec, { 10, 20, 30, 40, 50 } );
|
In addition, we can copy old values to another object list as we show in the following example:
std::vector<int> vec, vec0; vec0 = std::exchange( vec, { 10, 20, 30, 40, 50 } );
|
We can use std::exchange
to change function definitions too, for example assume we have myf1()
function, now we want to exchange myf()
function to myf1()
function, this is how we can do:
void (*myf)(); std::exchange(myf, myf1); // myf is now myf1 myf();
|
Is there a full example of how to use std::exchange in C++?
Here is a full example of how to use std::exchange
in C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
#include #include
void myf1() { std::cout << “My f1” << std::endl; }
void myf2() { std::cout << “My f2” << std::endl; }
int main() { // echange values int x = 500; int y = std::exchange(x, 333); std::cout << “x:” << x << “, y:” << y << std::endl;
// exchange object or object lists std::vector<int> vec{1, 2, 3, 4, 5}, vec0; vec0 = std::exchange( vec, {10, 20, 30, 40, 50} );
for (auto v:vec) { std::cout << v << ” “; } std::cout << std::endl;
for (auto v:vec0) { std::cout << v << ” “; } std::cout << std::endl;
// exchange function definitions void (*myf)();
std::exchange(myf, myf1); // myf is now myf1 myf();
std::exchange(myf, myf2); // myf is now myf2 myf();
system(“pause”); return 0; }
|
and the output will be as follows,
x:333, y:500 10 20 30 40 50 1 2 3 4 5 My f1 My f2 Press any key to continue . . .
|
For more information about this std::exchange
feature, please see https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3668.html
Note that, this function is being improved in C++23 standards.
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here