C ++ randomly breaks the Array

Source: Internet
Author: User

Want to start learning C ++ and check whether g ++-V is installed on the server.

Configured :.. /configure -- prefix =/usr -- Mandir =/usr/share/man -- infodir =/usr/share/info -- enable-shared -- enable-threads = POSIX -- enable-checking = release -- With-system-zlib -- enable-_ cxa_atexit -- disable-libunwind-exceptions -- enable-libgcj-Multifile
-- Enable-languages ages = C, C ++, objc, obj-C ++, Java, Fortran, ADA -- enable-Java-AWT = GTK -- disable-DSSI -- enable-plugin -- With-Java-home =/usr/lib/JVM/java-1.4.2-gcj-1.4.2.0/JRE -- With-CPU = generic -- Host = x86_64-redhat-linux
Thread model: POSIX
GCC version 4.1.2 20080704 (Red Hat 4.1.2-48)

If C ++ is not installed or is not supported, install the GNU make and GNU binutils packages. For more information, see gcc.gnu.org/install.

First, let's implement a simple algorithm:

From 0 to 9 randomly disrupt the array and Output

Shuffle. cpp

# Include <iostream>
# Include <vector>
# Include <algorithm>
# Include <iterator>
# Include "utils. H"

Using namespace STD;
Int main (){
Vector <int> V;
Back_insert_iterator <STD: vector <int> P = back_inserter (v );
For (INT I = 0; I <10; ++ I ){
* P = I;
}
Printcontainer (v, true );
Random_shuffle (V. Begin (), V. End ());
Printcontainer (v, true );
}

Utils. h

# Include <iostream>
# Include <string>
# Include <algorithm>
# Include <iterator>
# Include <vector>

Using namespace STD;

Template <typename FWD>
Void printrange (FWD first, FWD last, char delim = ',', ostream & out = cout ){
Out <"{";
While (first! = Last ){
Out <* first;
If (++ first! = Last)
Out <delim <'';
}
Out <"}" <Endl;
}

Template <typename C>
Void printcontainer (const C & C, char delim = ',', ostream & out = cout ){
Printrange (C. Begin (), C. End (), delim, out );
}

Compile:

G ++-O shuffle. cpp

Run:

./Shuffle

Output result:

{0 1 2 3 4 5 6 7 8 9}
{4 3 7 8 0 5 2 1 6 9}

Now, let's get started and try again later!

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.