Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sort merge vector of vectors

I have a vector of vectors, like this vector<vector<string>> vecData;

for example, a vector contains the following data(the data inside the vectors has already been sorted):

vecData[0] == {"1","5"}
vecData[1] == {"2","7"}
vecData[2] == {"6","9"}
vecData[3] == {"4"}`

how can I get a sorted merged vector resVec

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

recVec[0] == "1" 
recVec[1] == "2" 
recVec[2] == "4" 
..............
recVec[6] == "9"` 

I tried something like this, but I understand that this is complete nonsense

vector<vector<string>> separateFileData;
............................
vector<string> temp = separateFileData[0];
temp.reserve(1024);
for (auto iter = separateFileData.begin(); iter<separateFileData.end() - 1; iter++)
{
    merge(iter->begin(), iter->end(), (iter+1)->begin(), (iter + 1)->end(), temp.begin());
}

>Solution :

Like this, assuming I’m understanding you right.

vector<string> recVec;
for (const auto& v : vecData)
     recVec.insert(recVec.end(), v.begin(), v.end());
sort(recVec.begin(), recVec.end());

All strings from vecData are combined into recVec which is then sorted.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading