#ifndef CP5_ex7_24_h
#define CP5_ex7_24_h
#include <string>
class Screen {
public:
using pos = std::string::size_type;
Screen() = default; // 1
Screen(pos ht, pos wd) : height(ht), width(wd), contents(ht * wd, ' ') {} // 2
Screen(pos ht, pos wd, char c) : height(ht), width(wd), contents(ht * wd, c)
{
} // 3
char get() const { return contents[cursor]; }
char get(pos r, pos c) const { return contents[r * width + c]; }
private:
pos cursor = 0;
pos height = 0, width = 0;
std::string contents;
};
#endif
其中contents(ht * wd, c)这个什么意思 contents不是string类型的吗?(ht * wd, c)是什么意思呢
这是string构造函数的一个重载
basic_string( size_type count,
CharT ch,
const Allocator& alloc = Allocator() );
相当于contents由ht*wd个c组成
随便拿一个代码试一下就知道了
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char const *argv[])
{
string s(10, 'c');
cout << s << endl; // s=cccccccccc
return 0;
}