Article directory
1. String string conversion - std::transform function
1. std::transform function prototype description
The C++ std::transform
function is a general algorithm in the <algorithm> header file, used to convert elements within a specified range;
std
transform
The function in the command space is used to convert the contents of the specified range of the STL container;
According to the provided parameters, this function can extract characters from the source string, convert them according to the specified format, and then return a new string;
template< class InputIt, class OutputIt, class UnaryOperation >
OutputIt transform( InputIt first, InputIt last, OutputIt d_first, UnaryOperation unary_op );
template< class InputIt1, class InputIt2, class OutputIt, class BinaryOperation >
OutputIt transform( InputIt1 first1, InputIt1 last1, InputIt2 first2, OutputIt d_first, BinaryOperation binary_op );
Parameter Description :
- first: the starting iterator of the input range, indicating the range of elements to be converted;
- last: the terminal iterator of the input range, indicating the range of elements to be converted;
- d_first: the starting iterator of the output range, indicating the range in which the converted elements should be written;
- unary_op unary operation function object: used to convert each element in the input range;
- binary_op binary operation function object: used to combine and convert elements in input range 1 with elements in input range 2;
Operation function object:
- toupper: Convert string to uppercase letters;
- tolower: Convert the string to lowercase letters;
2. Code example - string class transform function conversion
Code example:
#include "iostream"
using namespace std;
#include "string"
#include "algorithm"
int main() {
string s1 = "Tom And Jerry";
// 将字符串转为大写字母
transform(s1.begin(), s1.end(), s1.begin(), toupper);
// 打印 s1值
cout << "s1 = " << s1 << endl;
// 将字符串转为小写字母
transform(s1.begin(), s1.end(), s1.begin(), tolower);
// 打印 s1值
cout << "s1 = " << s1 << endl;
// 控制台暂停 , 按任意键继续向后执行
system("pause");
return 0;
};
Results of the :
s1 = TOM AND JERRY
s1 = tom and jerry
Please press any key to continue. . .
2. String string flip - std::reverse function
1. std::reverse function prototype description
std::reverse
is an algorithmic function in the <algorithm> header file, used to reverse the order of elements within a given STL container range;
std::reverse
Function prototype:
template< class BidirectionalIt >
void reverse( BidirectionalIt first, BidirectionalIt last );
Parameter Description:
- first : A bidirectional iterator pointing to the starting element of the range to be reversed;
- last : A bidirectional iterator pointing to the terminating element of the range to reverse;
2. Code example - std::reverse function
Code example:
#include "iostream"
using namespace std;
#include "string"
#include "algorithm"
int main() {
string s1 = "Tom And Jerry";
// 将字符串 翻转
reverse(s1.begin(), s1.end());
// 打印 s1值
cout << "s1 = " << s1 << endl;
// 控制台暂停 , 按任意键继续向后执行
system("pause");
return 0;
};
Results of the :
s1 = yrreJ dnA moT
Please press any key to continue. . .