码迷,mamicode.com
首页 > 编程语言 > 详细

C++获取数组长度

时间:2017-06-09 00:46:55      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:字符   ios   bsp   二维数组   string   维数   names   ace   using   

1、获取字符数组长度

char a[]="abcdef";

int length1=sizeof(a)/sizeof(a[0]);
cout<<"a"<<length1<<endl;

length1为7.

cout<<"a"<<strlen(a)<<endl;

输出6

注:string不能用strlen函数

2、获取字符串string长度

#include <string>
#include <iostream>
using namespace std;
int main()
{
string str = "abcdef";
cout << str.length() << endl;
cout << str.size() << endl;
return 0;
}

输出皆为6

3、获取int数组长度

int marks[5] = {40, 90, 73, 81, 35};
int length1=sizeof(marks)/4;
cout<<"a"<<length1<<endl;

输出为5,实际长度

获取二维数组长度:

int marks[2][3]={1,2,3,4,5,6};
int length1=sizeof(marks[0])/4;
cout<<"a"<<length1<<endl;
int length2=sizeof(marks)/4;
cout<<"a"<<length2<<endl;

输出3,和6

 

C++获取数组长度

标签:字符   ios   bsp   二维数组   string   维数   names   ace   using   

原文地址:http://www.cnblogs.com/answer727/p/6965245.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!