You can't overload the input operator to work, e.g., with int
. I'm not entirely sure what you want to achieve exactly but one way you could possibly deal with the unwanted separators is to turn them into magically into spaces! Let's assume you try to read the data with a loop like
for (int a, b; std::cin >> a >> b; ) {
std::cout << "a=" << a << " b=" << b << "\n";
}
All what is really needed is that the separate is treated like space and skipped. To do so, you can use a custom std::ctype<char>
facet:
#include <algorithm>
#include <iostream>
#include <locale>
struct ctype
: std::ctype<char>
{
typedef std::ctype<char> base;
static base::mask const* make_table(unsigned char space,
base::mask* table)
{
base::mask const* classic(base::classic_table());
std::copy(classic, classic + base::table_size, table);
table[space] |= base::space;
return table;
}
ctype(unsigned char space)
: base(make_table(space, table))
{
}
base::mask table[base::table_size];
};
int main()
{
std::locale global;
std::locale loc(global, new ctype(';'));
std::cin.imbue(loc);
for (int a, b; std::cin >> a >> b; ) {
std::cout << "a=" << a << " b=" << b << "\n";
}
}
NOTE: I tried to compile this code with gcc on a Mac and it fails! The cause is actually not in the program but the problem is that std::ctype<char>::classic()
returns a null pointer. I have no idea what that is about. Compiling with clang and libc++ works, however.