標題:
排序 - vector
[打印本頁]
作者:
tonyh
時間:
2022-12-15 20:36
標題:
排序 - vector
本帖最後由 tonyh 於 2022-12-22 20:06 編輯
vector<int> v={5,7,3,9,8,1,2};
練習以各種可能的方式,針對 vector 做遞增與遞減排序。
#include<bits/stdc++.h>
using namespace std;
vector<int> v= {5,7,3,9,8,1,2};
int len;
bool compare(int a, int b)
{
return a>b;
}
void show()
{
for(int i: v)
cout<<i<<" ";
cout<<endl;
}
void show2()
{
for(int i=0; i<len; i++)
cout<<v[i]<<" ";
//cout<<v.at(i)<<" ";
cout<<endl;
}
void show3()
{
//for(vector<int>::iterator it=begin(v); it!=end(v); it++)
for(auto it=begin(v); it!=end(v); it++)
cout<<*it<<" ";
cout<<endl;
}
int main()
{
show();
len=v.size();
//遞增
//sort(v.begin(), v.end());
sort(begin(v), end(v));
//show();
show3();
//遞減
//sort(v.rbegin(), v.rend());
//sort(rbegin(v), rend(v));
sort(begin(v), end(v), compare);
show();
return 0;
}
複製代碼
作者:
黃宇綸
時間:
2022-12-22 20:07
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REP(i,n) FOR(i,0,n)
#define REP1(i,n) FOR(i,1,(n)+1)
#define RREP(i,n) for(int i=(n)-1;i>=0;i--)
#define f first
#define s second
#define pb push_back
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)(x.size())
#define SQ(x) (x)*(x)
#define pii pair<int,int>
#define Graph vector<vector<int>>
#define IOS() cin.sync_with_stdio(0),cin.tie(0),cout.tie(0)
const ll inf=(1ll<<63)-1;
const int maxn=1e5+5;
const ll mod=1e9+7;
vector<int> v={5,7,3,9,8,1,2};
bool so(int a,int b) { return a>b; }
signed main()
{
IOS();
int n=SZ(v);
sort(ALL(v));
REP(i,n) cout<<v[i]<<" "; cout<<"\n";
sort(ALL(v),so);
//sort(v.rbegin(),v.rend());
REP(i,n) cout<<v[i]<<" "; cout<<"\n";
return 0;
}
複製代碼
作者:
黃宥華
時間:
2022-12-22 20:08
#include<bits/stdc++.h>
using namespace std;
vector<int> vec={5,7,3,9,8,1,2,4,6};
int len=vec.size();
bool compare(int a, int b)
{
return a>b;
}
void show(){
cout<<"-----------------"<<endl;
for(int i: vec)
cout<<i<<" ";
cout<<endl;
}
void show2()
{
for(int i=0; i<len; i++)
cout<<vec[i]<<" ";
cout<<endl;
}
void show3()
{
for(auto it=begin(vec); it!=end(vec); it++)
cout<<*it<<" ";
cout<<endl;
}
int main()
{
show();
//sort(vec.begin(), vec.end());
sort(begin(vec),end(vec));
show2();
//sort(vec.rbegin(), vec.rend());
//sort(rbegin(vec), rend(vec));
sort(begin(vec),end(vec),compare);
show3();
return 0;
}
複製代碼
作者:
李沛昂
時間:
2022-12-22 20:09
#include<bits/stdc++.h>
using namespace std;
vector<int> v= {5,7,3,9,8,1,2};
int len;
bool compare(int a, int b)
{
return a>b;
}
void show()
{
for(int i: v)
cout<<i<<" ";
cout<<endl;
}
void show2()
{
for(int i=0; i<len; i++)
cout<<v[i]<<" ";
cout<<endl;
}
void show3()
{
for(auto it=begin(v); it!=end(v); it++)
cout<<*it<<" ";
cout<<endl;
}
int main()
{
show();
len=v.size();
sort(begin(v), end(v));
show3();
sort(begin(v), end(v), compare);
show();
return 0;
}
複製代碼
作者:
黃宇瑄
時間:
2022-12-22 20:10
#include<bits/stdc++.h>
using namespace std;
vector<int> v= {5,7,3,9,8,1,2};
int len;
bool compare(int a, int b)
{
return a>b;
}
void show2()
{
for(int i=0; i<len; i++)
cout<<v[i]<<" ";
cout<<endl;
}
void show3()
{
for(auto it=begin(v); it!=end(v); it++)
cout<<*it<<" ";
cout<<endl;
}
int main()
{
for(int i: v)
cout<<i<<" ";
cout<<endl;
len=v.size();
sort(begin(v), end(v));
show3();
sort(begin(v), end(v), compare);
show();
return 0;
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2