It isn't clear from your example what 'string' is. If you have:
#include <string>
using namespace std;
then string
is std::string
, and sizeof(std::string)
gives you the size of the class instance and its data members, not the length of the string. To get that, use:
string s;
cout << s.size();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…