C + + program Learning (ii)

Source: Internet
Author: User

1.c++ Data Type customization:

#include <iostream> int main () {typedef char NAME[20]; Name A,b;std::cout<<typeid (a). Name () <<typeid (b). Name (); Std::cin.get (); return 0;}

typeID (variable name). Method name = = Data type of the output variable

Another way to define this:

#include <iostream> int main () {typedef struct SU{CHAR name[10];int age;};                                Su n;std::cout <<typeid (n). Name () <<std::endl;std::cin.get (); return 0;}

2. Defining bit fields

#include <iostream> int main () {struct bs{unsigned a:1;unsigned b:3;unsigned c:4;} bit,*pbit;//defines the bit and pointer to bit pbitbit.a=1;bit.b=7;bit.c=15;printf ("%d,%d,%d\n", bit.a,bit.b,bit.c);p bit=&bit; pbit->a=0;pbit->b&=3;pbit->c|=1;printf ("%d,%d,%d\n", pbit->a,pbit->b,pbit->c); std:: Cin.get (); return 0;}

Nameless bit fields are not available.

struct K {int a:1 int:2/* The 2-bit cannot use */int b:3 int C:2};

3. Calculate the run time of the program

#include <iostream> #include <ctime> int main () {clock_t start,end;start=clock (); struct bs{unsigned a:1;uns igned b:3;unsigned C:4;} bit,*pbit;bit.a=1;bit.b=7;bit.c=15;printf ("%d,%d,%d\n", bit.a,bit.b,bit.c);p bit=&bit;pbit->a=0;pbit-> b&=3;pbit->c|=1;printf ("%d,%d,%d\n", pbit->a,pbit->b,pbit->c); End=clock ();//std::cout<< ( Double) (End-start)/clocks_per_sec;std::cout <<start;std::cout <<end;std::cin.get (); return 0;}


This article is from the "Wang Nimei Adult Road" blog, reprint please contact the author!

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.