|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
为什么我str无论输多长,sizeof(str)总等于24- #include <iostream>
- #include <string>
- int main()
- {
- std::string str = "qwertyuiopasdfghjklmnbvczaqwedsxcvfrtgbnhyujm,kiol.,kiujmnhytgbvfredcxswqazqwertaysudhfncjdjdhdiecheuihduhd";
- std::cout<<str.length()<<"\n";
- std::cout<<sizeof(str)<<"\n";
- return 0;
- }
复制代码
str 是一个string 对象, sizeof(str)返回的是str这个对象的大小,
获取一个str的长度可以这样
- #include <iostream>
- #include <string>
- int main()
- {
- std::string str = "qwertyuiopasdfghjklmnbvczaqwedsxcvfrtgbnhyujm,kiol.,kiujmnhytgbvfredcxswqazqwertaysudhfncjdjdhdiecheuihduhd";
- std::cout<<str.length()<<"\n";
- std::cout<<sizeof(str)<<"\n";
- std::cout<<str.size()<<std::endl;
- return 0;
- }
复制代码
|
|