若干有序合并成依然有序的序列
#if 1 #include <iostream> #include <string> #include <cstdio> #include <cstdlib> #include <sstream> #include <iostream> #include <string> #include <vector> #include <list> #include <iostream> #include <algorithm> #include <numeric> #include <cstdlib> #include <cstdio> #include<stack> #include <string> #include <set> #include <sstream> #include <iterator> using namespace std; vector<int> MergeList(vector<int> list1, vector<int> list2){ vector<int> result; int i = 0, j = 0; for (; i < list1.size() && j < list2.size();){ if (list1[i] < list2[j]){ result.push_back(list1[i]); i++; } else{ result.push_back(list2[j]); j++; } } while (i < list1.size()){ result.push_back(list1[i]); i++; } while (j < list2.size()){ result.push_back(list2[j]); j++; } return result; } vector<int>* Merge(vector<vector<int>> vect){ vector<int> result; if (vect.size() < 1) return NULL; result = vect[0]; for (int i = 1; i< vect.size(); i++){ result = MergeList(result, vect[i]); } return &result; } vector<int> Merge1(vector<vector<int>>& vect){ vector<int> result,result1; if (vect.size() < 1) return result; result = vect[0]; for (int i = 1; i < vect.size(); i++){ result1.resize(vect[i].size() + result.size()); merge(vect[i].begin(), vect[i].end(), result.begin(), result.end(), result1.begin()); result = result1; } return result; } vector<int> testSplit(string srcStr, const string& delim) { int nPos = 0; vector<int> vec; nPos = srcStr.find(delim.c_str()); while (-1 != nPos) { string temp = srcStr.substr(0, nPos); vec.push_back(stoi(temp)); srcStr = srcStr.substr(nPos + 1); nPos = srcStr.find(delim.c_str()); } vec.push_back(stoi(srcStr)); for (auto i:vec) { cout << i << endl; } return vec; } int main() { int num = 0, b = 0; string name,subname; cin >> num; vector<vector<int>> vvi(num); cin.ignore(199, '\n'); for (int i = 0;i <num;i++) { getline(cin, name); vvi[i]=testSplit(name,","); //cin.ignore(199, '\n'); } for (int i = 0; i < vvi.size();i++) { for (int j = 0; j < vvi[i].size();j++) { cout << vvi[i][j] << " "; } cout << endl; } vector<int> vi=Merge1(vvi); for (auto v:vi) { cout << v << " "; } cout << endl; system("pause"); } #endif