C and C + + code pristine NOTE 1

Source: Internet
Author: User
Tags setf

CH1 better C.

    1. Operator overloading specifies that there must be a custom type for the user
    2. Some functions that the output does not notice:

      float x = 123.456, y = 12345;

      Cout.precision (2); Show two valid values, others are scientific notation

      cout << x << "" << y << Endl;

      ?

      COUT.SETF (ios::showpoint);//Display the end of the 0

      cout << x << "" << y << Endl;

      ?

      COUT.SETF (Ios::showpos);//Display symbol

      cout << x << "" << y << Endl;

    3. Print Address

      Printf ("%p", s);

      cout<< "Address:" <<&s;

?

CH2 pointer

    1. Distinguish the position of the const

      Const Char *p; Note that *p is constant and cannot be changed.

      char* const P; Note that P is constant and cannot be changed.

Depending on the position of the *.

  1. Initialize two-dimensional pointers

    int **a;

    A = new int*[3];//line

    ???? for (int i = 0; i < 4; ++i)

    ???? {

    ???????? A[i] = new int[4];//column

    ????}

    Delete[] A;

    ?

    ?

    ???? int A[][4] = {{1,2,3},{4,5,6},{7,8,9}};

    ???? int (*p) [4] = A;

    ?

    ???? P[i] = = * (p+i);

    ???? P[I][J] = = * (p[i]+j) = = * (* (p+i) +j);

    ?

    ?

    ???? size_t totals = sizeof (a);//entire number of bytes

    ???? size_t totalcols = sizeof (a[0]);//The entire column of bytes

    ???? size_t rows = sizeof (a)/sizeof (a[0]);

    ???? size_t cols = sizeof (a[0])/sizeof (a[0][0]);

    ???? for (int i = 0; i < rows; ++i)

    ???? {

    ???????? for (int j = 0; j < cols; ++j)

    ???????????? cout << P[i][j] << "";

    ???????? cout <<endl;

    ????}

    ???? cout << rows << "<< cols << Endl;

  2. function pointers

    Pointers to non-member functions:

    #include <Windows.h>

    #include <iostream>

    ?

    using namespace Std;

    ?

    Int (*FP) (int,int);//Global pointer variable

    void (*farray[]) (void); Can be defined outside the main function or internally. Array of function pointers

    int Max (int a, int b)

    {

    ???? if (a > B)

    ???????? return A;

    ???? Else

    ???? return b;

    }

    int main ()

    {

    ???? fp = Max;

    ???? int a = FP (UP);

    ???? cout << a << Endl;

    return 0;

    }

    ?

    Pointers to member functions:

    #include <Windows.h>

    #include <iostream>

    ?

    using namespace Std;

    ?

    Class C

    {

    Public

    ???? void F () {cout << "c::f \ n";};

    ???? void G () {cout << "c::g \ n";};

    ???? int Max (int a, int b)

    {

    ???? if (a > B)

    ???????? return A;

    ???? Else

    ???? return b;

    }

    };

    ?

    int main ()

    {

    c C;

    void (C::* pmf) () = &C::f; Defines a pointer to a member function

    (C.*PMF) ();//A pointer to a member function that is slightly different from the pointer of a non-member function and has more objects. *

    PMF = &C::g;

    (C.*PMF) ();

    ?

    Int (C::* fp) (int,int) = &C::Max;

    int aa = (C.*FP) (3,4);

    cout << AA << Endl;

    return 0;

    }

Array of pointers to member functions

  1. #include <Windows.h>
  2. #include <iostream>
  3. ?
  4. using namespace std;
  5. ?
  6. class Object
  7. {
  8. Public:
  9. ??? void Retreve () {cout << "object::retreve";}
  10. ??? void Insert () {cout << "object::insert";}
  11. ??? void Update () {cout << "object::update";}
  12. ??? void process (int choice);
  13. Private:
  14. ??? typedef void(Object::* OMF) (); //From the alias up good
  15. ??? Static OMF Farray[3]; //member function pointer array
  16. ?
  17. };
  18. OBJECT::OMF Object::farray[3] = {
  19. ?? &object::insert,
  20. ?? &object::retreve,
  21. ?? &object::update
  22. };
  23. void Object::p rocess (int choice)
  24. {
  25. ??? if (Choice >=0 && choice <=2)
  26. ??? {
  27. ?????? (this->*farray[choice]) ();
  28. ?????? cout << Endl;
  29. ???}
  30. }
  31. int Main ()
  32. {
  33. ??? Object o;
  34. ??? for (;;)
  35. ??? {
  36. ?????? int Choice;
  37. ?????? CIN >> Choice;
  38. ?????? if (Choice >=0 && choice<=2)
  39. ????????? O.process (choice);
  40. ?????? Else
  41. ?????????? break;
  42. ???}
  43. ??? return 0;
  44. }
    1. Encapsulation vs. Incomplete types

      The implementation of a class similar to the source class is to redefine a class similar to the source class and then repack it.

?

CH3 Preprocessor

    1. Debug mode can run under release under Run, with macro definition implementation

      #define DEBUG 1

      ?

      int main ()

      {

      int i = 2;

      #if DEBUG

      cout << "Debugmod" << Endl;

      #endif

      return 0;

      }

      ?

      The following sentence can also achieve the above function

      ?

#ifdef _DEBUG

cout << "Debugmod" << Endl;

#endif //_DEBUG

?

?

To mix the new C + + code with the old C code, you need to add the following statement

extern "C" void f (); F () is compiled in C environment

    1. Character

      Enter \ r newline \ \b Warning \a

      ?

      ?

      ?

One of the CH4 C standard libraries: for qualified programmers

1. <ctype.h>

Common functions: Is series. Eg:isupper (); Islower (); Isspcace () and so on.

char a = ' a ';

int B = Isalpha (a);

Instance:

  1. #include <Windows.h>
  2. #include <iostream>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. using namespace std;
  8. ?
  9. long Atox (char *s)
  10. {
  11. ??? while (Isspace (*s)) //Kill the opening space, as long as there is a nonzero number that jumps out
  12. ?????? s++;
  13. ??? long sum;
  14. ??? for (sum = 0L; Isxdigit (*s); ++s)
  15. ??? {
  16. ?????? int Digit;
  17. ?????? if (IsDigit (*s))
  18. ????????? digit = *s-' 0 ';
  19. ?????? Else
  20. ????????? digit = ToUpper (*s)-' A ' + 10;
  21. ?????? sum = sum*16l + digit;
  22. ???}
  23. ??? return sum;
  24. }
  25. ?
  26. long Atox (char*s)
  27. {
  28. ??? char xdigs[] = {0123456789ABCDEF};
  29. ??? long sum;
  30. ??? while (Isspace (*s))
  31. ?????? s++;
  32. ??? for (sum = 0; Isxdigit (*s); ++s)
  33. ??? {
  34. ?????? int digit = STRCHR (Xdigs,toupper (*s))-xdigs; //Find the position of the pointer, that is, the position of the number (0-15 values) to find the position of the first occurrence of the string (*s) in Xdigs.
  35. ?????? sum = sum*16l + digit;
  36. ???}
  37. }
  38. long Atox (char *s)
  39. {
  40. ??? long n = 0;
    1. ??? SSCANF (S, "%lx", &n); Reference code: http://www.91linux.com/html/article/program/cpp/20081130/14121.html
  41. ??? return N;
  42. }
  43. int Main ()
  44. {
  45. ??? Char* s = "123 abc";
  46. ??? //Method one
  47. ??? long t = Atox (s);
  48. ??? //Method two
  49. ??? Char* * ss = NULL;
  50. ??? long tt = Strtol (s,ss,16);
  51. ??? cout << tt << Endl;
  52. ??? return 0;
  53. }

?

C and C + + code pristine NOTE 1

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.