?:,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式

posted @ 2018-04-05 15:59  zzuli风尘  阅读(306)  评论(0编辑  收藏  举报