Simple Introduction to C + + string classes and strings access and concatenation operations

Source: Internet
Author: User


C + + enhanced support for strings, in addition to the use of strings in C, and the use of built-in data types string,string class processing strings can go through a lot, replacing the char array and the char pointer in the C language completely.

Use the Sting class needs to include header file < Sting, and here's how to use it.

Several uses of string:

#include <iostream>
#include <string>

using namespace Std;

void Main () {

string S1;
string s2 = "C plus plus";
string s3 = S2;
String S4 (5, ' s ');

cout << s1 << S2 << S3 << S4 << Endl;

System ("pause");


}
Output results:

C Plus PLUSC plus plussssss
Please press any key to continue ...
The preceding lines of code describe several methods that define a string type variable, S1 only defined but not initialized, and the compiler will attach the default value to S1, the default is "" (empty string).

The variable s2 is also initialized to "C plus plus" and is different from the char string in the C language, and the string type does not end with the

' Yes ', the string type is essentially a string class, and the variable we define is an object of one string class, and the variable S3 is initialized directly with S2 when it is defined, so the S3 content is also ' C plus plus '. The variable S4 is initialized to a string consisting of 5 ' s ' strings, which is ' sssss '.

Unlike strings in C, we can call the length () function provided by the string class when we need to know the string lengths. As shown below:

string s = "C plus plus";
int len = S.length ();
cout<<len<<endl;
Results of compilation run:

11

Here, the variable s is also the object of the string class, and length () is its member function. Because the string variable has no ' at the end ' character, length () returns the true length of the string, rather than the length +1.

Convert to char array string

Although C + + provides string classes to replace strings in the form of char arrays in C, it is sometimes necessary to use C-style strings in programming, and for this purpose, the String class provides a converted function c_str (); a function can convert a string variable to a const The form of an array of strings and returns a pointer to the array. Take a look at the following code:

string filename = "Input.txt";
Ifstream in;
In.open (Filename.c_str ());
In order to use the file to open the function open (), you must convert a variable of type string to an array of strings.

#ifndef _iterator_debug_level
#define _ITERATOR_DEBUG_LEVEL 0
#else
#undef _iterator_debug_level
#define _ITERATOR_DEBUG_LEVEL 0
#endif

#include <iostream>
#include <string>

String string->c strings Conversion
void Main ()
{
String-> char*
string S1 = "Far Away";
Const char* C = S1.C_STR ();
printf ("%s\n", c);

//
string s2 = C;

String->char[]
Assign characters from string to char[]
Char arr[50] = {0};
S1.copy (arr,4,0);

cout << arr << Endl;

System ("pause");
}
Compile output results:

Far Away
Far
Please press any key to continue ...
Input and output of string strings

The String class overloads the operator of the input and output, inputs with ">>", and outputs with "<<", see the following code:

#include <iostream>
#include <string>
using namespace Std;
int main () {
string S;
Cin >> S; Input string
cout << s << endl; Output string

System ("pause");
return 0;
}
Run Result:

C Plus
C
Please press any key to continue ...
Although we entered two "C Plus" separated by a space, only one was exported because the operator ">>" By default ignores spaces, and the input ends with a space, so the last plus input is not stored in the variable.

accessing characters in a string

A string string can also access each of these characters as an array of strings by following the subscript. The starting subscript of the string string is still starting at 0. Take a look at the following code:

#include <string>
using namespace Std;

String traversal
void Main () {
string S1 = "ABCDEFG";

1. Array mode

cout << "array mode:" << Endl;
for (int i = 0; i < s1.length (); i++) {
cout <<s1[i] << Endl;

}

2, Iterative mode
cout << "iterative mode:" << Endl;
for (String::iterator it = S1.begin (); it!= s1.end (); it++) {

cout<<*it<< Endl;

}

System ("pause");
}
The output results are:

Array mode:
A
B
C
D
E
F
G
Iteration Mode:
A
B
C
D
E
F
G
Please press any key to continue ...
Concatenation of strings

With the string class, we can use the "+" or "+ +" operators to directly splice strings, very convenient, no longer need to use the C language strcat (), Strcopy (), malloc () and other functions to stitching strings, and no need to worry about the overflow of space enough.

Here's a look:

string concatenation
void Main ()
{
string S1 = "Alan";
String s2 = "Xiho";

1.
String s3 = s1 + s2;

String S4 = "Wahaha";

2.
S3.append (S4);

cout << S3 << Endl;

System ("pause");
}
The output results are:

Alanxiho Wahaha
Please press any key to continue ...

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.