C + + strings (string)

Posted by dclamp on Tue, 21 Jan 2020 11:49:20 +0100

C + + strings (string)

Constructors

Syntax:

  string();
  
  string( size_type length, char ch );
  
  string( const char *str );
  string( const char *str, size_type length );
  string( string &str, size_type index, size_type length );
  
  string( input_iterator start, input_iterator end );

The constructor of a string creates a new string, including:
Copy of ch with length (i.e. length ch)
Start with str (any length),
The substring starting with index, with length, or
Take the element from start to end as the initial value
For example,

1. If there is no initialized operation string str_;, the default constructor string() will be called;

#include<iostream>
using namespace std;
int main()
{
	string str_1;
	cout << str_1;	
	return 0; 
}

2. Call the constructor string (size_type length, char CH); initialize string

#include<iostream>
using namespace std;
int main()
{
   string str_2(10, 'A');
   cout << str_2;
   return 0; 
}

3. Call constructors separately

string( const char *str );
string( const char *str, size_type length );
string( string &str, size_type index, size_type length );

Initialize string

#include<iostream>
using namespace std;
int main()
{
	string str_3_0();
	
	string str_3_1_1("0123456789");
	string str_3_1_2("0123456789", 5);
	string str_3_1_3("0123456789", 0, 5);
	
	string str_3_2_1(str_3_1_1);
	string str_3_2_2(str_3_1_1, 5);
	string str_3_2_3(str_3_1_1, 0, 5);
	
	cout <<  "str_3_0 = " << str_3_0 << endl << endl;
	
	cout <<  "str_3_1_1 = " << str_3_1_1 << endl;
	cout <<  "str_3_1_2 = " << str_3_1_2 << endl;
	cout <<  "str_3_1_3 = " << str_3_1_3 << endl << endl;
	
	cout <<  "str_3_2_1 = " << str_3_2_1 << endl;
	cout <<  "str_3_2_2 = " << str_3_2_2 << endl;
	cout <<  "str_3_2_3 = " << str_3_2_3 << endl;

	return 0; 
}

Result

Note: String STR < 0(); then STR < 0 is "1"
Note the distinction:

string str_3_1_2("0123456789", 5);
string str_3_2_2(str_3_1_1, 5);

The first one calls string (const char * STR, size_type length); if parameter 5 is length, the length of string is 5.
The second line calls string (string & STR, size_type index, size_type length); if parameter 5 is index, the construction starts from the 5th character of str. if the parameter is not received by length, the string defaults to the full length after index.

4. Call the constructor string (input iterator start, input iterator end); initialize string

#include<iostream>
using namespace std;
int main()
{

	string str_3_1_1("0123456789");

	string::iterator begin = str_3_1_1.begin()+5;
	string::iterator end   = str_3_1_1.end();	
	
	string string_4(begin, end);
	
	cout << string_4;

	return 0; 
}

Operators


Use '=', '' ',' = ', and' < 'to compare string value sizes

#include<iostream>
#include<string>
using namespace std;
int main()
{

	string str_1 = "123";
	string str_2 = "00000000001";

	if(str_1 > str_2)
		cout << str_1 << " > " << str_2 << endl;
	else if(str_1 < str_2)
		cout << str_1 << " < " << str_2 << endl;
	else
		cout << str_1 << " = " << str_2 << endl;

	return 0; 
}

Result
Change to

string str_1 = "1";
string str_2 = "01";

Result

Change to

string str_1 = "A";
string str_2 = "B";

Result

Summary: the comparison algorithm is to see the ASCII code size of the first character of the string. If it is the same, then compare the next character until the end.
Therefore, the case of 01 < 1 will appear. It should be noted that the whole string is not treated as a numerical comparison.

Use '+':

#include<iostream>
#include<string>
using namespace std;
int main()
{

	string str_1 = "A";
	string str_2 = "B";
	
	string str_3 = str_1 + str_2;	
	cout << str_3 << endl << endl;
	
	str_3 = "123" + str_3;	
	cout << str_3 << endl;

	return 0; 
}

Result:

Note that '-' is not overloaded.

Use '[]':

#include<iostream>
#include<string>
using namespace std;
int main()
{

	string str = "0123456789";
	cout << str[6] << endl;
	
	str[6] = 'A';	
	cout << str << endl;

	return 0; 
}

Result:

Add text (append) (not commonly used)

Assign (not commonly used)

at

Syntax:

reference at( size_type index );

The at() function returns a reference to the character at the index position. If the index is not within the string range, at() will report an "out of range" error and throw an out of range exception. For example, the following code:

#include<iostream>
#include<string>
using namespace std;
int main()
{

	string str_1 = "0123456789";
	
	int index = 4; 
	char c = str_1.at(index);	
	cout << "Character string str_1 The first " << index << " A character is " << c << endl << endl;;
	
	c = 'A';
	cout << str_1;

	return 0; 
}

Result:

It can quickly obtain characters with unknown subscripts, such as str_1.at(index); and str_1[index] is not allowed.
The at() function returns a reference, but why in the above example, you can't modify str_1 by modifying C.

begin

Syntax:

iterator begin();

The begin() function returns an iterator that points to the first element of the string

end

Syntax:

iterator end();

The end() function returns an iterator that points to the end of the string (the next position of the last character)

Application:

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str_1 = "0123456789";
	
	string::iterator begin = str_1.begin();
	cout << "*begin = " << *begin << endl;
	
	string::iterator end = str_1.end();
	cout << "*end = " << *end << endl;
	end--;
	cout << "*end(after end--) = " << *end << endl;
	
	cout << "(int)(end-begin) = " << (int)(end-begin) << endl;

	return 0; 
}

Result:

c_str

Syntax:

const char *c_str();

The C ﹣ str() function returns a pointer to a regular C string with the same contents as this string

Application:

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str_1 = "0123456789";
	
	const char *p = str_1.c_str();	//Must add const
	cout << *p << endl;

	while(*p!='\0')
	{
		cout << *p;
		p++;
	}
	
	return 0; 
}

Result:

Syntax: const char c_str(); the c_str() function returns a pointer to a regular C string, with the same content as this string
This is to be compatible with C language. There is no string type in C language. Therefore, string must be set by the member function c_str() of string class object
Object to the string style in c. Note: be sure to use the strcpy() function to operate the pointer returned by the method c ﹐ str(), for example: it's better not to do this: char
c. String s = "1234"; c = s.c_str(); //c finally points to garbage, because s object is destructed, its content is processed
It should be used as follows: char c[20]; string s = "1234"; strcpy(c,s.c_str());
In this way, there is no error. C_str() returns a temporary pointer, which cannot be operated on. For another example, c_str() returns a string in the form of char *
Include string if a function requires the char * parameter, you can use the C ﹐ str() method: string s = "Hello World!";
Printf (""% s ", s.c_str()); / / output" Hello World! "

data

data
Syntax:

const char *data();

The data() function returns a pointer to its first character

Related topics:
c_str()

string.data(): similar to c_str(), but the returned array does not terminate with a null character.

Capacity (omitted)

Compare

(it is recommended to use the operator to compare two strings.)

Copy (infrequently)

empty

Syntax:
bool empty();

empty() returns true if the string is empty, false otherwise

Delete (erase)

Syntax:

iterator erase( iterator pos );
iterator erase( iterator start, iterator end );
basic_string &erase( size_type index = 0, size_type num = npos );

The erase() function can:

Delete the character pointed by pos, return the iterator pointing to the next character, delete all characters from start to end, return an iterator, point to the next position of the last character to be deleted, delete num characters starting from index index, return * this. Parameter index and num have default values, This means that erase() can be called with index only to delete all characters after index, or without any parameters to delete all characters

Application:

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string s("0123456789");
    cout << "The original string is '" << s << "'" << endl;
  
    s.erase( 5, 2 );
    cout << "Now the string is '" << s << "'" << endl;

    s.erase( 5 );
    cout << "Now the string is '" << s << "'" << endl;

    s.erase();
    cout << "Now the string is '" << s << "'" << endl;

	return 0; 
}

Result:

Published 31 original articles, won praise 15, visited 10000+
Private letter follow

Topics: C ascii