计蒜客练习题:蒜头君学英语(transform)

   

这里主要就是涉及到了一个string的大小写转换函数(在algorithm的库里):

        transform(first, last, result, op);

first是容器的首迭代器,last为容器的末迭代器,result为存放结果的容器,op为要进行操作的一元函数对象或sturct、class。

具体使用如下:

#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
 
using namespace std;
 
int main()
{
    string s = "Hello World";
    cout << s << endl;
    transform(s.begin(),s.end(),s.begin(),::toupper);//变大写
    cout << s << endl;
    transform(s.begin(),s.end(),s.begin(),::tolower);//变小写
   return 0; }

 另附AC代码:

#include <set>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
    int n;
    cin >> n;
    set<string> s;
    for (int i = 0; i < n; i++)
    {
        int temp1;
        string temp2;
        cin >> temp1 >> temp2;
        transform(temp2.begin(),temp2.end(),temp2.begin(),::tolower);
        if (!temp1)
            s.insert(temp2);
        else
        {
            if (s.count(temp2))
                cout << "Yes
";
            else
                cout << "No
";
        }
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/Vikyanite/p/11740964.html