I have a collection of about a hundred sorted vector<int> . Although most vectors have a small number of integers in them, some of the vectors contain large (> 10K) of them (thus, the vectors do not necessarily have the same size).
What I would like to do is essentially iterate over the smallest integer that is contained in all of these sorted vectors.
One way to do this is to combine all of these sorted vectors into a sorted vector and simply repeat. Thus,
Question 1: What is the fastest way to merge sorted vectors into a sorted vector?
I am sure that, on the other hand, there are faster / smarter ways to do this without merging and re-sorting all of this - perhaps by choosing the smallest integer iteratively from this collection of sorted vectors; without merging them first .. like this:
Question 2: What is the fast / best way to replenish the smallest element from a group of sorted vector<int> ?
Based on the answers below and comments on the question, I applied an approach in which I queue the priorities of iterators for sorted vectors. I'm not sure if this is efficient performance, but it seems to be very memory efficient. I think this question is still open, as I'm not sure that we have installed the fastest way.
// compare vector pointers by integers pointed struct cmp_seeds { bool operator () (const pair< vector<int>::iterator, vector<int>::iterator> p1, const pair< vector<int>::iterator, vector<int>::iterator> p2) const { return *(p1.first) > *(p2.first); } }; int pq_heapsort_trial() { /* Set up the Sorted Vectors */ int a1[] = { 2, 10, 100}; int a2[] = { 5, 15, 90, 200}; int a3[] = { 12 }; vector<int> v1 (a1, a1 + sizeof(a1) / sizeof(int)); vector<int> v2 (a2, a2 + sizeof(a2) / sizeof(int)); vector<int> v3 (a3, a3 + sizeof(a3) / sizeof(int)); vector< vector <int> * > sorted_vectors; sorted_vectors.push_back(&v1); sorted_vectors.push_back(&v2); sorted_vectors.push_back(&v3); /* the above simulates the "for" i have in my own code that gives me sorted vectors */ pair< vector<int>::iterator, vector<int>::iterator> c_lead; cmp_seeds mycompare; priority_queue< pair< vector<int>::iterator, vector<int>::iterator>, vector<pair< vector<int>::iterator, vector<int>::iterator> >, cmp_seeds> cluster_feeder(mycompare); for (vector<vector <int> *>::iterator k = sorted_vectors.begin(); k != sorted_vectors.end(); ++k) { cluster_feeder.push( make_pair( (*k)->begin(), (*k)->end() )); } while ( cluster_feeder.empty() != true) { c_lead = cluster_feeder.top(); cluster_feeder.pop(); // sorted output cout << *(c_lead.first) << endl; c_lead.first++; if (c_lead.first != c_lead.second) { cluster_feeder.push(c_lead); } } return 0; }