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> ...
随机推荐
- 【bzoj2287】[POJ Challenge]消失之物 背包dp
题目描述 ftiasch 有 N 个物品, 体积分别是 W1, W2, ..., WN. 由于她的疏忽, 第 i 个物品丢失了. “要使用剩下的 N - 1 物品装满容积为 x 的背包,有几种方法呢? ...
- 【bzoj3262】陌上花开 CDQ分治+树状数组
题目描述 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),又三个整数表示.现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量.定义一朵花A比另一朵花B要美丽,当且仅当Sa&g ...
- jQuery - AJAX get()和post()方法
jQuery get()和post()方法用于通过HTTP GET或POST请求从服务器请求数据. HTTP请求:GET VS POST 两种在客户端和服务器端进行请求-响应的常用方法是:GET和PO ...
- P1641 [SCOI2010]生成字符串
P1641 [SCOI2010]生成字符串 题目描述 lxhgww最近接到了一个生成字符串的任务,任务需要他把n个1和m个0组成字符串,但是任务还要求在组成的字符串中,在任意的前k个字符中,1的个数不 ...
- ionic2-键盘覆盖输入框和返回键问题解决方案
http://blog.csdn.net/u012979009/article/details/52514892有遇到这个问题的去这个地址看
- CodeIgniter自带的数据库类使用介绍
在 CodeIgniter 中,使用数据库是非常频繁的事情.你可以使用框架自带的数据库类,就能便捷地进行数据库操作. 初始化数据库类 依据你的数据库配置载入并初始化数据库类: view source ...
- wget命令下载FTP整个目录进行文件备份
使用wget下载整个FTP目录,可以用于服务器间文件传输,进行远程备份.通过限制网速,可以解决带宽限制问题. #wget ftp://IP:PORT/* --ftp-user=xxx --ftp-pa ...
- jquery处理鼠标左中右键事件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- JS中二维数组的声明
var myarr=new Array(); //先声明一维 for(var i=0;i<2;i++){ //一维长度为2 myarr[i]=new Array(); //再声明二维 for(v ...
- 根据select创建input并赋值
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> ...