首页 > 代码库 > 向量 vector

向量 vector

#include <iostream>#include <vector>#include <algorithm>using namespace std;bool cmp(int a,int b){    return a>b;  }int main(){     int n,i;    cin>>n;    vector<int> a(n);    for(i=0;i<n;i++)                      {         cin>>a[i];    }        sort(a.begin(),a.end(),cmp);        cout<<endl;    for(i=0;i<n;i++)                      {  cout<<a[i]<<"    ";    }    cout<<endl;    return 0;}
View Code

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool cmp(int a,int b)
{ return a>b; }

int main()
{
int n,i;
cin>>n;
vector<int> a(n);
for(i=0;i<n;i++)
{ cin>>a[i]; }

sort(a.begin(),a.end(),cmp);

cout<<endl;
for(i=0;i<n;i++)
{ cout<<a[i]<<" "; }
cout<<endl;
return 0;
}

 

 

 

 

 

 

 

#include <iostream>#include <vector>#include <algorithm>using namespace std;int main(){    int i=0;    vector<string> s_word(100),s_word1;    string word,word1;    while(cin>>word)    {        s_word[i++]=word;        s_word1.push_back(word);    }    for(int i=0;i<s_word.size();i++)    {        cout<<s_word[i]<<" ";    }        cout<<endl;    cout<<"单词个数为:"<<s_word.size()<<endl<<endl;    cout<<"去掉重复单词后:"<<endl;        //去年重复的单词    s_word.erase(unique(s_word.begin(),s_word.end()),s_word.end());        for(int i=0;i<s_word.size();i++)    {        cout<<s_word[i]<<" ";    }    cout<<endl;    cout<<"单词个数为:"<<s_word.size()<<endl;        //每个单词有原来的文章中所占的比率    cout<<"每个单词的频率:"<<endl;     for(int i=0;i<s_word.size();i++)    {        //cout<<s_word[i]<<":  "<<(double)count(s_word1.begin(),s_word1.end(),s_word[i])/s_word1.size()<<endl;    }    }
View Code

 

 

 

 

 

 

#include<iostream>#include<vector>#include<algorithm>using namespace std;struct f{   int x;    int y;    double r;}a[100000];int f1(int fz,int fm){    return fm==0?fz:f1(fm,fz%fm);}bool cmp(struct f a1,struct f a2){    return a1.r<a2.r;}int main(){    int n,i,j,k=0,t;    cin>>n;    for(i=1;i<n;i++)        for(j=i+1;j<=n;j++)          if(f1(i,j)==1)           {    a[k].x=i;a[k].y=j;a[k].r=(double)i/j; k++;}    cout<<0<</<<1<<endl;            sort(a,a+k,cmp);    for(i=0;i<k;i++)        cout<<a[i].x<</<<a[i].y<<endl;    cout<<1<</<<1<<endl;    return 0;} 
View Code