I have n sorted streams. I was wondering if there was a general way to merge them without the code growing. Here is what I have for 2 streamsCode:int merge_and_print(saga::job::istream out, saga::job::istream out2){ int temp=-1, temp2=-1; if(!out.eof() && !out.fail()){ out >> temp; } if(!out2.eof() && !out2.fail()){ out2 >> temp2; } while(1){ if(temp == -1 && temp2 == -1){ std::cout << std::endl << "Done reading output" << std::endl; return 1;} if(temp == -1){ std::cout << temp2 << " "; while(!out2.eof() && !out2.fail()){ out2 >> temp2; std::cout << temp2 << " "; } std::cout << std::endl << "Done reading output" << std::endl; return 1; } else if(temp2==-1){ std::cout << temp << " "; temp = -1; while(!out.eof() && !out.fail()){ out >> temp; std::cout << temp << " "; } std::cout << std::endl << "Done Reading output" << std::endl; return 1; } else if(temp <= temp2){ std::cout << temp << " "; if(!out.eof() && !out.fail()){ out >> temp; } else temp = -1; } else if(temp2 < temp){ std::cout << temp2 << " "; if(!out2.eof() && !out2.fail()){ out2 >> temp2; } else temp2=-1; } } return 0;



LinkBack URL
About LinkBacks



