format_list_bulleted
[C++] Explanation of string operations (std::string class) in C++
最終更新日時:2022-07-04 01:06:08



In competition programming, many problems related to string manipulation are published. Therefore, this article describes the std::string class, which allows string manipulation in C++.

What is std::string in C++?

In C++, the std::string class is provided as a variable for handling strings. std::string class can be used not only to declare a string type (string type), but also to obtain the length of a string, insert and delete characters, etc.

Basic usage of std::string

This section describes the basic usage of std::string, from string assignment to output.

Code used for description

The following code is used as an example.

タイトル:sample.cpp

#include<iostream>
#include<string> //stringクラスを使用可能にする
using namespace std; 

int main() {
  string str = "Code Database"; //宣言・代入
  cout << str << endl; //出力
}

Introduction (include)

To handle the std::string class, which can handle strings, #include<string>. By declaring "using namespace std;" at the beginning of the code, you can omit the description of std:: in the main function.

#include<iostream>
#include<string> //Enable string class
using namespace std; 

Declaration and output

To declare the type of a string, write string str as shown below.

Also, to assign a string, it is necessary to enclose the value in single quotation marks ('') or double quotation marks ("") for the string to be assigned.

string str = "Code Database"; //Declaration and assignment

Now, let's actually output the data. The following code should output Code Database.

  cout << str << endl; //output

About getting the contents of a string

Obtaining the number of characters (size())

If you want to get the length of a string, you can use size().

タイトル:string_length.cpp

#include<iostream>
#include<string>
using namespace std;

int main() {
  string str1 = "Code Database";
  string str2 = "Code";
  cout << str1 << endl; 
  cout << str2 << endl; 
  cout << str1.size() << endl;
  cout << str2.size() << endl;
  cout << str1.length() << endl;
  cout << str2.length() << endl;
}

タイトル:result

Code Database
Code
13
4
13
4

Checks if string is empty (empty())

If you want to determine if a string is empty or not, you can use empty().

In this case, "1" is returned if the string is empty, and "0" if the string is not empty.

タイトル:empty.cpp

#include<iostream>
#include<string>
using namespace std;

int main() {
  string str1 = "Code Database";
  string str2 = "";
  cout << str1 << endl; 
  cout << str2 << endl; 
  cout << str1.empty() << endl;
  cout << str2.empty() << endl;
}

タイトル:result

Code Database

0
1

Character search(find())

When there is a string to be searched from a certain string, find() is used.

As shown in the code below, when find() is used to search for the letter "D" in the "Code Database", the position of the letter "D" is counted from the beginning as 0 (5). If the string is not found, the number "18446744073709551615" will appear, which is the value of string::npos.

タイトル:string_find.cpp

#include<iostream>
#include<string>
using namespace std;

int main() {
  string str1 = "Code Database";
  string str2 = "Code";  
  cout << str1.find("D") << endl;
  cout << str1.find("F") << endl;
  cout << (str1.find("F") == string::npos) << endl;
}

タイトル:result

5
18446744073709551615
1


Basic Editing of Strings

String concatenation (+)

Strings can be connected by + as in addition.

タイトル:string_plus.cpp

#include<iostream>
#include<string>
using namespace std;

int main() {
  string str1 = "Code";
  string str2 = "Database";  
  cout << str1+str2 << endl;
}

タイトル:result

CodeDatabase

String splitting(substr())

To split a string, use substr().

If substr(x) is used, the xth character or later strings can be extracted.

If substr(x,y) is used, the yth character after the xth character can be extracted.

Substr() can also be used in combination with find() to locate and split characters.

タイトル:string_substr.cpp

#include<iostream>
#include<string>
using namespace std;

int main() {
  string str1 = "Code Database";
  cout << str1.substr(5) << endl;
  cout << str1.substr(str1.find("D")) << endl;
  cout << str1.substr(5,3) << endl;
}

タイトル:result

Database
Database
Dat

Delete string(erase())

To delete a string, use erase().

If erase(x) is used, the xth character or later strings can be deleted.

If erase(x,y) is used, the xth and subsequent y characters can be deleted.

erase() can also be used in combination with find() to locate and split characters.

タイトル:string_erase.cpp

#include<iostream>
#include<string>
using namespace std;

int main() {
  string str1 = "Code Database";
  string str2 = str1.erase(5);
  str1 = "Code Database";
  string str3 = str1.erase(str1.find("D"));
  str1 = "Code Database";
  string str4 = str1.erase(5,2);
  cout << str2 << endl;
  cout << str3 << endl;
  cout << str4 << endl;
}

タイトル:result

Code 
Code 
Code tabase

Insert string(insert())

To insert a string, use insert().

Also, insert(x,y) can be used to insert the string y before the xth character.

Also, insert() can be combined with find() to search for the position (x) of a character and insert it at the xth character.

タイトル:string_insert.cpp

#include<iostream>
#include<string>
using namespace std;

int main() {
  string str1 = "Code";
  string str2 = str1.insert(4,"Database");
  str1 = "Code";
  string str3 = str1.insert(str1.find("e")+1,"Database");

  cout << str2 << endl;
  cout << str3 << endl;
}

タイトル:result

CodeDatabase
CodeDatabase

String replacement(replace())

To replace a string, use replace().

Also, when replace(x,y,z) is used, it replaces the xth to yth character string with z.

タイトル:string_replace.cpp

#include<iostream>
#include<string>
using namespace std;

int main() {
  string str1 = "Code Database";
  string str2 = str1.replace(5,4,"X");
  cout << str2 << endl;
}

タイトル:result

Code Xbase

まとめ

This article introduced the basic usage of std::string in C++. We will summarize the main points at the end.

  • The std::string class can be used to handle strings.
  • Not only can strings be declared, but also the length of strings can be obtained, characters can be inserted and deleted, etc.

I urge you all to take advantage of the string!