Human visual angle C + + string (top)

Source: Internet
Author: User
Tags throw exception

  Long time no Update blog, this period has been busy in the image processing projects, recently empty, it is time to integrate the relevant content of C + +, meditation experience under the charm of programming language, and we discuss learning together. I will be in the form of a header file to learn, and only about the application of the relevant interface, as for the implementation of the internal specific, in view of my limited level, dare not caught dead. After consideration, it is decided to start with the header files related to the data structure, because the contents of these header files are often used in OJ. Today is to learn the string header file, where the string is a C + + string, and the C string is still reserved in C + +, the header file name is CString, here do not expand. Note: The following code is run under the VS 2015 compilation environment.

Some detailsbasic_string

In fact, string header file is not only the type of string, the main thing in this header is a class template called basic_string , the declaration of this class template is as follows:

class charT,           class traits = char_traits<charT>,           class Alloc = allocator<charT>           class basic_string;

The class template construction above is a bit complicated, and we don't have to delve into it, just know that we can customize three types as member type in the Basic_string class. The chart (character type) is the most important template parameter, which directly describes the type of element in the string, and the other two types take default parameters (classes built with the CharT parameter).

Why mention basic_string this thing, because the string type is Basic_string's special type, in the actual operation, the most commonly used is the char type (character type), So simply converting the chart above to char is the string type. The string type is constructed as follows:

typedef basic_string<charstring;      //元素为8bits字符类型

Similarly, in C + + built-in types, there are wstring,u16string , and u32string(the latter two are under the C++11 standard), and we can choose the string type according to the actual need. They are constructed as follows:

typedef basic_string<wchar_t> wstring;      //16位或32位typedef basic_string<char16_t> u16string;   //16位typedef basic_string<char32_t> u32string;   //32位
String versus Character Array

I've seen an article before about string and char*, and I'll make a summary here.

    1. String memory is managed by the system to automatically implement memory requests and releases, while char* needs to be managed by itself. If you are using a large amount of memory, the string system will automatically request memory that may not be available. Therefore, it is recommended to use char* in cases where memory size is known, but you can use string when memory size is not known, but is small.
    2. String compared to char* has one advantage, the C + + standard Library of the string class is encapsulated, the various member functions inside the string is very convenient to handle, which is one of the reasons I used string.

      As for the string and char type of vector what is the difference, I have not delved into, probably the member function is different, know the small partners can leave a message below.

Elaborationmember functionsstring::string
string();//(1) default constructor, constructs an empty string with a string length of 0string(Const string& str);//(2) copy constructorstring(Const string& STR, size_t pos, size_t len = NPOs);//(3) Copy str part, POS is the starting position, Len is the copied string length (the default is to the bottom of the string) here to note the first character of Str pos=0string(Const Char* s);//(4) Copy the character array (string) pointed to by the S pointer in c modestring(Const Char* S, size_t N);//(5) Copy the first n character array elementsstring(size_t N,Charc);//(6) Copy n consecutive characters CTemplate<classInputiterator>string(Inputiterator first, Inputiterator last);//(7) Copy the character sequence using iterator, the range is [First,last], and note that the last one does not includestring(initializer_list<Char> IL);//(8) Convert the initialization list IL to stringstring(string&& str)noexcept;//(9) rvalue refers to STR and does not throw exception information. 

About rvalue Reference and move semantics this piece of content, is a new feature of c++11, I thought for a long time also is foggy, estimate is oneself perception is not high, I think there are two articles written well, posted here. In addition, do not know the right value and temporary variables are different, know the small partners can also leave a message below.

https://www.ibm.com/developerworks/cn/aix/library/1307_lisl_c11/

https://my.oschina.net/letiantian/blog/470921

Demonstration://For the brief, the future code will omit cout, the operation of their own should be added#include <iostream>#include <string> //Call string header file #include <initializer_list> //(8) to use using namespace STD;stringTestConst string& x)//(9) of the test function that produces the right value{returnx;}intMain () {stringS4 ("String Now");//(4)    stringS1;//(1)    stringS2 (S4);//(2)    stringS3A (S4,7,3);//(3) Set length    stringS3B (S4,7);//(3) default length, i.e. to the bottom    stringS5 ("String Now",6);//(5)    stringS6 (3,' 6 ');//(6)    stringS7 (S4.begin (), S4.begin () +6);//(7) After the BEGIN function, it is mentioned that you can generate iteratorinitializer_list<Char> s0 = {' s ',' t ',' R ',' I ',' n ',' G '};//Generate Initializer_list    stringS8 (S0);//(8)    stringS9 (Test (S4));//(9)}//Self-study the sequence number in the note against the above ordinal

The result is as follows, to see if it is the same as you think.

String::operator=
string&operator= (Const string& str);//(1) Assigning a string type variablestring&operator= (Const Char* s);//(2) Assignment C-style stringstring&operator= (Charc);//(3) Assign a character with a string length of 1string&operator= (initializer_list<Char> IL);//(4) Assignment initialization list Ilstring&operator= (string&& str)noexcept;//(5) Assigning values using rvalue references
Demonstration:#include <iostream>#include <string> //Call string header file #include <initializer_list> //(4) to use using namespace STD;stringTestConst string& x)//(5) of the test function that produces the right value{returnx;}intMain () {stringS1, S2, S3, S4, S5;//For separating the operator= and constructors, use the default constructorS2 ="String Now";//(2)S1 = s2;//(1)S3 =' 6 ';//(3)initializer_list<Char> s0 = {' s ',' t ',' R ',' I ',' n ',' G '}; S4 = S0;//(4)S5 = test (S2);//(5)}

Some more Detailsstring type object is end With '/'?

It is estimated that many people like me have thought of this problem, because in the C language, the system default strings are truncated with '. We might as well make a small program to verify it today.

#include <iostream>#include <string>using namespace STD;intMain () {stringS0 ="String Now";//Initialize S0    if(s0[Ten] ==' + ')cout<<"Yes."<< Endl;//If the end is ' + ', the output is yes.    Else cout<<"No."<< Endl;//If the end is not ' + ', the output is no.    return 0;}

Here, we get a satisfying answer, stating that the C + + style string is still the end of ' s ', usually, at the end of ' + ' we can ignore.

The effect of encoding type on string

This is because I have noticed the remark that I have quoted here.

Note that this is class handles bytes independently of the encoding used:if used to handle sequences of multi-byte or Variab Le-length characters (such as UTF-8), all members of the This class (such as length or size), as well as its iterators, would s Till operate in terms of bytes (not actual encoded characters).

The meaning of this sentence is summed up to say that no matter what type of encoding you use, the operation of string type will be handled in a byte -by-bit manner. We can not help but think of Chinese, because the Chinese is encoded in the GB2312, and one of the characters occupy two bytes, which will cause problems? Let's test it.

 #include <iostream>   #include <string>  using  namespace  STD ; int  Main () {string  s0 = " test "; //initialized in Chinese s0  cout  << s0 << endl << Endl; //see if I can output Chinese  for  (int  i = 0 ; i < 2 ; ++i) //output first 2 bytes to see if output test two characters  cout  << s0[ I] << Endl; return  0 ;} 

At first, we output the entire string to see if the word "test" is displayed, and the result is that the two words show that C + + is capable of identifying GB2312 encoding. And when we try to output the string as a single byte, we find the problem, because the output is blank, which means that the string type does not directly tell what the encoding is, it can only be read by a single byte, which causes the byte to have no default ASCII encoding to correspond to, Therefore, there will be no content output.

There is a lot of string content, in order to keep the page streamlined, easy to browse, I will put the next part of the content in the next blog post. If there is any mistake, please correct me!

Human visual angle C + + string (top)

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.