screen类的使用,
程序代码:
#include<iostream> #include<string> using namespace std; class Screen{ public: typedef std::string::size_type index; char get()const {return contents[cursor];} inline char get(index ht,index wd)const; index get_cursor()const; Screen(index hght,index wdth,const string &cntnts); Screen& move(index r,index c); Screen& set(char ); Screen& display(ostream &os); private: std::string contents; index cursor; index height,width; }; Screen::Screen(index hght,index wdth,const string &cntnts=" "):contents(cntnts), cursor(0),height(hght),width(wdth){ contents.assign(hght*wdth,' '); if(cntnts.size()!=0) contents.replace(0,cntnts.size(),cntnts); } char Screen::get(index ht,index wd)const{ index row=ht*width; return contents[row+wd]; } inline Screen::index Screen::get_cursor() const{ return cursor; } Screen& Screen::set(char c){ contents[cursor]=c; return *this; } Screen& Screen::move(index r,index x) { if(r>=height||x>=width){ cerr<<":invelid row or c"<<endl; throw EXIT_FAILURE; } index row=r*width; cursor=row+x; return *this; } Screen& Screen::display(ostream &os){ string::size_type index=0; while(index!=contents.size()){ os<<contents[index]; if((index+1)%width==0){ os<<'\n'; }++index; } return *this; } int main(){ Screen myscreen(5,6,"aaaaa\naaaaa\naaaaa\naaaaa\naaaaa\n"); myscreen.move(4,0).set('#').display(cout); return 0; }
为什么(4,0)运行程序修改的是第五行的第一个,