The C++ char_traits class member, length() program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Using the length() member function to returns the current number of elements in a string in C++ programming

To show: How to use the length() member function to returns the current number of elements in a string in C++ programming

 

// the C++ char_traits member, length() example

#include <string>

#include <iostream>

using namespace std;

 

int main(void)

{

size_t LenStr1;

const char* str1= "Testing 1...2...3";

 

cout<<"str1 C-string is: "<<str1<<endl;

cout<<"\nOperation: length(str1)"<<endl;

LenStr1 = char_traits<char>::length(str1);

cout<<"The length of str1 is: "<<unsigned int(LenStr1)<<endl<<endl;

 

return 0;

}

 

Output example:

 

str1 C-string is: Testing 1...2...3

 

Operation: length(str1)

The length of str1 is: 17

 

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index