#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>
using namespace std;
int main(void) {
char a[5] = { 'a','b','c','d'};
cout << "a: " << sizeof(a) << " " << strlen(a) << endl;
string b = "abcd";
cout << "b: " << b.size() << " " << b.length() << endl;
system("pause");
}
输出:
a: 5 5
b: 4 4
C++字符串长度与内存占用探究
本文通过实例展示了C++中字符数组和字符串对象的内存占用及长度计算,对比了`sizeof`和`strlen`的区别,并提及了`string`类的`size()`和`length()`方法。
1908

被折叠的 条评论
为什么被折叠?



