使用字符拷贝的 C++ 字符串初始化?

标签 c++ string

我正在学习 C++。 我的教科书是,从 C++ 开始,从控制结构到对象 7。 在string类解释中,有一段代码

string lineFull('z', 20); 
// Defines a string object named lineFill initialized with 10 'z' characters

我在我的电脑上试过这段代码,但没有用。这个代码对吗?或对此有任何依赖/要求吗?

提前感谢您的帮助!

最佳答案

看起来正确的语法是

string lineFull(20, 'z')

来自 std::string constructor docs

关于使用字符拷贝的 C++ 字符串初始化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15592155/

相关文章:

c++ - 为什么在代码 "456"+1,输出是 "56"

c++ - 编写程序语言解释器

c++ - 将 ICO 文件添加到 Win32 程序

Python 3.0+ 设置为最多 4 位小数和四舍五入

java - Java 中的 StringIndexOutOfBounds

c++ - 比较 vector 中的元素并返回对象的最佳方法

c++ - OpenCV findFundamentalMat 奇怪的行为

javascript - 写出一个字的JS函数,二进制计数器风格

c++ - 为什么 strlen() 在 VC++ 调试器中似乎返回 <void>?

string - Knuth-Morris-Pratt 算法中的 DFA 构造