X-man

导航

sort实现vector的多级排序

#include<stdio.h>
#include<vector>
#include<string>
#include<fstream>
#include<algorithm>//include count
#include<iostream>
using namespace std;
vector<string>v;
vector<string>::iterator it;
bool cmp(string s1,string s2)
{
    if(s1.length()<s2.length())return true;
    int c1=count(s1.begin(),s1.end(),'1');
    int c2=count(s2.begin(),s2.end(),'1');
    if(c1<c2)return true;
    if(s1<s2)return true;
}
int main()
{
    ifstream cin("C.txt");
    string s;
    int i=0;
    while(cin>>s)
        i++,v.push_back(s);
    
    for(it=v.begin();it!=v.end();it++)
    cout<<*it<<endl;
    return 0;
}
关键语句:sort(v.begin(),v.end(),cmp);//sort的vector 排序

posted on 2013-05-01 15:40  雨钝风轻  阅读(362)  评论(0编辑  收藏  举报