Hello, C + + (11) How do I use a string data type to represent a string of text? Automatically infer the Auto keyword of the data type based on the initial value (c + + 11)

Source: Internet
Author: User
Tags mul


3.5.2 String Type


We can represent a single character using a variable of type char, so how do we represent a string that has more than one character? We notice that a string is formed by a string that is linked together. Naturally, the simplest and most straightforward approach is to use arrays, a form of data organization and management, that organizes multiple data elements of the same type into a sequence of data for easy access. For more information, refer to section 3.6 for an introduction to the array) to hold the individual characters in a string, and finally to use a special character, '% ', to represent the end of the string to concatenate character data of multiple char types into a string. For example:

// Define a character array to save the string
char msg [32];
// Save each character to the corresponding position of the array in sequence
msg [0] = ‘G’; // The first character, saved to the first position of the array, and so on after
msg [1] = ‘o‘;
msg [2] = ‘o‘;
msg [3] = ‘d’;
// Save a ‘\ 0’ in the last position to indicate the end of the string
msg [4] = ‘\ 0’;
// output the string in the msg array
cout << msg << endl;
Although character strings are simple and feasible, they have many inconveniences. In C ++, we use STL (Standard Template Library, standard template library, is a set of function library using template technology, which provides some commonly used containers and algorithms to facilitate the processing of data. The characters The string type string is a data type defined by it to represent a string. Regarding STL, we will introduce it in detail later in Chapter 8) to represent a string. This type of string is essentially a wrapper for a char type character array. While storing string data, it also adds some common operations on strings, such as obtaining the length of the string, looking for specific characters, etc. Handling is much more convenient. Just like char and wchar_t correspond to each other to deal with different ranges of characters, corresponding to string, C ++ also provides a wstring string type that can handle wchar_t type characters. E.g:

#include <iostream>
#include <string> // Header file where string type is located

using namespace std;

int main ()
{
    // Define a variable of type string, which represents an English string
    string strEn = "Good Morning!";
    // Define a wstring type variable to represent a Chinese string
    wstring strChs = L "Shaan A-82103";
    // Use cout to output string type string
    // Use wcout to output wstring type string
    cout << strEn;

    wcout.imbue (locale ("chs")); // Set the locale

    wcout << strChs << endl;

    return 0;
}
As you can see, because string and wstring are encoded differently, different strings need to be output in different ways. For string variables of type wstring, you need to use the wcout object when outputting, and you need to use the imbue () function to set the character encoding method. In addition, it is worth pointing out here that English strings can be expressed not only in string type but also in wstring type, while Chinese strings can only be expressed in wstring type.

The string type not only wraps a character array, it can store each character in the string, but also provides many operations related to the string, for example, you can get the length of a string, or find a character in the string , Which greatly facilitates the processing of character strings. E.g:

// Define a string variable to save the user name entered by the user
string strName = "";

cout << "Please enter the user name:" << endl;
// Get the string entered by the user and save it to the strName variable
cin >> strName;
// Get the length of strName through the length () function of string type
// and determine whether its length is less than 6 (whether it is less than 6 characters in the string)
if (strName.length () <6)
{
    // If less than, an error message
    cout << "Error: Username contains at least 6 characters" << endl;
}
In the above code, we first define a string type variable strName to save the user name string entered by the user, and then use cin to obtain the user input string and save it to the strName variable. Then use the string type length () function to obtain the length of the string, which is the number of characters in strName. Finally, use the if conditional structure to compare it with the string length 6 we require. If it does not meet the conditions, an error message will be displayed.

Know more: auto type variables-infer the true data type based on the initial value

In the previous chapter, we introduced a variety of data types in C ++: there are int types that represent integers and float types that represent floating-point numbers; there are char types that represent single characters, and string types that represent strings, which have different meanings. Different data types provide us with a wealth of choices for defining variables to represent data in the real world. However, these data types have a common requirement in use. That is, when defining variables to represent data, we must first know what type of data we want to represent, whether it is a decimal or a string of characters, and then we can accordingly Decide whether to use float or string. However, in development practice, sometimes we can't easily determine the data type a variable should have. For example, when assigning a complex expression as an initial value to a newly defined variable, it is often difficult to determine the data type of the expression, and thus cannot determine the data type that the variable should have. In order to solve this problem, C ++ 11 provides us with the auto keyword, using it as the data type defined by a variable, the compiler will automatically infer the reasonable data type of this variable based on the initial value of this variable without us Man-made designation. E.g:

auto x = 7; // Use the integer 7 to initialize the variable x, x is inferred to be of type int
auto y = 1.982; // Initialize variable y using floating point number 1.982, y is inferred to be of type double

Handler GetHandler ();
// Use the return value of the GetHandler () function to initialize the variable handler
// handler is inferred to be of type Handler
auto handler = GetHandler ();
Here, when we define the variable x, we did not specify its specific data type, but use auto as a substitute. In this way, when the compiler compiles this code, it will automatically infer that the actual data type of x is int based on the initial value of 7. In the same way, the variable y initialized with the floating point number 1.982 will be automatically inferred by the compiler as type double; and the last variable handler will be initialized as the return value type Handler of GetHandler () function. Although the auto keyword will automatically infer the data type of the variable based on the initial value, its use does not require additional compilation time. There are benefits and no additional costs. The auto keyword is like a free big gift in the mall. Who doesn't like this big cheap?

In fact, you can think of the auto keyword as a placeholder for a data type in a variable definition, which occupies the place where it should be a specific data type. When compiling, the compiler will infer the specific data type that the variable should have based on the initial value of the variable, and then replace the auto keyword, it becomes a common variable definition with a specific data type. The form of defining a variable with the auto keyword is no different from the general form of defining a variable. The only difference is that when using the auto keyword to define a variable, the variable must have an initial value:

auto variable name = initial value expression; // assignment form
// or
auto variable name {initial value expression}; // initialization list form
In this way, the data type of the result of this initial value expression calculation will be inferred by the compiler as the data type of the variable.

Usually when defining a variable, if it is difficult to accurately infer its data type, or the data type of this variable is difficult to write, you can use auto as the variable data type to define the variable, and the real data type is left to the compiler The device infers from the initial value of the variable. To do this kind of hard work, the computer is much faster than the human brain. This not only saves us the trouble of inferring the data type ourselves, avoids possible human errors, but also achieves the purpose of simplifying the code. E.g:

template <typename T>
// The "&" symbol after the data type vector <T> indicates that the variable defined thereafter is a reference
// Reference is a special way to access data in C ++, we will introduce it in detail in the later section 7.1
void printall (const vector <T> & v)
{
    // Automatically infer the data type of variable it based on the return value type of v.begin ()
    for (auto it = v.begin (); it! = v.end (); ++ it)
        cout << * it << endl;
}
In order to express the same meaning, without the help of the auto keyword, we have to write the following cumbersome form:

template <typename T>
void printall (const vector <T> & v)
{
    for (typename vector <T> :: const_iterator it = v.begin ();
        it! = v.end (); ++ it)
        cout << * it << endl;
}
In addition to simplifying the code, the auto keyword can sometimes even help us complete tasks that were impossible before C ++ 11, becoming a necessity. For example, in the template function, when the data type of a variable depends on the template parameter, if you do not use the auto keyword, you will not be able to determine the data type of the variable at all, because we cannot predict in advance what data type the user uses as the template parameter To call this template function so that the data type of this variable cannot be determined. But after using the auto keyword, all problems will be solved. E.g:

template <typename T, typename U>
void mul (const T & t, const U & u)
{
    // ...
    // Use the auto keyword as the data type, the compiler will use the actual data types of u and t,
    // Automatically infer the data type of the variable tmp
    auto tmp = t * u;

    // ...
}
Here, the data type of the variable tmp should be the same as the data type of the result of multiplying the template parameters T and U, that is, the data type dependent on T and U. For programmers, when writing this template function, the types of template parameters T and U have not been determined, so the type of variable tmp cannot be determined. Therefore, we use the auto keyword as a placeholder to occupy the position of the data type, and the real data type is left to the compiler at the time of final compilation, inferred according to the specific type of template parameters T and U given. In this way, a thing that was originally impossible becomes possible.

Using the auto keyword, you can automatically infer its data type based on the initial value of the variable, which greatly facilitates the definition of complex data type variables. However, this method is good or bad, but it has a disadvantage that the data type inferred each time can only be used once when the variable is defined, and cannot be retained for continued use. It is easy to infer that the data type obtained can only be used once, which seems a little low-carbon and environmentally friendly. Sometimes, we also need the data from this inference to be retained, so that it can be reused to define multiple variables of the same type. To make up for this shortcoming, C ++ 11 also provides a decltype keyword. Its usage syntax is as follows:

typedef decltype (expression) user data type;
Among them, decltype (expression) is the inferred data type (declared type) of this expression, that is, the data type of the calculation result of this expression. Typedef defines this data type as a user-defined data type. In other words, it takes a name for this inferred data type, so that it can be used as a new data type to define variables and create objects. Wait for any place that requires data type. For example, we can rewrite the above example with the decltype keyword:

template <typename T, typename U>
void mul (const T & t, const U & u)
{
    // ...
    // Use decltype to get the data type of t * u,
    // And use the typedef keyword to define it as a new data type M
    typedef decltype (t * u) M;
    // Use this new data type M to define pointer variables (variables representing variables or function addresses) and create objects of type M
    M * tmp = nullptr;
    tmp = new M; // ...
}
The functions of auto and decltype are somewhat similar, and both can infer the specific data type of an expression. However, the use of the two is slightly different. If we just want to determine the appropriate data type for a variable based on the initial value, then auto is the best candidate. And only when we need to infer the data type of an expression and treat it as a new We only really need to use decltype when data types are reused (for example, defining multiple variables of the same type) or used alone (for example, as the return type of a function).

 

Original address: http://www.cnblogs.com/nihaoCPP/p/3989736.html

(Reprinted) Hello, how does C ++ (11) use string data type to represent a string of text? The auto keyword that automatically infers the data type based on the initial value (C ++ 11)

 

Related 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.