The C++ char_traits class member functions: move()/ _Move_s(), find() and length() C++ 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 _Move_s()/move() to copies a specified number of characters in a sequence to another, possibly overlapping sequence, find() to search for the first occurrence of a specified character in a range of characters and length() to returns the length of a string in C++ programming

To show: How to use _Move_s()/move() to copies a specified number of characters in a sequence to another, possibly overlapping sequence and find() to search for the first occurrence of a specified character in a range of characters  and length() to returns the length of a string in C++ programming in C++ programming

 

// the char_traits class member functions: move()/_Move_s(), find() and length()

#include <string>

#include <iostream>

using namespace std;

 

int main(void)

{

char_traits<char>::char_type str1[25] = "The Hell Boy";

char_traits<char>::char_type str2[25] = "Something To ponder";

char_traits<char>::char_type *result1;

 

cout<<"The source str1 string is: "<<str1<<endl;

cout<<"The destination str2 string is: "<<str2<<endl;

 

// result1 = char_traits<char>::move(str2, str1, 10);

result1 = char_traits<char>::_Move_s(str2, char_traits<char>::length(str2), str1, 10);

cout<<"\nOperation: move(str2, str1, 10)"<<endl;

cout<<"The result1 = "<<result1<<endl;

 

// when source and destination overlap

char_traits<char>::char_type str3[30] = "Testing the move()";

char_traits<char>::char_type *result2;

cout << "The source/destination (overlap) str3 string is: "<<str3<<endl;

cout<<"\nOperation: str4 = find(str3, 12, 'h')"<<endl;

 

const char *str4 = char_traits<char>::find(str3, 12, 'h');

cout<<"Operation: move(str3, str4, 9)"<<endl;

// result2 = char_traits<char>::move(str3, str4, 9);

result2 = char_traits<char>::_Move_s(str3, char_traits<char>::length(str3), str4, 9);

cout<<"The result2 = "<<result2<<endl;

 

return 0;

}

 

Output example:

 

The source str1 string is: The Hell Boy

The destination str2 string is: Something To ponder

 

Operation: move(str2, str1, 10)

The result1 = The Hell BTo ponder

The source/destination (overlap) str3 string is: Testing the move()

 

Operation: str4 = find(str3, 12, 'h')

Operation: move(str3, str4, 9)

The result2 = he move()he move()

Press any key to continue . . .

 

 

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