Results 1 to 5 of 5

Thread: The inplace_merge() of C++

  1. #1
    Join Date
    Jan 2010
    Posts
    26

    The inplace_merge() of C++

    Hi, How are you!! I am the studying in the BSC-IT first year. I can have learn the C++ language. I can know the concept of functions. I can learn the functions in the first semester of the BSC-IT in the C language. So, I would like to know about the inplace_merge() function of the C++ language. I also would like to know about what is the use of the inplace_merge() function of the C++ language. Anyone can help me to know about the inplace_merge() function of the C++ language, Reply Me!!!

  2. #2
    Join Date
    Oct 2005
    Posts
    2,393

    Re: The inplace_merge() of C++

    The inplace_merge() function of the C++ language can be used to merge consecutive given specified ranges that can be sorted. The following can be the general form of the inplace_merge() function of the C++ language as :
    Code:
    template <class BidrctnlItrtr>
      void inplace_merge ( BidrctnlItrtr fst, BidrctnlItrtr mdl,
                           BidrctnlItrtr lst );

  3. #3
    Join Date
    Nov 2005
    Posts
    1,323

    The inplace_merge() of C++

    The following program can demonstrates you how to implement the inplace_merge() function of the C++ language in the C++ programming as follows :
    Code:
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    int main () 
    {
      int fst[] = {5,10,15,20,25};
      int scnd[] = {50,40,30,20,10};
      vector<int> w(10);
      vector<int>::iterator it;
      sort (fst,fst+5);
      sort (scnd,scnd+5);
      copy (fst,fst+5,w.begin());
      copy (scnd,scnd+5,w.begin()+5);
      inplace_merge (w.begin(),w.begin()+5,w.end());
      cout << "The resulting vector contains:";
      for (it=w.begin(); it!=w.end(); ++it)
      cout << " " << *it;
      cout << endl;
      return 0;
    }
    Output:
    Code:
    The resulting vector contains: 5 10 10 15 20 20 25 30 40 50

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    Re: The inplace_merge() of C++

    The following can be the parameters of the inplace_merge() function of the C++ language as :
    1. mdl : This parameter of the inplace_merge() function can contains bidirectional iterator to the starting position of the given second sequence that can because both the sequences can be similar in the sequence that can matches the past-the-end position of the first sequence.
    2. lst : This parameter of the inplace_merge() function can contains bidirectional iterator to the final position of the given sequence that can be second. This can be also the final position in the resulting range that can be merged can be stored.
    3. fst : This parameter of the inplace_merge() function can contains bidirectional iterator to the starting position in the first sequence that can be merged. This can be also the starting position of where the resulting range can be merged can be stored.

  5. #5
    Join Date
    Apr 2008
    Posts
    2,005

    Re: The inplace_merge() of C++

    This can be one more example for you that can states you what is the use and how to use the inplace_merge() function in the C++ programming as follows :
    Code:
    #include <functional>   
    #include <iostream> 
    #include <iterator> 
    #include <vector>
    int main ()
    {
    typedef std::vector<int, std::allocator<int> > Vector;
    const Vector::value_type d1[] = { 1, 2, 3, 4};
    const Vector::value_type d2[] = { 11, 13, 15, 17, 
      12, 14, 16, 18};
    Vector v1 (d1 + 0, d1 + sizeof d1 / sizeof *d1);
    Vector v2 (d1 + 0, d1 + sizeof d1 / sizeof *d1);
    Vector v3 (d2 + 0, d2 + sizeof d2 / sizeof *d2);
    Vector v4 (v3);
    Vector v5 (v3);
    Vector v6 (v3);
    Vector v7;
    std::merge (v1.begin (), v1.end (),
    v2.begin (), v2.end (), v3.begin ());
    std::merge (v1.begin (), v1.end (),
    v2.begin (), v2.end (), v4.begin (), 
    std::less<int>());
    Vector::iterator mid = v5.begin ();
    std::advance (mid, 4);
    std::inplace_merge (v5.begin (), mid, v5.end ());
    mid = v6.begin ();
    std::advance (mid, 4); 
    std::inplace_merge (v6.begin (), mid, v6.end (), 
    std::less<int>());
    std::merge (v1.begin (), v1.end (),
    v2.begin (), v2.end (), 
    std::back_inserter (v7));
    std::ostream_iterator<int, char, std::char_traits<char> >
      out (std::cout," ");
    std::copy (v1.begin (), v1.end (), out);
    std::cout << std::endl;
    std::copy (v2.begin(), v2.end (), out);
    std::cout << std::endl;
    std::copy (v3.begin (), v3.end (), out);
    std::cout << std::endl;
    std::copy (v4.begin (), v4.end (), out);
    std::cout << std::endl;
    std::copy (v5.begin (), v5.end (), out);
    std::cout << std::endl;
    std::copy (v6.begin (),v6.end (), out);
    std::cout << std::endl;
    std::copy (v7.begin (), v7.end (), out);
    std::cout << std::endl;
    std::merge (v1.begin (), v1.end (), 
    v2.begin (), v2.end (), out);
    return 0;
    }
    Program Output:
    Code:
    1 2 3 4 
    1 2 3 4 
    1 1 2 2 3 3 4 4 
    1 1 2 2 3 3 4 4 
    11 12 13 14 15 16 17 18 
    11 12 13 14 15 16 17 18 
    1 1 2 2 3 3 4 4 
    1 1 2 2 3 3 4 4
    Last edited by kelfro; 03-03-2010 at 10:23 PM.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,328,320.43600 seconds with 16 queries