首页 > 代码库 > [C++] String Basic

[C++] String Basic

Namespace Declarations

A using declaration let us use a name from a namespace without qualify the name with a namespace_name:: prefix.

//  using namespace::name    using std::vector    using std::string 

A sperate using declaration is required for each name.

Header should not include using declaration.

 

String

Initialize string

    string s1;          // default initialization; s1 is the empty string    string s2 = s1;    string s3 = "hello";    string s4(10, c);  // s4 is cccccccccccccc            

When we initialize a variable using =, we are asking the comiler to copy initialize the object by copying the initilizer on the right-hand side into the object being created. Otherwise, when we omit the =, we use direct initialization.

    string s4 = string(10, c);    // copy initialization; lower efficiency

It is the same as:

    string tmp(10, c);    // tmp is cccccccccc    string s4 = tmp;      // copy tmp to s4

 

Reading and Writing string

int main(){    string 1, s2;    cin >> s1 >> s2;    cout << s1 << s2 << endl;}

The string input operator reads and discards any leading whitespace(e.g. spaces, newline, tabs). It then reads characters until the next whitespace character is encountered. So, if the input to the program is "Hello World!", the output will be "HelloWorld!".

 

Like the input operator, getline() return its istream argument. As a result, we can use getline() as a condiction just as we use the input operatocr as a condiction.

    string line;    while (getline(cin, line){        cout << line << endl;    }

The newline that causes getline to return is discarded; the newline is not stored in the sting. Because line don‘t contain a newline, we must write our won. As usually, we use endl as a newline, to end the current line and flush the buffer.

 

Because string::size() return an unsigned_type, it is essentual to remember that expression that mis signed and unsigned data can have surpring results. 

For example, if n is an int that holds a negative, then s.size() < n will almost surely evaluate as true. It yield true because the negative value in n will convert to a large unsigned value.

You can avoid problems due to conversion between unsigned and int by not using ints in expressions that use size().

 

Assignment for string

In general, the library type strives to make it as easy to use library type as it is to use a built-in type. We can assigne one string object to another.

    string s1(10, c), s2;    // s2 is empty string    s1 = s2;              // replace the content of s1 with a copy of s2 -- empty string

The string library let us convert both character literals and string literals to strings.

    string s1= "hello", s2 = "world";    string s3 = s1 + , + s2 + "!";

When we mix string and string or character literals, at least one operand to each + operator must be of string type.

    string s6 = s1 + ", " + "world!";    // ok    string s7 = "hello" + ", " + s1;     // error

s6 works in much the same way as when we chain together input or output expressions. s7 is illegal.

For historical reason, and for compatibility with C, string literals are not standard library strings.

 

Advise: Used the C++ version of C library Headers.

In addition to facilities defined specially for C++, the C++ library incorporate the C library. Headers in C have names of the form name .h. The C++ version‘s of these versions header are name c-name - they remove the .h suffix, and precede the name with the letter c.

Hence, cctype has the same content as ctype.h, but in a form that is appropriate for C++ programs.

 

The range-base for to process every character.

    string str("some thing");    for ( auto c: str){        cout << c << endl;    }

On each iteratoion, the next character in str will be copied into c. The changes in c will not impact the content of str.

    for (auto &c: str){        c = toupper(c);    }    cout << c << endls;

Remember that reference is just another name for the giving object. When we use a reference as our control varaible, the variable is bound to each element in the sequence in turn. we can change the character to which the reference is bound.

 

There are two ways to access individual characters in a string: a subscript or an iterator.

The subscript operator(the [] operator) return a reference to the character at the given position. The values we use to subscript a string must be >=0 and < size();

The return of using an index outside the range is undefined.

    if (!s.empty()){        cout << s[0] << endl;    }

Any time we use a subscript, we must ensure the index is in range of the string‘s length.

 

Reference:

C++ Primer, Fifth Edition, 3.2. Library string Type

 

 

[C++] String Basic