该问题归结为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中。

transform(str.begin(), str.end(), str.begin(), ::tolower); //将大写的都转换成小写
transform(str.begin(), str.end(), str.begin(), ::tolower); //将大写的都转换成小写
transform(str.begin(), str.end(), str.begin(), ::tolower); //将大写的都转换成小写

std::string 字符串大小写转换(转)的更多相关文章

  1. std::string转化大小写(C++)

    #include <string> #include <algorithm> void test() { std::string strA="QQQQWWWqqqqq ...

  2. boost 字符串大小写转换

    示例代码如下: #include <boost/algorithm/algorithm.hpp> #include <iostream> using namespace std ...

  3. linux bash shell:最方便的字符串大小写转换(lowercase/uppercase conversion) (转)

    原文地址:https://blog.csdn.net/10km/article/details/83384145 关于字符串大小写转换,是写 linux 脚本经常干的事儿,所以总想找个方便的方法让我少 ...

  4. [Swift]字符串大小写转换,同时实现本地化或设置语言环境

    在NSString中提供了3种字符串大小写转换方式:1. 转换字符串大小写2. 转换字符串大小写,并实现本地化3. 转换字符串大小写,并设置语言环境. 一. 转换字符串大小写如果只是想单纯的将字符串进 ...

  5. java字符串大小写转换的两种方法

    转载自:飞扬青春sina blogjava字符串大小写转换的两种方法 import java.io..* public class convertToPrintString {          pu ...

  6. 【转】Python 字符串大小写转换

    转载自:python 中字符串大小写转换 一.pyhton字符串的大小写转换, 常用的有以下几种方法: 1.对字符串中所有字符(仅对字母有效)的大小写转换,有两个方法: print 'just to ...

  7. python 字符串大小写转换(不能使用swapcase()方法)

    python 3字符串大小写转换 要求不能使用swapcase()方法 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wa ...

  8. std::string 字符串替换

    std::string 没有原生的字符串替换函数,需要自己来完成 string& replace_str(string& str, const string& to_repla ...

  9. std::string 字符串切割

    在很多字符串类库里都实现了split函数.不过在std里没有实现.在这里拿出几个: 1. 用单字符作为分隔 #include <string> #include <vector> ...

随机推荐

  1. 【python】python中的定义类属性和对像属性

    python中变量是没有类型的可以绑定任意类型,但是在语法上不能声明变量. 那我们怎麽来声名一个变量呢? f=None 这样我们给着个变量绑定了以各None类型,我们随时可用重新绑定其它类型.这样我们 ...

  2. BZOJ4690 Never Wait for Weights(并查集)

    带权并查集按秩合并即可维护. #include<iostream> #include<cstdio> #include<cmath> #include<cst ...

  3. 【题解】SHOI2001化工厂装箱员

    ————传送:洛谷P2530 这道题目还是挺简单的,状态也容易想到. 数据范围非常的小,所以即便是很多维度,复杂度也完全可以接受.定义状态:dp[i][a][b][c]为手上的货物拿到第i个时三种物品 ...

  4. ACM International Collegiate Programming Contest, Egyptian Collegiate Programming Contest (ECPC 2015)

    A.Arcade Game(康拓展开) 题意: 给出一个每个数位都不同的数n,进行一场游戏.每次游戏将n个数的每个数位重组.如果重组后的数比原来的数大则继续游戏,否则算输.如果重组后的数是最大的数则算 ...

  5. [codeforces] 17E Palisection

    原题 题目要求相交的回文串对数,这显然非常难,但是要有一种正难则反的心态,求不出来相交的,求出来不相交的不就好了! 对于每以位置i结尾的字符串,在他后面与他不相交的就是以这个位置为结尾的个数和以这个位 ...

  6. [Leetcode] Unique binary search trees ii 唯一二叉搜索树

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  7. bzoj 4004 [JLOI2015]装备购买 拟阵+线性基

    [JLOI2015]装备购买 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 1820  Solved: 547[Submit][Status][Dis ...

  8. MyBatis对象关联关系----多对多的保存与查询

    模拟情景: 对象:学生,课程 关系:一个学生可选多个课程,一门课程可被多个学生选择 一.保存 1.创建数据库表,student,course,student_course,其中student_cour ...

  9. oracle12c创建用户等问题

    一:前言 这几天我重新装了下电脑,然后自己有试着去装了下oracle11g,结果还是失败了然后我自己又去下载了最新的oracle12c,oracle12c中有两个用户sys和system,scott已 ...

  10. 图论:Stoer-Wagner算法

    利用Stoer-Wagner算法求无向图最小割 直接给出算法描述和过程实现: 算法步骤: . 设最小割cut=INF, 任选一个点s到集合A中, 定义W(A, p)为A中的所有点到A外一点p的权总和. ...