The C string function corresponds to C++ string operation detail

  • 2020-06-23 01:34:03
  • OfStack

string is a standard class in STL. We are used to using pointer to operate char * string of C. At the beginning, it is not very suitable.

strcpy: str.substr()

strncpy: str.substr(pos, n)

strcmp: ==

strncmp: ==

strstr: str.find()

strcat: str append ()

strchr: str find ()

strtok: No corresponding function, using ES45en.find () to encapsulate 1 ES47en.split ()

The ultimate tip:

Conversion of char * to string

char converted to string: string str(char)

string converted to char * : ES68en. c_string()

Extended learning:

String (substr() function)


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

  cout<<" return str[3] Subsequent substrings :"<<str.substr(3)<<endl; 
  cout<<" Return from 2 The start of the 4 A string of six characters, including 2 Character of position :"<<str.substr(2,4)<<endl;
  return 0;
}

Above is the introduction of all the relevant knowledge points, you have any supplementary content can contact this site this site.


Related articles: