STL learning notes string

Posted by derrick24 on Sun, 19 Sep 2021 20:41:52 +0200

STL learning notes (I) string

c_str()

const char *c_str();

c_ The str() function returns a pointer to the normal C string, with the same content as this string

string s="123"
printf("%s",s.c_str())

Add: insert()

  iterator insert( iterator i, const char &ch );
  basic_string &insert( size_type index, const basic_string &str );
  basic_string &insert( size_type index, const char *str );
  basic_string &insert( size_type index1, const basic_string &str, size_type index2, size_type num );
  basic_string &insert( size_type index, const char *str, size_type num );
  basic_string &insert( size_type index, size_type num, char ch );
  void insert( iterator i, size_type num, const char &ch );
  void insert( iterator i, iterator start, iterator end );

Insert a character ch before the position represented by iterator i, and insert the string str at the position index of the string,
Insert a substring of the string str at the position index of the string (starting from index2, num characters long),
Insert num characters of string str at the position index of the string, and insert a copy of num characters ch at the position index of the string,
Insert a copy of num characters ch before the position represented by iterator i, and insert a character before the position represented by iterator i, starting with start and ending with end

Delete: erase()

  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 to by pos, return the iterator pointing to the next character, and delete all characters from start to end,
Returns an iterator, points to the next position of the last character to be deleted, deletes num characters from the index index, and returns the * this. Parameters index and
num has a default value, which means that erase() can be called with only index to delete all characters after index, or without any parameters to delete all characters
For example:

string s("So, you like donuts, eh? Well, have all the donuts in the world!");
cout << "The original string is '" << s << "'" << endl;
  
s.erase( 50, 14 );
cout << "Now the string is '" << s << "'" << endl;

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

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

Will show

The original string is 'So, you like donuts, eh? Well, have all the donuts in the world!'
Now the string is 'So, you like donuts, eh? Well, have all the donuts'
Now the string is 'So, you like donuts, eh?'
Now the string is ''

Change: replace()

  basic_string &replace( size_type index, size_type num, const basic_string &str );
  basic_string &replace( size_type index1, size_type num1, const basic_string &str, size_type index2,
  size_type num2 );
  basic_string &replace( size_type index, size_type num, const char *str );
  basic_string &replace( size_type index, size_type num1, const char *str, size_type num2 );
  basic_string &replace( size_type index, size_type num1, size_type num2, char ch );
  basic_string &replace( iterator start, iterator end, const basic_string &str );
  basic_string &replace( iterator start, iterator end, const char *str );
  basic_string &replace( iterator start, iterator end, const char *str, size_type num );
  basic_string &replace( iterator start, iterator end, size_type num, char ch );

Replace the characters in this string with num characters in str, starting with index
Replace the characters in this string with num2 characters in str (starting from index2), starting from index1 and up to num1 characters
Replace the characters in this string with num characters in str (starting from index)
Replace the characters in this string with num2 characters in str (starting from index2), and num1 characters starting from index1
Replace the characters in this string with num2 ch characters, replace the characters in this string with the characters in str from index, and the iterators start and end indicate the range
Replace the contents of this string with num characters in str, and the iterators start and end indicate the range,
Replace the contents of this string with num ch characters, and the iterators start and end indicate the range

Query: find()

Returns the first occurrence of str in a string (starting with index). If it is not found, it returns string::npos,
Returns the position where str first appears in the string (starting from index and length). If it is not found, it returns string::npos,
Returns the first occurrence of the character ch in a string (starting with index). If not found, return string::npos

For example,

	string str1( "Alpha Beta Gamma Delta" );
	if( str1.find("amm", 0 ) != string::npos )
		cout << "Found Omega at " << str1.find( "amm", 0 ) << endl;
	else
		cout << "Didn't find amm" << endl;

Found Omega at 12

After testing, string::npos is octet all 1.

Topics: C C++ data structure STL