Skip to content

Instantly share code, notes, and snippets.

@dgodfrey206
Created January 25, 2015 04:32
Show Gist options
  • Save dgodfrey206/2c161977de379c5d37a1 to your computer and use it in GitHub Desktop.
Save dgodfrey206/2c161977de379c5d37a1 to your computer and use it in GitHub Desktop.
Student with whitespace fmt
#include <iostream>
#include <vector>
#include <iterator>
class student_fmt;
struct student
{
student() = default;
student(std::string const& name, std::string const& a,
std::string const& b, std::string const& c)
: m_name(name)
, m_a(a)
, m_b(b)
, m_c(c)
{ }
friend std::istream& operator>>(std::istream& is, student& s)
{
if (std::has_facet<student_fmt>(is.getloc()))
{
is >> s.m_name >> s.m_a >> s.m_b >> s.m_c;
}
return is;
}
void display() const
{
std::cout << m_name << m_a << " " << m_b << " " << m_c << '\n';
}
private:
std::string m_name, m_a, m_b, m_c;
};
class student_fmt : public std::ctype<char>
{
static mask table[table_size];
public:
student_fmt(size_t refs = 0) : std::ctype<char>(table, false, refs)
{
table[static_cast<int>(',')] |= space;
}
};
student_fmt::mask student_fmt::table[table_size];
int main()
{
std::cin.imbue(std::locale(std::cin.getloc(), new student_fmt));
std::vector<student> v(std::istream_iterator<student>(std::cin), {});
for (auto& c : v)
c.display();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment