C + + practical syntax

Source: Internet
Author: User

String-to-char array conversion:

String str ("Please split this sentence into tokens");
char * CStr = new char [Str.length () +1];
strcpy (CStr, Str.c_str());

Quick Sort Qsort function

Need to include <stdlib.h> qsort

Features: Sorting using the quick sort routines

Usage: void qsort (void *base, int nelem, int width, int (*fcmp) (const void *,const void *));

Each parameter

1 array first address to sort

2 number of elements to be sorted in the array

3 space size of each element

4 pointers to functions

First, sort an array of type int

int cmp (const void *a, const void *b) {return * (int *) A-* (int *) b;} Qsort (Num,100,sizeof (num[0]), CMP);

Second, sort the array of char types (same as int type)

int cmp (const void *a, const void *b) {return * (char *) A-* (int *) b;} Qsort (Word,100,sizeof (word[0]), CMP);

Third, sort the array of double types (especially note)

int cmp (const void *a, const void *b) {return * (double *) a > * (double *) b? 1:-1;} Qsort (In,100,sizeof (in[0]), CMP);

Iv. Array of strings

int Compare_c (const void *a,const void *b) {return strcmp ((char*) A, (char*) b);} Qsort (F,n,16,compare_c);

C + + practical syntax

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.