for(auto &a:c)的基本的使用

Posted 五菱宏光车神

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了for(auto &a:c)的基本的使用相关的知识,希望对你有一定的参考价值。

for(auto &a:c) 的用法
昨天我第一次见到for(auto &a:c) 的使用,在忽略大小写比较字符串大小,用它可以直接调整整体的字符串的大小写;还有一系列的基本的函数都可以使用。

 #include<bits/stdc++.h>
using namespace std;
string a[1000],b,d;
int main()

    string s("hello world");
      for(auto c:a)
       c = 't';
      cout<< s<<endl;//未改变输出为“hello wrold
      for(auto &c:s)
        c= 't';
      cout<<s<<endl;//改变输出为“ttttttttttt”


实例的使用;
忽略大小写的比较
#include

#include <string>

using namespace std;

void add(string a,string b)

        for(auto &c:a)
            c = tolower(c);
        for(auto &c:b)
            c = tolower(c);
        if(a==b)
            cout<<"=";
        else if(a>b)
            cout<<">";
        else if(a<b)
            cout<<"<";



int main()

    string a,b;
    getline(cin,a);
    getline(cin,b);
    add(a,b);

    return 0;


以上是关于for(auto &a:c)的基本的使用的主要内容,如果未能解决你的问题,请参考以下文章