merging n vectors

This is a discussion on merging n vectors within the C++ Programming forums, part of the General Programming Boards category; I have n sorted streams. I was wondering if there was a general way to merge them without the code ...

  1. #1
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926

    merging n vectors

    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 streams
    Code:
    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;

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Why not load the data into actual vectors and merge the data in the vectors with an algorithm from <algorithm>?

    BTW, does your current code repeat the last value of the streams an extra time?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    Calling two input streams 'out' and 'out2' is just too weird.

    A vector of N stream references perhaps?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    I can't load the values from the stream into a vector and then merge because the streams are from jobs that are running in parallel. So I don't think I can do that, but I'm not sure. I think the best way would be a vector of n streams. Thanks. I'll try that.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,024
    You can do it with some thread security if you want. But they're generally expensive and you have to ask yourself if it's worth it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by linuxdude View Post
    I can't load the values from the stream into a vector and then merge because the streams are from jobs that are running in parallel. So I don't think I can do that, but I'm not sure. I think the best way would be a vector of n streams. Thanks. I'll try that.
    For better performance when merging from many streams, store pointers to the streams in heap order inside the vector.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 09:21 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 03:05 PM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 11:35 AM
  4. How to use Vector's in C++ !?!
    By IndioDoido in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 12:13 PM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 02:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21