C + + Several common classes introduce the first article

Source: Internet
Author: User



a , Common C++MFC class


1, CString

Website: Http://baike.baidu.com/link?url=PDpqZHn6cNhR4uoOpyxeTT7O6Vv1F15q4kgAomDiFwGP_lnXxUYkZsdSZ_FdThwKs0tLJkb-1ZcQVG_Qg9Wdeq


1) String connection can reflect the CString type of convenience features is the connection of strings, using the CString type, you can easily connect two strings, as the following example:

CString Gray ("gray"); CString Cat ("Cat"); CString Graycat = Gray + cat;//then graycat= "Graycat"

2) string input and output and number formatting

CString S;s.format ("The total was%d"), total); Note here that the advantage of this approach is that you do not have to worry about whether the buffer used to store the formatted data is large enough to be done by the CString class. Formatting is one of the most common techniques for converting data other than string types into CString types, such as converting an integer into a CString type, as follows: CString S;s.format (L ("%d"), total);
3) member functions
>> Constructors
CString (const cstring& STRINGSRC); Copies the contents of an already existing CString object STRINGSRC to the CString object. For example: CString str1 (L (Jizhuomi)); Copy the constant string to str1 CString str2 (STR1);//Copy the contents of STR1 to STR2
CString (LPCTSTR lpch,int nlength); Copies the first nlength characters in a string lpch to the CString object. For example: CString str (L ("Wwwjizhuomi"), 3);//Constructed string object content of "www"
CString (TCHAR ch,int nlength = 1); The CString object constructed with this function will contain nlength repeating ch characters. For example: CString str (L (' W '), 3);//str "WWW"
Case conversion and sequential conversion functions of the >>cstring class
The connection of multiple CString objects can be implemented by overloading operator + and + =. For example: CString str (_t ("Jizhuomi")); STR content is "Jizhuomi" str = _t ("www") + str + _t ("-"); STR is "wwwjizhuomi-" str + = _t ("com"); STR to wwwjizhuomi-com
Comparison of >>cstring objects
CString objects can be compared by = =,!    =, <;, >;, <=, >=, and so on, can also be implemented using compare and CompareNoCase member functions.    int Compare (PCXSTR psz) const;    Compares the CString object to the Psz string, returns 0 if it is equal, or less than 0 if it is less than psz, or a value greater than 0 if it is greater than psz.    int CompareNoCase (PCXSTR psz) const throw ();    This function is similar to the Compare function, except that it is case insensitive.    For example: CString str1 = _t ("Jizhuomi");    CString str2 = _t ("Jizhuomi");    if (str1 = = str2) {//Because STR1, str2 are not equal, do not execute the following code ...} if (0 = = Str1.comparenocase (str2)) {//Because the CompareNoCase function returns 0 when the case is not case-sensitive, execute the following code ...}
>>cstring Object String Extraction operation
CString Left (int ncount) const;    Extracts a substring of ncount characters to the left of the string and returns a CString object that contains a copy of the substring.    CString Right (int ncount) const;    Extracts a substring of the ncount character to the right of the string and returns a CString object that contains a copy of the substring.    CString Mid (int ifirst,int ncount) const;    Extracts a substring of ncount characters starting at the index Ifirst position in the string, and returns a CString object that contains a copy of the substring.    CString Mid (int ifirst) const;    Extracts the substring in the string that starts at the index ifirst position until the end of the string, and returns a CString object that contains a copy of the substring.    For example: CString str1 = _t ("Jizhuomi"); CString str2 = str1. Left⑶;//STR2 is "jiz" str2 = str1. Right⑵;//str2 for "mi" str2 = str1. Mid (1,3);//STR2 is "Izh" str2 = str1. Mid⑸;//STR2 as "OMI"
Find operations for >>cstring object strings
int Find (PCXSTR pszsub,int istart=0) const throw ();    int Find (Xchar ch,int istart=0) const throw ();    Starts at the Istart index position of the CString object string, finds the position of the substring pszsub or the first occurrence of the character ch, and returns 1 if not found.    int findoneof (PCXSTR pszcharset) const throw ();    Finds any character in the pszCharSet string, returns the first occurrence of the position, and returns 1 if it is not found.    int Reversefind (Xchar ch) const throw (); Starts from the end of the string to find the specified character ch, returns its position, and returns 1 if it is not found.    It is important to note that the index of the position is to be counted from the beginning, although it is looked up from behind.    CString str = _t ("Jizhuomi"); int nIndex1 = str. Find (_t ("zh")); The value of NINDEX1 is 2 int nIndex2 = str. Findoneof (_t ("MUI")); The value of NINDEX2 is 1 int nIndex3 = str. Reversefind (_t (' I ')); The value of NINDEX3 is 7
Substitution and deletion of >>cstring class object strings
int Replace (PCXSTR pszold,pcxstr psznew);    Replaces the substring pszold in the CString object with the string psznew, returning the number of characters replaced.    int Replace (Xchar chold,xchar chnew);    Replaces the character Chold in the CString object with the character chnew, returning the number of characters replaced.    int delete (int iindex,int ncount = 1), removes the ncount character from the IIndex position at the beginning of the string, and returns the length of the string after the delete operation.    int Remove (Xchar chremove);    Removes all characters specified by Chremove in the string, returning the number of characters deleted.    For example: CString str = _t ("Jizhuomi"); int n1 = str. Replace (_t (' I '), _t (' J ')); STR is "JJZHUOMJ" and N1 is 2 int n2 = str. Delete,//STR is "JHUOMJ", N2 is 6 int n3 = str. Remove (_t (' J ')); STR is "Ihuom" and N3 is 1
Format string method for the >>cstring class
Use the format member function of the CString class to format data types such as int, short, long, float, and double as string objects.    void __cdecl Format (pcxstr pszfo rmat,[,argument] ...); The parameter pszformat is the format control string, the parameter argument is optional, for the data to be formatted, generally each argument in Pszformat has corresponding sub-string representing its type, the argument of int should be "%d",    Float type response should be "%f", and so on.    For example: CString str;    int a = 1;    float B = 2.3f; Str. Format (_t ("a=%d,b=%f"), A, b); STR is "a=1,b=2.300000
>> Convert a string of type char* to CString type char* to CString you have a char* type of data, or a string. How do I create a CString object? Here are some examples: char * p = "This is a test", or more Unicode-aware as follows: TCHAR * p = _t ("This is a test") or LPTSTR p = _t ("This is a test") You can use any of the following: CString s = "This is a test"; 8-bit onlycstring s = _t ("This is a Test"); Unicode-awarecstring S ("This is a Test"); 8-bit onlycstring S (_t ("This is a Test"); unicode-awarecstring s = p; CString s (p); These methods make it easy to convert a constant string or pointer to CString. It is important to note that the assignment of a character is always copied to the CString object, so you can do the following: TCHAR * p = _t ("Gray"); CString s (p);p = _t ("Cat"); s + = p; The result string is definitely "Graycat".

2, Cpect
Website: http://baike.baidu.com/link?url=_UdTnW20X1jLgzUhOxrFyhiWUGh8xA17mZXpZXQJDSF38EAb2M5ZhpT_ XO7EQYQSVLUNWUQB9MVZMQ3CDRXW__ >>crect member functions
Width calculation CRect The height of the crect calculates the size of the CRect TopLeft returns the CRect's upper-left corner BottomRight returns CRect to the lower-right corner CenterPoint Returns the center point of the CRect Isrectempty determines whether the CRect is empty. If the width and/or height of the CRect is 0, then it is empty isrectnull determines CRect top,bottom,left, and right is equal to 0PtInRect to determine whether the specified point is within CRect SetRect Set the size of the CRect Setrectempty set CRect to an empty rectangle (all coordinates equal to 0) Copyrect copies the dimensions of a source rectangle to Crectequalrect to determine if CRect is equal to the given rectangle Inflaterect Increase the width and height of the CRect deflaterect reduce the width and height of the CRect normalizerect make the height and width of the CRect return the specification Offsetrect move CRect to the specified offset subtractrect Subtract a rectangle from a rectangular area intersectrect set CRect equals two rectangle intersection unionrect set CRect equals two rectangle of the set >>crect operator
Operator Lpcrect converts a crect to a lpcrectoperator LPRECT converts a crect to a lprectoperator = copies the dimensions of a rectangle to Crectoperator = = Determines whether the CRect is equal to a rectangle operator! = Determines whether CRect is not equal to another rectangle operator + = causes CRect to increment the specified offset, or causes CRect to enlarge operator-= Subtracts the specified offset from the CRect, or zoom out crectoperator &= settings CRect equals CRect and a rectangle of intersection operator |= settings CRect equals CRect and a rectangle and operator + increases the given offset to CRect, and returns the resulting CRect object operator-minus the given offset from the CRect, and returns the resulting CRect object operator & creates a crect with a rectangle and returns the CRect object operator | Creates a crect with a rectangle and returns the resulting CRect object
Default coordinate system: The origin is the upper-left corner, the right side is the x-axis positive direction, and the bottom is the y-axis positive direction.
There are many examples, which are no longer described in detail
3, CPen
Website: http://baike.baidu.com/link?url=SsNEoaWAy_e_ 4nilgrx95arjjurrnw6o8pitk9dz13xuvuxlevje7zy1ktogummtjv7xcailyxumz_n7e5f6r_#3
Example: CPen pen1; CPen *oldpen;pen1. CreatePen (Ps_solid,2,rgb (192,192,192));//Create Object Oldpen = Pdc->selectobject (&AMP;PEN1);//Select the brush into the device description table and save the old brush
4, CImage
>> Save picture Save picture There is only one function, which is the Save function, which has two parameters, one is the full path and file name of the file, and the other is the file extension. However, if only save and display the image function, CImage's save function does not play a big role, but is a dump file tool. The great thing about CImage is that you can change the picture and save it. The function that modifies a picture has so 3 function can modify picture, all is pixel level operation. These three functions are setpixel,setpixelrgb,setpixelindexed respectively. The screen can be scanned by a double loop, and the pixel color values are saved to the CImage object to achieve the screenshot. We can use a very simple code to implement a DC (device context) drawn graphics into the image file. The basic idea is to initialize a CImage object, and then set the size and number of digits for it, that is, the image size and the total number of colors in the picture, and then use the device context to draw. After drawing the drawing and then establishing another device context object, called the destination DC, which is called the source DC of the previously drawn DC, the destination DC must be associated with the CImage object, and then the contents of the drawing DC are copied to the destination DC using the BitBlt function and other functions. Finally, the CImage object is saved to a file, the following is the code implementation (VS2010 supports the Chinese variable name): CRect rect;this->getclientrect (rect); CImage Image;image. Create (rect. Width (), Rect. Height (), 24); cdc* dcpoint=this->getdc ();D cpoint->ellipse (20,20,200,200); CDC Dstdc;dstdc.createcompatibledc (dcpoint);d stdc.selectobject (image);d Stdc.bitblt (0,0,rect. Width (), Rect. Height (), dcpoint,0,0,srccopy); image. Save (L "d:\\ user directory \\Pictures\\ write picture file. jpg"); >> Show Pictures
First look at the following code CImage image;image. Load (L "d:\\ user directory \\Pictures\\example.jpg"); image. Draw (GetDC ()->m_hdc,crect (0,0,320,240)); The CImage class supports reading a file on a local disk with the Load method and displaying the picture using the Draw method. Many of the member functions of the CIAMGE class can be implemented to display pictures. such as the BITBLT approach. As in the following code: image. BitBlt (GetDC ()->m_hdc,0,0,320,240,0,0,srccopy); The BitBlt method, like the BitBlt function of the CDC class, is 1:1 copies the image and displays it on the screen, supporting the inverse of the source image, which is the inverse color display. This function is implemented by the last parameter of the BitBlt, for example, the parameter is notsrccopy is the inverse color display. MaskBlt can perform special raster manipulation and mask processing for images and destination images. The PLGBLT function can stretch a picture into parallelogram for display. The StretchBlt function allows you to stretch the picture to display. >> Create a picture
CImage's creat functions and Createx functions are used to create blank images, which programmers can use to create images and add code that allows the user to change the pixel color of the CImage object using the mouse to achieve similar functionality as Windows Paint. Pdc->moveto (10,10);//define the starting point Pdc->lineto (200,20);//The end of the line Pdc->selectobject (oldpen);//Select the previous brush into the device description table
5, CArry
The CArray class supports arrays similar to c arrays, but can be dynamically compressed and extended if necessary. The array index starts at 0. You can decide whether to fix the upper bounds of an array or to extend the current bounds when you add elements. The upper bound of the memory pair is continuously allocated space, even some elements can be empty. Before using an array, use SetSize to establish its size and allocate memory for it. If you do not use setsize, adding elements to an array can cause frequent redistribution and copying. Frequent redistribution and copying is not only inefficient, but also leads to memory fragmentation.
MFC provides a set of template libraries to implement some of the more common data structures such as ARRAY,LIST,MAP. CArray is one of the functions used to implement dynamic arrays. CArray is derived from CObject, has two template arguments, the first parameter is the variable type of the CArray class array element, and the latter is the parameter type when the function is called. There is a class object, to define a dynamic array of object, then you can use the following two methods:carray<object,object> Var1; Carray<object,object&> Var2; The efficiency of the VAR2 is high.





C + + Several common classes introduce the first 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.