C++ //练习 16.14 编写Screen类模板,用非类型参数定义Screen的高和宽。
C++ Primer(第5版) 练习 16.14
练习 16.14 编写Screen类模板,用非类型参数定义Screen的高和宽。
环境:Linux Ubuntu(云服务器)
工具:vim
代码块
template <int H, int W> class Screen{public:using pos = string::size_type;Screen() = default;Screen(char c): contents(H * W, c) {}char get() const;char get(pos ht, pos wd) const;Screen &move(pos r, pos c);private:pos cursor = 0;string contents;
};template <int H, int W>
inline char Screen<H, W>::get() const{return contents[cursor];
}template <int H, int W>
inline char Screen<H, W>::get(pos ht, pos wd) const{pos row = ht * W;return contents[row + wd];
}template <int H, int W>
inline Screen<H, W>& Screen<H, W>::move(pos r, pos c){pos row = r * W;cursor = row + c;return *this;
}