How can we write a function which swap enum variables?
Also are these variables standard and working with them doesn't matter in a project? How do C++ complier use them and put them in the RAM?
Use std::swap
:
enum Foo {
CAT,
DOG,
ELEPHANT
};
Foo a = CAT;
Foo b = ELEPHANT;
std::swap(a, b);
std::cout << (a == ELEPHANT) << "\n";
enum
is perfectly standard; they are equivalent to integer types.
You don't need to write a swap function, std::swap
works out of the box.
enum
is standard C++. enum
values are represented as an integer type in memory, by default as an int
. You can change this representation (in C++11) with, eg
enum Foo : short { A, B, C };
确实,只需使用std::swap(....)
。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.