Practical C ++ language design-accept two non-decreasing linear tables and merge them into non-decreasing LC

Source: Internet
Author: User

Platform: VC ++ 2005 passed the test!
. Vcproj
This is the main project file of the VC ++ project generated by using the Application Wizard.
It contains the Visual C ++ version information that generates the file, as well as information about the platform, configuration, and project features selected by the Application Wizard.
Stdafx. H, stdafx. cpp
These files are used to generate a pre-compiled header (PCH) file named twod. PCH and a pre-compiled file named stdafx. obj.
These are all VC ++ files generated using the Application Wizard, so they are not listed
I will only list the main parts of the program!

// Mergelist_vector.cpp: defines the entry point of the console application.
//

# Include "stdafx. H"
# Include <iostream>
# Include <vector>
# Include <string>
# Include <algorithm>
Using namespace STD;

Void mergelist_l (const vector <int> & la, const vector <int> & Lb, vector <int> & lc );

Int _ tmain (INT argc, _ tchar * argv [])
{
Vector <int> La, LB, LC;
Cout <"***************** mergelist ******************* "<Endl;
Cout <"Enter the elements of the La linear table (elements are separated by spaces, and-1 is the sequence Terminator ):";
Int input =-1;
While (CIN> input) & (input! =-1 ))
La. push_back (input );

Cout <"Enter the elements in the LB linear table (elements are separated by spaces and-1 is the sequence Terminator ):";
Input =-1;
While (CIN> input) & (input! =-1 ))
LB. push_back (input );
// Sort the elements of La and lb to obtain two non-decreasing linear tables.
Vector <int>: iterator Pos;
Sort (LA. Begin (), La. End ());
Cout <"content after La sorting :";
For (INT I = 0; I <la. Size (); I ++)
Cout <la [I] <"";
Cout <Endl;
 
Sort (Lb. Begin (), LB. End ());
Cout <"LB sorted content :";
For (INT I = 0; I <lb. Size (); I ++)
Cout <LB [I] <"";
Cout <Endl;
 
LC. Resize (LC. Size () + (LA. Size () + lB. Size ()));

Mergelist_sq (La, LB, LC );

Cout <"LC content :";
For (INT I = 0; I <LC. Size (); I ++)
Cout <LC [I] <"";
Cout <Endl;

Return 0;
}

// Accept two non-decreasing linear tables and merge them into non-decreasing LC
Void mergelist_sq (const vector <int> & la, const vector <int> & Lb, vector <int> & lc)
{
Vector <int>: const_iterator Pa = La. Begin ();
Vector <int>: const_iterator Pb = LB. Begin ();
Vector <int>: const_iterator pa_last = La. End ();
Vector <int>: const_iterator pb_last = LB. End ();
Vector <int>: iterator Pc = Lc. Begin ();

// Note that the while loop contains Pa <pa_last instead of Pa <= pa_last
// This is because pa_last points to the next position of the last element in the vector.
While (Pa <pa_last) & (PB <pb_last )){
If (* Pa <= * pb) * PC ++ = * pA ++;
Else * PC ++ = * pb ++;
}
While (Pa <pa_last) * PC ++ = * pA ++;
While (PB <pb_last) * PC ++ = * pb ++;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.