清风与酒f42ij 题解分享 · 2026/4/6
vector排序——题解
include using namespace std; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n,c,t; cin>>n; vector a[n+1];//创建了n+1个vector对象,也就是一维数组a的每个元素都是vector for(int i=1;i >c;//输入数组的大小 while(c--)//利用while循环c次,将对应的数组元素推入每一个数组 { cin>>t; a[i].push_back(t); } sort(a[i].begin(),a[i].end());//每完成一次就先将当前数组排序 } sort(a+1,a+n+1);//按字典序对n个数组进行排序,因为数组一旦创建,下标就是从0开始的 //而我们在存数据的时候是从1开始的,所以要使用a+1,至于a+n+1,因为C++的 sort(first, last) 排序范围是 [first, last), //包含first,不包含last,所以sort的第二个参数要指向最后一个元素的下一个位置,是 a+n+1 for(int i=1;i<=n;i++) { for(int j=0;j<a[i].size();j++) { cout<<a[i][j]<<" ";//输出数组元素 //i代表是第i组数组,j代表是第i组数组的第j个元素 } cout<<endl; } return 0; }
查看全文
2 0 0 16
kunshao 题解分享 · 2025/3/14
vector排序 - 题解
``` /* 对数组元素排序 再对数组排序 */ #include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; //创建n个数组 vector<vector<int>> array(n); //添加数据 for ( int i = 0; i < n; i++ ) { int x; cin >> x; //根据x调整数组大小 用resize() array[i].resize(x); //输入 for ( int j = 0; j < x; j++ ) { cin >> array[i][j]; } //数组元素排序 sort(array[i].begin() , array[i].end()); } //输出 for ( int i = 0; i < n; i++ ) { //数组排序 sort(array.begin() , array.end()); //输出 for ( size_t j = 0; j < array[i].size(); j++ ) { cout << array[i][j] << " "; } //换行 cout << endl; } return 0; } ```
查看全文
0 0 0 19
晚舟 题解分享 · 2025/2/3
vector排序 - 题解
``` #include <bits/stdc++.h> using namespace std; int main() { vector<int> a[1003]; int n; cin >> n; int x, y; int c; for (int i = 0; i < n; i++) { cin >> c; for (int j = 0; j < c; j++) { cin >> x; a[i].push_back(x); } sort(a[i].begin(), a[i].end()); } sort(a, a + n); for (int i = 0; i < n; i++) { for (auto &ele : a[i]) { cout << ele << ' '; } cout << endl; } return 0; } ```
查看全文
0 0 0 12
CQS 题解分享 · 2025/3/5
vector排序 - 题解
``` #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int n; cin >> n; vector<vector<int>> arrays(n); for (int i = 0; i < n; i++) { int size; cin >> size; for (int j = 0; j < size; j++) { int num; cin >> num; arrays[i].push_back(num); } } for (int i = 0; i < n; i++) { sort(arrays[i].begin(), arrays[i].end()); } sort(arrays.begin(), arrays.end()); for (int i = 0; i < n; i++) { for (const auto& elem : arrays[i]) { cout << elem <<" "; } cout << endl; } return 0; } ```
查看全文
0 0 0 8