Basic usage of string

Posted by Mr_Mako on Wed, 08 May 2019 22:03:02 +0200

  • Worship the big man orz http://www.cnblogs.com/OIerShawnZhou/

  • Basically, header files

    #include <cstring>
    #include <string>

    These two include string s. You must write them in the exam.

  • The most basic, everybody will.

    string a;//Statement String a
    a="12345";//assignment
    string b="54321";
    int len=a.length();//Get length
    cin >> a;//Input (bounded by space newline characters)
    getline(cin,a)//You can read in spaces
    cout << a;
    swap(a,b)//exchange
    printf("%s\n",a.c_str());//C style output, what is c_str()?
  • c_str()

Returns a pointer of type char *, char

With regard to char [], char *, string, try to use strings all with string, otherwise they all use char [], the latter two transformations I think are very troublesome.

  • Overloading aspects

+ Connect two String s

string c=a+b;
c="123"+c+"321";

> <==!=: Comparisons according to dictionary order

inline bool cmp(string a,string b)
{return a>b;}
...
int main()

    sort(a+1,a+1+n,cmp);
  • insert() insert

One String A. insert (location, another string)

    string str="to be question";
    string str2="the ";
    str.insert(6,str2);// to be (the )question
  • erase() deletion

    erase(pos,n);

Deleting n characters starting with pos, such as erase(0,1), is to delete the first character.

//Connect
    str.erase(0,3); //(~~to ~~)be question
  • clear() clearance

    Eliminate...

  • replace() substitution

One String a.replace(pos,len, another String b)

Replace the characters of pos starting len in a with b

    str.replace(0,2,"To");// (To) be question

It is often used with find().

  • find() and rfind()

Perfectly Matched String b

a.find(b) Finds the first place where B appears from the beginning and returns

a.find(b,pos) starts with POS to find the location where B first appears and returns

string str="To be, or not to be - that is the question";
    int t=str.find("be");\\ t=3,str[t]='b'(To be Of be)
    int t=str.find("be",4);\\ t=17,str[t]='b'(not to be Of be)

rfind(b) or rfind(b,pos) find O()~

    int t=str.rfind("be");\\ t=17,str[t]='b'(not to be Of be)
    int t=str.rfind("be",16);\\ t=3,str[t]='b'(To be Of be)

No, return npos, i. e. - 1 (printed out as 4294967295)

if (str.find("Be")==string::npos)
    cout <<"NO"<<endl;// Output NO
if (str.rfind("Be")==-1)
    cout <<"NO"<<endl; // Output NO
  • find_first_of() and find_last_of()

Find any character in String b in a'(any one)'

a.find_first_of(b) or a.find_first_of(b,pos)

Search backwards at the beginning of a (or from pos), as long as a character encounters in a, which is the same as any character in c, stop searching and return the position of the character in a; if the match fails, return npos.

Raise a chestnut.

//Replace all vowel letters in a string with*
//Code from C++ Reference, address: http://www.cplusplus.com/reference/string/basic_string/find_first_of/
#include<iostream>
#include<string>

using namespace std;

int main()
{
    std::string str("PLease, replace the vowels in this sentence by asterisks.");
    std::string::size_type found = str.find_first_of("aeiou");
    while (found != std::string::npos)
    {
        str[found] = '*';
        found = str.find_first_of("aeiou", found + 1);
    }
    std::cout << str << '\n';
    return 0;
}
//Operation results:
//PL**s* r*pl*c* th* v*w*ls *n th*s s*nt*nc* by *st*r*sks

find_last_of

  • find_first_not_of() and find_last_not_of()

The feeling is the opposite of the previous one, similar to finding a complement set. That is to search for characters that are not found in b in a and return to their positions.

Ibid., the first is String b, the second is optional pos, do not write POS default 0

If the str.find_first_of in the previous example is changed to str.find_first_not_of, the output will be non-A (i) (u) (u) (e) (o) (escape). * * *

Summarize the parameters of "find" functions (string b,pos,len)

B is the object to be searched. pos (optional) indicates the starting point of search in a, and the third parameter len (optional) indicates the number of characters searched in B (that is, a string of b).

  • substr() string

sub(start,length)

If the second parameter is not written, it is from start to the end of the string.

string str="To be, or not to be - that is the question";
    str.substr(0,2);// To
    str.substr(str.find("question"));// question
  • String and Int interoperability (excluding C++11 functions)

    int to string

    ostringstream outs; //Output string stream
    int x = 12; 
    outs << x; //Output the contents of x to the output string stream 
    string a=outs.str(); //Using str function of string stream to get content in stream

    string to int

    string a="12";
    istringstream ins(a); //Input string stream, the content of stream is initialized to a
    int x; 
    ins >> x; //Read from is stream and store in x

It's useless. )

  • Conversion of String and char

    String to char*

      1.data()

    string str = "hello";
    const char* p = str.data();//Add const or in the form of char * p=(char*)str.data();

    At the same time, it should be noted that const should be added to compile in devc++, otherwise invalid conversion from const char to char will be wrong. Here, const can be added before or coerced into the type of char* after the equal sign.

To explain the problem below, const char can not be assigned directly to char, so compilation can not pass, the reason: if possible, then through char can modify the content of const char pointing, which is not allowed. So char has to open up a new space, that is, the form above.

  2. c_str()

string str="world";
const char *p = str.c_str();//Ibid., add const or equal sign to the right with char*

  3. copy()

string str="hmmm";
char p[50];
str.copy(p, 5, 0);//Here 5 represents the duplication of several characters and 0 represents the location of the duplication.
*(p+5)='\0';//Pay attention to manual plus terminator!!!

String to char [], direct loop assignment

string pp = "dagah";
char p[8];
int i;
for( i=0;i<pp.length();i++)
    p[i] = pp[i];
p[i] = '\0';

Summary of some references

Summary of Common Function Usage of string in c++ Language

Analysis of find Series Functions Used in C++string

Conversion of string, char*, char[] in C++

Amway's own Blog coyangjr