• 把一个string串的所有小写字母转成大写字母的例子来看看看全局函数的使用


    今天写了一个小例子,把字符串里面的所有小写字母全部转换成大写字母
    http://blog.csdn.net/yasaken/article/details/7303903

    1
    #include "stdafx.h" 2 #include <string> 3 #include <algorithm> 4 #include <iostream> 5 6 using namespace std; 7 8 int _tmain(int argc, _TCHAR* argv[]) 9 { 10 string strA = "123@456$abc"; 11 transform(strA.begin(), strA.end(), strA.begin(), ::toupper); //当时一直没明白为什么toupper前面要加:: 12 cout<<strA<<endl; 13 getchar(); 14 return 0; 15 }
    如果没有 上面 using namespace std; 这句话 
    transform(strA.begin(), strA.end(), strA.begin(), ::toupper);和transform(strA.begin(), strA.end(), strA.begin(), toupper); 都可以编译通过

    从网上找了个讲解的例子http://blog.sina.com.cn/s/blog_48d5933f0100riz5.html

    标准库重载了一个toupper函数,而GCC完全由c库去提供重载, 而glibc做不到这一点,所以在编译的时候g++就认为这个函数有歧义了.下面是标准库中toupper函数的两种形式
    int std::toupper(int); //from <cctype>
    template<class charT>
    charT std::toupper(charT, const local&); //from <locale>
    此时有三种解决办法:
    第一种 包装函数-->只有在包装函数中指明要使用的函数,歧义自然就没了
    Int toUpper(int c)
    {
    return toupper(c);
    }
    第二种 强制转化--->将toupper转换为一个返回值是int,参数只有一个int的函数指针
    std::transform(s.begin(), s.end(), s.begin(), (int(*)(int)) toupper);
    第三种 GCC中将toupper实现为一个宏而不是函数,而在全局命名空间中有实现的函数(而不是宏), 所以我们明确命名空间,这并不总是奏效,但是我我的g++环境中没有这个问题
    transform(s.begin(), s.end(), s.begin(), ::toupper);

     再附带一个小例子


    现在我们来看看 transform函数, 开发人员只需要提供一个函数对象,例如将char转成大写的toupper函数或者小写的函数tolower函数
    template < class InputIterator, class OutputIteror, 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, InputIterator2 last1, InputIterator2 first2, OutputIterator result, BinaryOperator binary_op);
     
  • 相关阅读:
    linux面试常问命令
    图卷积网络 GCN Graph Convolutional Network(谱域GCN)的理解和详细推导
    总结一些牛客网上 的算法岗面试题
    Pytorch-Tensor基本操作
    C++使用代码创建一个Windows桌面应用程序
    Windows中的消息与消息队列
    解决COM组件在WPF设计器中命名空间不存在XXX的问题(附带如何在WPF中使用APlayer引擎)
    Mybatis的一级缓存
    面试题:zookeeper实现分布式锁
    sublime text 文件默认打开格式
  • 原文地址:https://www.cnblogs.com/silentNight/p/5406072.html
Copyright © 2020-2023  润新知