C + + Get array length

Source: Internet
Author: User
Tags array length

1. Get the character array length

Char a[]= "abcdef";

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

Length1 is 7.

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

Output 6

Note: string cannot be used with the strlen function

2. Get string length

#include <string>
#include <iostream>
using namespace Std;
int main ()
{
String str = "ABCDEF";
cout << str.length () << Endl;
cout << str.size () << Endl;
return 0;
}

Output is 6

3. Get int Array length

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

Output is 5, actual length

Get the two-dimensional array length:

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;

Output 3, and 6

C + + Get array length

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.