1.length()函數(shù)
在c++中,length()只是用來獲取字符串的長度。在獲取字符串長度的時候size()和length()函數(shù)作用相同。
string s = "asdafa";
cout << s.size()<<endl;
cout << s.length() << endl;
2.size()函數(shù)
size函數(shù)除了可以獲取字符串長度外,還可以獲取vector類型的長度。文章來源:http://www.zghlxwxcb.cn/news/detail-628736.html
vector<int> b;
cout << b.size() << endl; //獲取vector長度
cout << sizeof(b) << endl; //獲取vector容器所占空間,因為他是動態(tài)數(shù)組,與編譯器有關(guān)
3.sizeof()函數(shù)
在c++中,sizeof()用于獲取數(shù)據(jù)類型或者變量所占內(nèi)存空間的大小??梢酝ㄟ^sizeof計算獲取數(shù)組元素個數(shù)。文章來源地址http://www.zghlxwxcb.cn/news/detail-628736.html
int a[26] = { 0 };
//cout << a.size();
//cout << a.length();
cout << sizeof(a) << endl;
cout << (sizeof(a)/sizeof(a[0]))<< endl;
到了這里,關(guān)于【C++】sizeof()、size()、length()的用法及區(qū)別的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!