一句话搞定string类型大小写转换之transform用法
来源:互联网 发布:java开源博客系统 编辑:程序博客网 时间:2024/06/06 09:14
对于cpp中的string类型大小写转换用的可以说非常频繁,下面是使用STL实现的一种非常简单的方式进行转换
std::transform (strExt.begin(), strExt.end(), strExt.begin(), ::toupper ); //将strExt转换成大写。
其实transform的用法非常广泛,下面对其进行简要介绍。
查看transform的标准定义
template < class InputIterator, class OutputIterator, class UnaryOperator > OutputIterator transform ( InputIterator first1, InputIterator last1, OutputIterator result, UnaryOperator op );template < class InputIterator1, class InputIterator2, class OutputIterator, class BinaryOperator > OutputIterator transform ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator result, BinaryOperator binary_op );其实现类似于下面:
template < class InputIterator, class OutputIterator, class UnaryOperator > OutputIterator transform ( InputIterator first1, InputIterator last1, OutputIterator result, UnaryOperator op ){ while (first1 != last1) *result++ = op(*first1++); // or: *result++=binary_op(*first1++,*first2++); return result;}
举一个例子,对vector进行操作
// transform algorithm example#include <iostream>#include <algorithm>#include <vector>using namespace std;int op_increase (int i) { return ++i; }int op_sum (int i, int j) { return i+j; }int main () { vector<int> first; vector<int> second; vector<int>::iterator it; // set some values: for (int i=1; i<6; i++) first.push_back (i*10); // first: 10 20 30 40 50 second.resize(first.size()); // allocate space transform (first.begin(), first.end(), second.begin(), op_increase); // second: 11 21 31 41 51 transform (first.begin(), first.end(), second.begin(), first.begin(), op_sum); // first: 21 41 61 81 101 cout << "first contains:"; for (it=first.begin(); it!=first.end(); ++it) cout << " " << *it; cout << endl; return 0;}结果输出
first contains: 21 41 61 81 101
- 一句话搞定string类型大小写转换之transform用法
- 一句话搞定string类型大小写转换之transform用法
- C++ string大小写转换以及transform,tolower,toupper,用法
- transform函数转换字符串string的大小写
- 使用transform函数转换字符串string的大小写
- transform函数转换大小写
- C++ STL string 大小写转换时的 no matching function for call to ‘transform 错误
- C++ STL string 大小写转换时的 no matching function for call to ‘transform 错误
- string 大小写转换
- string大小写转换函数
- string大小写转换函数
- C++ string大小写转换
- string的大小写转换
- string 大小写转换
- string大小写转换函数
- string大小写转换
- C++ string大小写转换
- JAVA String 大小写转换
- BF533 LED学习(1):头文件
- linux内核中jiffies的回绕问题
- SAP 取月度期初库存和月度期末库存(历史库存)
- winC++ 由std::string转为LPCWSTR
- cocos2d-x action动作整理集合
- 一句话搞定string类型大小写转换之transform用法
- flex 非模态的titlewindow,点击titlewindow外部时的事件
- 一个外企HR自述如何筛选简历
- LINUX下DNS的配置及基本介绍!
- Logcat过滤及常见用法整理
- 网络营销技巧:如何判断网站是否被Google惩罚
- centos安装CODEBLOCKS
- Linux服务器安全配置
- 深入分析Linux内核源码