今天做一道题,要用string类,涉及大小写转换,查看了C++文档,string类没有提供这样的方法,只好自己写。
之后是想到一个比较笨的方法,我把string当成一个容器,然后用迭代器一个一个来替换。

比如下面的是大写转小写:

  1. string temp;
  2. string::iterator it;
  3. for (it = temp.begin(); it != temp.end(); it++)
  4. if ((*it) < 'a')
  5. *it = *it + 32;

测试一下代码:

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. int main(void)
  5. {
  6. string temp;
  7. string::iterator it;
  8. cin >> temp;
  9. for (it = temp.begin(); it != temp.end(); it++) //大写转小写
  10. if ((*it) < 'a')
  11. *it = *it + 32;
  12. cout <<"转换成小写之后" <<temp << endl;
  13. for (it = temp.begin(); it != temp.end(); it++) //小写转大写
  14. if ((*it) > 'Z')
  15. *it = *it - 32;
  16. cout <<"转换成大写之后" <<temp << endl;
  17. return 0;
  18. }

测试输入
AsdFghJkL

测试输出
转换成小写之后asdfghjkl
转换成大写之后ASDFGHJKL

测试图片:

但是后面我发现其他大佬有更简单的做法,使用模板函数transform可以轻松解决这个问题,我们只需要提供一个函数对象,例如将char转成大写的toupper函数或者小写的函数tolower函数。

transform原型:

  1. template <class InputIterator, class OutputIterator, class UnaryOperator>
  2. OutputIterator transform (InputIterator first1, InputIterator last1,
  3. OutputIterator result, UnaryOperator op)
  4. {
  5. while (first1 != last1) {
  6. *result = op(*first1); // or: *result=binary_op(*first1,*first2++);
  7. ++result; ++first1;
  8. }
  9. return result;
  10. }

以上的原型来自文档
C++官方文档

所以对于这个大小写转换只要这么写就行:

  1. transform(temp.begin(),temp.end(),temp.begin(),::tolower); //转小写
  2. transform(temp.begin(),temp.end(),temp.begin(),::toupper); //转大写

更改代码

  1. #include<algorithm>
  2. #include<iostream>
  3. #include<string>
  4. using namespace std;
  5. int main(void)
  6. {
  7. string temp;
  8. cin >> temp;
  9. transform(temp.begin(),temp.end(),temp.begin(),::tolower); //转小写
  10. cout <<"转换成小写之后" <<temp << endl;
  11. transform(temp.begin(),temp.end(),temp.begin(),::toupper); //转大写
  12. cout <<"转换成大写之后" <<temp << endl;
  13. return 0;
  14. }

结果一样:

string类中字符的大小写转换的更多相关文章

  1. String类中常用的操作

    一.获取: 1.获取字符串的长度(注意是方法,不是跟数组的属性一样的) int length(); 1 public static void getLength(){ 2 String s = &qu ...

  2. java面向对象中的String类中12种常用的方法

    1.字符串与字符数组的转换 字符串可以使用toCharArray()方法变成一个字符数组,也可以使用String类的构造方法把一个字符数组变成一个字符串. public class StringAPI ...

  3. String类中的常用方法

    String类 一.转换成String方法 1.public String(); 空参构造 初始化一个新创建的 String 对象,使其表示一个空字符序列 2.public String(byte[] ...

  4. js中实现字母大小写转换

    js中实现字母大小写转换主要用到了四个js函数: 1.toLocaleUpperCase  2.toUpperCase3.toLocaleLowerCase4.toLowerCase 下面就这四个实现 ...

  5. Java——String类中的compareTo方法总结

    String类的定义:    java.lang  类 String   java.lang.Object      java.lang.String 所有已实现的接口:Serializable, C ...

  6. String类中的equals()方法:

    String类中的equals()方法: public boolean equals(Object anObject) { //如果是同一个对象 if (this == anObject) { ret ...

  7. Java用代码演示String类中的以下方法的用法

    用代码演示String类中的以下方法的用法 (1)boolean isEmpty(): 判断字符串是不是空串,如果是空的就返回true (2)char charAt(int index): 返回索引上 ...

  8. 2019.4.1今日一练String类中的方法

    package com.pjc.objects;                        replaceAll()方法的理解引出正则表达式import java.util.regex.Patte ...

  9. String类中intern方法的原理分析

    一,前言 ​ 昨天简单整理了JVM内存分配和String类常用方法,遇到了String中的intern()方法.本来想一并总结起来,但是intern方法还涉及到JDK版本的问题,内容也相对较多,所以今 ...

随机推荐

  1. 获取当前的日期时间的js函数,格式为“yyyy-MM-dd hh:mm:ss”

    //获取当前的日期时间函数,格式为“yyyy-MM-dd hh:mm:ss” function getNowFormatDate(date) { if (date == null) { var dat ...

  2. 【数据库】2.0 MySQL入门学习(二)——如何获得MySQL以及MySQL安装

    1.0 如何获得MySQL: www.oracle.com https://dev.mysql.com/downloads/ 2.0 例如进入Oracle官网,找到MySQL: 进入页面后,切换到“资 ...

  3. 原生js实现雪花飘落效果

    雪花飘落的效果实现步骤:1.使用setInterval定时器每800毫秒创建一个雪花:2.把每一个雪花作为参数传进动态下落的方法中即可. <style> *{padding: 0;marg ...

  4. struts2.5框架使用通配符无效问题

    错误: Struts has detected an unhandled exception: Messages: There is no Action mapped for namespace [/ ...

  5. ContentProvider启动浅析

    一.自己的理解 对于content provide的启动我是这样认为的,要用ContentResolver去获得一个contentProvider,在这的获得的过程中, 1.如果本应用之前有conte ...

  6. Siebel escript学习笔记

    Siebel(escript)的学习:1.Siebel的数据类型Primitive(原始的)---Number,Integer,Hexadecimal(十六进制),Octal(八进制),Floatin ...

  7. 基于VB语言对SolidWorks参数化设计的二次开发

    0 引言 随着数字信息化进程的快速推进,如今三维CAD技术在越来越多的企业当中得到运用.为了降低在设计生产中的成本,缩短设计周期,增强企业竞争力,三维参数化技术随之应声,它凭借更贴近现代概念的设计以及 ...

  8. SQL 根据身份证号码获取年龄的函数

    在数据库的运用过程中,我们时常会碰到根据身份证号码来获取当前的年龄,今天我在这里写了一个函数,就是关于获取年龄的 create or replace function FUNC_COMPARE_SFZ ...

  9. 笔记本win8系统共享wifi上网方法

    华硕笔记本电脑,安装了win8系统,使用wifi上网,由于连接无线路由的机器太多,超过路由连接数上限,因此转为使用笔记本共享wifi方式给手机上网. 最终上网方式为: 笔记本网卡接入无线路由器上网,笔 ...

  10. nutz 结合QueryResult,Record 自定义分页查询,不构建pojo 整合

    public QueryResult getHistoryIncome(int d, int curPage) throws Exception { /**sql**/ Sql sql = Sqls. ...