C++ string basic usage

Table of contents

1. How to create a C++ string

1. The header file of string

TIP:

2. Common way to create string

3. Rare initialization method

2. The difference between C++ string and C language char*

1. The most essential difference

2. How to access the string defined by string?

 3. Basic operations of C++ string

1. Comparison of strings

(1), how to compare strings

(2) cout is more practical

(3) Call the comparison member function

2. String concatenation

(1) cout direct method

(2) Connect through the append() function

3. Search for characters/strings

(1) Commonly used find function and rfind function search

 4. String replacement (replace function)

5. Deletion of strings (erase function)


1. How to create a C++ string

1. The header file of string

#include <iostream>//等价于C语言中的#include<stdio.h>
#include <cstring>
#include <string.h>
//以上两种都是C语言的string头文件
#include <string>//真正的C++头文件

TIP:

In C++, #include<string> is the real C++ header file!

2. Common way to create string

int main()
{
  string str1 = "str1";
  cout << str1 << endl;//调用构造函数
  string str2;
  str2 = "str2";
  cout << str2 << endl;//调用拷贝构造函数
  string str3 = ("str3");
  cout << str3 << endl;
  return 0;
}

Output result:

3. Rare initialization method

string str4(4,'x);

 Initialize the string with 4 characters, the output result is xxxx

string str5("iloveyou",1,4);

Indicates that str5 is initialized with 4 bits starting from subscript 1 of the string (in C++, the subscript of the string also starts from 0, which is the same as the array in C language)

So the output is love

2. The difference between C++ string and C language char*

1. The most essential difference

The reason is that the string defined by C++ string does not have '\0 ' !

char arr[]="iloveyou";
string str5="iloveyou";
cout<<str5.size()<<endl;
cout<<str5.length()<<endl;

Two functions that output strings in C++ - length(), size()

There are 8+1 characters in total defined by the char type above, and the output result defined by string below is 8 characters.

2. How to access the string defined by string?

Note that the string type can be directly accessed with square brackets in the form of an array [], and the subscript also starts from 1 !

First of all, it cannot be accessed by the printf (%s) function in C language, because the string type is not a string type in essence.

 If access is forced, the output will be garbled characters!

C++ string provides us with an interface to access strings - data(), c_str();

These two functions convert the string type to a string type , which is convenient for us to print with the printf function .

int main()
{
  string str1 = "str1";
  cout << str1 << endl;
  printf("%s\n", str1);
  printf("%s\n", str1.data());
  printf("%s\n", str1.c_str());
  return 0;
}

output result

 3. Basic operations of C++ string

1. Comparison of strings

In C++, the rules for string comparison are the same as in C language.

(1), how to compare strings

Q: Compared with "123" and "13", which string is bigger?

Answer: "13" is bigger, because comparing from left to right, characters are compared according to ASCII code value, the first character is '1', the second character '3' is bigger than '2', so "13" is bigger than '2' "123" large.

In C++, cout can directly output the comparison of judging strings

(2) cout is more practical
int main()
{
  string first = "123";
  string second = "13";
  cout << (first < second) << endl;
  cout << (first != second) << endl;
  cout << (first > second) << endl;
  return 0;
}

Precautions

  • Because of the return value problem, use () to enclose the comparison formula
  • The return value is bool type, if true, return 1, false return 0 .
(3) Call the comparison member function

cout << first.compare(second) << endl;
return value is 1: first-second>0 ------> first>second

The return value is -1: first-second<0 ------> first<second

The return value is 0: first-second=0 ------> first=second

2. String concatenation

(1) cout direct method
int main()
{
  string first = "i love you,";
  string second = "i miss you";
  cout << first + second << endl;
  return 0;
}

If you want to add the two strings first and second, use addition + directly in cout! 

The output result is: i love you, i miss you

(2) Connect through the append() function

Because the above cout is directly +, it can only add two complete strings, which is too cumbersome, and the append() function is more flexible, you can decide which digit to start connecting, and the number can be determined by yourself.

int main()
{
  string first = "i love you,";
  string second = "i miss you";
  cout << first + second << endl;
  first.append(second);
  cout << first << endl;
  first.append(second, 1, 5);
  cout << first << endl;
  first.append(4, '1');
  cout << first << endl;
  first.append("i miss you", 1, 5);
  cout << first << endl;
  return 0;
}

Output result:

3. Search for characters/strings

Foreword:

The function of the string search type, if not found, returns -1.

Therefore, when searching, it is generally necessary to judge whether the return value is equal to string:: nops (the value is -1 )

(1) Commonly used find function and rfind function search


The find function finds the occurrence of a string or character from left to right.

The rfind function finds occurrences of strings or characters from right to left.

int main()
{
  string first = "i love you,";
  string second = "i miss you";
  if (first.find('o') != -1)
  {
    cout << first.find('o') << endl;
  }
  if (first.find("love") != -1)
  {
    cout << first.find("love")<< endl;
  }
  return 0;
}

 4. String replacement (replace function)

The parameters of the replace function generally start with the original subscript to be replaced, followed by the number of replaced characters.

int main()
{
  string first = "iloveyou,";
  string second = "imissyou";
  first.replace(1, 4,second,1,4);
  cout << first << endl;
  return 0;
}

The output is imissyou

5. Deletion of strings (erase function)

int main()
{
  string first = "iloveyou,";
  string second = "imissyou";
  first.erase(0);
  cout << first << endl;
  second.erase(1, 4);
  cout << second << endl;
  return 0;
}

The first method of use is to delete the subscript 0 and subsequent characters ( note that all subsequent characters are deleted! )

The second method is to delete the subscript 1, followed by 4 characters ( the number of custom deletions)

Guess you like

Origin blog.csdn.net/hanwangyyds/article/details/131443051