zoukankan      html  css  js  c++  java
  • ?:,reverse,vector的基本小结

     1 #include <cstdio>  //此代码为网上所复制
     2 #include <iostream>  
     3 #include <string>  
     4 #include <set>  
     5 #include <algorithm>  
     6 #include <vector>  
     7 using namespace std;
     8 
     9 bool Comp(const string &s1, const string &s2)
    10 {
    11     return s1.length() != s2.length() ? s1.length()<s2.length() : s1<s2;
    12 }
    13 int main()
    14 {
    15     vector<string> v;
    16     string t, s;
    17     int n;
    18     cin >> n;
    19     getchar();
    20     while (n--)
    21     {
    22         getline(cin, s);
    23         t = s;
    24         //反转字符串,用来判断字符是否对称  
    25         reverse(t.begin(), t.end());
    26         if (t == s)
    27         {
    28             v.push_back(s);
    29         }
    30     }
    31     sort(v.begin(), v.end(), Comp);
    32     for (int i = 0; i<v.size(); i++)
    33     {
    34         cout << v[i] << endl;
    35     }
    36     return 0;
    37 }

    vector可以当作一个动态数组用,遍历的时候也可以当做是一个数组,因为可以随机访问,所以可以使用sort等algorithm里的函数

    注意:下次如果遇到关于字符串倒转问题时首先考虑翻转reverse;

    还有(1)?(2):(3)的意思,1式为判断,true返回2式,flase返回3式

  • 相关阅读:
    codevs 2632 非常好友
    codevs 1213 解的个数
    codevs 2751 军训分批
    codevs 1519 过路费
    codevs 1503 愚蠢的宠物
    codevs 2639 约会计划
    codevs 3369 膜拜
    codevs 3135 River Hopscotch
    数论模板
    JXOJ 9.7 NOIP 放松模拟赛 总结
  • 原文地址:https://www.cnblogs.com/kangdong/p/8722950.html
Copyright © 2011-2022 走看看