std::string 字符串大小写转换(转)
该问题归结为std::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 );
说明:
对于第一个原型:函数将对从输入参数的first1-last1的全部变量做op函数操作。结果保存到result中,或是通过返回值返回。
对于原形二:这个是对一的一个扩展,对于1这个只能对单个元素队列进行op操作,而第二个原形则是对对first1-last1上的元素和first2开始序列的元素逐个binary_op计算再保存到结果
示例
int op_inc(int num) { return ++i; }
int op_sum(int numa,int numb) { return a+b; }
int main()
{
vector<int >first;
vector<int >second;
vector<int >result;
.........//初始化过程省略,不过要注意当调用第二个原型的时候,保持队列二的长度要大于等于1
transform(first.begin(), first.end(), result.begin(), op_inc);
transform(first.begin(), first.end(), second.begin(), result.begin(), op_sum);
........//输出结果
return 0;
}
以上是transform的基本使用,接下来说明如何用它来处理字符串的大小写转换
事实上很简单,主要用到了,单个字符的大小写转换函数:tolower(),toupper()
不过对于大写转小写,同时小写转大写的要自己单独处理,函数如下
char exchange(char c)
{
if(c <= 'Z' && c >= 'A')
c = tolower(c);
else if(c >= 'a' && c <= 'z')
c = toupper(c);
return c;
}
示例
std::string str = "Http";
transform(str.begin(), str.end(), str.begin(), ::tolower); //将大写的都转换成小写
transform(str.begin(), str.end(), str.begin(), ::toupper); //将小写的都转换成大写
transform(str.begin(), str.end(), str.begin(), exchange); //大小写切换
注以上结果都保存在str中。
std::string 字符串大小写转换(转)的更多相关文章
- std::string转化大小写(C++)
#include <string> #include <algorithm> void test() { std::string strA="QQQQWWWqqqqq ...
- boost 字符串大小写转换
示例代码如下: #include <boost/algorithm/algorithm.hpp> #include <iostream> using namespace std ...
- linux bash shell:最方便的字符串大小写转换(lowercase/uppercase conversion) (转)
原文地址:https://blog.csdn.net/10km/article/details/83384145 关于字符串大小写转换,是写 linux 脚本经常干的事儿,所以总想找个方便的方法让我少 ...
- [Swift]字符串大小写转换,同时实现本地化或设置语言环境
在NSString中提供了3种字符串大小写转换方式:1. 转换字符串大小写2. 转换字符串大小写,并实现本地化3. 转换字符串大小写,并设置语言环境. 一. 转换字符串大小写如果只是想单纯的将字符串进 ...
- java字符串大小写转换的两种方法
转载自:飞扬青春sina blogjava字符串大小写转换的两种方法 import java.io..* public class convertToPrintString { pu ...
- 【转】Python 字符串大小写转换
转载自:python 中字符串大小写转换 一.pyhton字符串的大小写转换, 常用的有以下几种方法: 1.对字符串中所有字符(仅对字母有效)的大小写转换,有两个方法: print 'just to ...
- python 字符串大小写转换(不能使用swapcase()方法)
python 3字符串大小写转换 要求不能使用swapcase()方法 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wa ...
- std::string 字符串替换
std::string 没有原生的字符串替换函数,需要自己来完成 string& replace_str(string& str, const string& to_repla ...
- std::string 字符串切割
在很多字符串类库里都实现了split函数.不过在std里没有实现.在这里拿出几个: 1. 用单字符作为分隔 #include <string> #include <vector> ...
随机推荐
- 算法(2) Find All Numbers Disappeared in an Array
题目:整数数组满足1<=a[i]<=n(n是数组的长度),某些元素出现一次,某些元素出现两次,在数组a[i]中找到[1,n]区间中未出现的数字.比如输入[4,3,2,7,8,2,3,1], ...
- JConsole本地连接失败
一.问题描述 笔者在使用JDK自带的JConsole小工具连接Myeclipse里面注册的MBean时,报如下错: 二.解决办法 添加JVM运行参数: -Dcom.sun.management.jmx ...
- P1140 相似基因
题目背景 大家都知道,基因可以看作一个碱基对序列.它包含了4种核苷酸,简记作A,C,G,T.生物学家正致力于寻找人类基因的功能,以利用于诊断疾病和发明药物. 在一个人类基因工作组的任务中,生物学家研究 ...
- 【题解】ZJOI2007报表统计
洛谷传送门 主要思路大概也是差不多的,对于两种询问分别用线段树与平衡树来维护. 1.MIN_SORT_GAP:显然平衡树简单操作,来一发前驱.后继即可. 2.MIN_GAP:这一个我用的是线段树:可以 ...
- js金额转大写数字
//金额转大写数字 const intToChinese = money => { //汉字的数字 let cnNums = new Array('零', '壹', '贰', '叁', '肆', ...
- Expect使用小记
By francis_hao May 31,2017 本文翻译了部分Expect的man手册,只选取了个人常用的功能,因此并不完善. Expect是一个可以和交互式程序对话的程序 概述 ...
- Ubuntu14.04 换源 阿里云
sudo cp /etc/apt/sources.list /etc/apt/sources.list_backup sudo vim /etc/apt/sources.list sudo apt-g ...
- bzoj1914 [Usaco2010 OPen]Triangle Counting 数三角形 计算机和
[Usaco2010 OPen]Triangle Counting 数三角形 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 526 Solved: 2 ...
- 固定width但是有间隔
<!DOCTYPE > <html> <head> <title></title> <meta name="name&quo ...
- 用eval转化对象
var str = '{"name": "tom","age": 12,"sex": "man"}' ...