发布日期:2018-03-26
C++字符串长度?+ 查看更多
C++字符串长度?
+ 查看更多
发布日期:2018-02-26 11:02
分类:CPlusPlus
浏览次数:172
C++中怎么获得字符串的字符个数?
回答
如果你想使用std::string,调用length():
const char *str = "\005hello"; std::cout << str + 1 << ":" << *str; // Outputs "hello:5"如果你想使用c-string,调用strlen() const char *str = "hello"; std::cout << str << ":" << strlen(str); // Outputs "hello:5"或者你碰巧喜欢使用Pascal风格字符串(或者f*****这种字符串当出现以NULL结尾时Joel Spolsky喜欢去调用他们)那就间接调用第一个字符
const char *str = "\005hello"; std::cout << str + 1 << ":" << *str; // Outputs "hello:5"