Skip to content

Instantly share code, notes, and snippets.

@bathoryr
Last active February 2, 2023 21:45
Show Gist options
  • Save bathoryr/158a306319ef9687e3ee86c3eab99918 to your computer and use it in GitHub Desktop.
Save bathoryr/158a306319ef9687e3ee86c3eab99918 to your computer and use it in GitHub Desktop.
Windows console iostream positioning manipulator std::pos(x, y)
#include <Windows.h>
#include <iostream>
namespace std
{
struct _pos
{
_pos(short x, short y) : _x(x), _y(y)
{}
template<typename Elem, typename Traits>
friend std::basic_ostream<Elem, Traits>& operator<<(std::basic_ostream<Elem, Traits>& ostr, const _pos& manip)
{
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), COORD{manip._x, manip._y});
return ostr;
}
private:
short _x, _y;
};
_pos pos(short x, short y)
{
return _pos(x, y);
}
}
@bathoryr
Copy link
Author

bathoryr commented Feb 2, 2023

Set cursor position on Windows console when using std::cout operator << and manipulator std::pos(x, y)

std::cout << std::pos(2, 2) << cnt.c1;
std::cout << std::pos(2, 3) << cnt.c2;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment