获取数组长度,是经常需要用到的函数。以下做一些常用的比较
1. size()与length(),获取字符串长度时,作用相同的。
2. sizeof()是用来获取对象所占内存的大小。
3. size()是获取字符元素的函数, 用于字符串。
实例:
#include <iostream>
#include <string>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv)
{
char a = 'c';
char arr[] = "abcedf";
string s = "hello!";
cout << sizeof(a) << endl; // 1
cout << sizeof(arr) << endl; // 7
cout << s.size() << endl; // 6
cout << s.length() << endl; // 6
return 0;
}
最后
以上就是自信丝袜最近收集整理的关于C++ 获取数组长度函数比较的全部内容,更多相关C++内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复