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> ...
随机推荐
- java基础知识(一)- 数据类型
Java有两大数据类型: 基本数据类型,基本数据类型都可以直接分配到栈中 引用数据类型,引用存放在栈中,对象本身存放在堆中 基本数据类型共有四类八种 第一类:字符型(char) 第二类:逻辑型(boo ...
- [剑指Offer] 14.链表中倒数第k个结点
[思路]利用两个相隔为k-1个结点的指针进行遍历,当后一个指针移到末尾时,前一个指针就是要求的结点. /* struct ListNode { int val; struct ListNode *ne ...
- servletContext的定义
- Educational Codeforces Round 53 Div. 2翻车记
A:差点开场懵逼.只要有相邻两位不同就可以作为答案. #include<iostream> #include<cstdio> #include<cmath> #in ...
- 【题解】CQOI2017老C的方块
网络流真的是一种神奇的算法.在一张图上面求感觉高度自动化的方案一般而言好像都是网络流的主阵地.讲真一开始看到这道题也有点懵,题面很长,感觉很难的样子.不过,仔细阅读了题意之后明白了:我们所要做的就是要 ...
- E. Sonya and Ice Cream(开拓思维)
E. Sonya and Ice Cream time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- Boke光纤交换机的snmp配置
今天我配置了一下Boke光纤交换机e-300的snmp trap的配置 敲击help命令你会发现配置snmp的命令共有四个 snmpconfig Config ...
- vue+koa+mysql简易demo
功能支持网址收藏编辑 代码: https://github.com/lanleilin/lanOdyssey/tree/master/vueKoa/webCollection1 运行方法: 在serv ...
- P3076 [USACO13FEB]出租车Taxi
题目描述 Bessie is running a taxi service for the other cows on the farm. The cows have been gathering a ...
- BZOJ 100题纪念