Std: string, pants fart stdstring

Source: Internet
Author: User

Std: string, pants fart stdstring

A daily struggle with c # Forces c ++ programmers to change their previous code and find the following lines.


Char szPath [MAX_PATH] = {0 };

GetModuleFileNameA (NULL, szPath, sizeof (szPath ));


Std: string strPath = szPath;

Std: string strDir = strPath. substr (0, strPath. find_last_of ('\\'));


I feel uncomfortable. Why are there char arrays and strings? Is it better to use strings.

The following code is changed:


Std: strPath;

StrPath. reserve (MAX_PATH );

GetModuleFileNameA (NULL, & strPath [0], MAX_PATH );

Std: string strDir = strPath. substr (0, strPath. find_last_of ('\\'));


Okay, I have finished modifying. The Nima function has changed... you know .......

StrDir is empty.


For some reason, although there is content in strPath, the API will not assign a value to the size of string.

If you want to use string to replace the char array correctly, you can only use the following method.


Std: strPath;

StrPath. resize (MAX_PATH, 0 );

GetModuleFileNameA (NULL, & strPath [0], strPath. size ());

StrPath. asign (strPath. c_str (); // assign a value again.


This... isn't it because of dragging your pants?










Zookeeper
Std: string

Substr (n) starts from the nth character to the last
Substr (m, n) from m to n
If the endpoint does not contain the endpoint, check msdn again. I remember it was closed before and after it was enabled.

C ++ question std: What does string & strOut mean?

Std is a system namespace (for more information about namespaces, see baike.baidu.com/view/159924.htm? Fr = ala0_1), string is a variable type, which is included in the std namespace. If you declare a header file with using namespace std; in later programs, you do not need to write the std: statement like this. As for & strOut, strOut is a variable name, and adding & is a reference, I don't know if you know what to cite. For example,
Int a = 1
Int & B =;
B can be understood as the outer number of a, occupying the same memory space, cout <a <B; the structure is 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.