当前位置:网站首页>STL notes (IV): String
STL notes (IV): String
2022-07-25 05:09:00 【Reyn Morales】
STL note ( Four ): character string
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
// www.cplusplus.com - documentation
// Creation and initialization of strings
void test1()
{
string s1;
string s2("Reyn");
string s3(s2); // Copy structure
string s4(s2, 0, 2); // Start from zero , Length is two
string s5 = "Lisa"; // assignment - Operator overloading
string s6 = s2 + "Good!"; // Sum up - Operator overloading - String splicing
string s7(s2.begin(), s2.end() + 1); // s6 = s2 [ ) - Left closed right away
string s8(s2.begin(), s2.begin() + 3); // "Rey"
}
// String basic operation
void test2()
{
// 1. Reading data
string s = "How are you";
cout << s[0] << endl; // 'H', Do not detect out of bounds
cout << s.at(0) << endl; // 'H', Exceptions will be thrown for out of bounds operations
// 2. length
cout << s.size() << endl;
// 3. Substring
cout << s.substr(0, 7) << endl;
// 4. Replace
cout << s.replace(0, 1, "h") << endl;
// 5. In exchange for
string str1 = "Reyn";
string str2 = "Lisa";
str1.swap(str2);
cout << str1 << ' ' << str2 << endl;
// 6. Compare
if (str1 > str2)
cout << "fuc" << endl;
if (str1 == str2)
cout << "Nice job" << endl;
}
// Insert 、 Delete
void test3()
{
// 1. Insert
string s = " do ";
s.insert(0, "How"); // Insert at any position
s.append("you do"); // Append the string to the end
s += "?"; // String splicing
cout << s << endl; // How do you do?
// 2. Delete
s.erase(0, 4);
s.erase(6);
s.erase(s.begin(), s.begin() + 3);
s.erase(s.begin());
cout << s << endl;
}
// Inquire about
void test4()
{
string s = "How are you?";
int position;
position = s.find("are");
position = s.find("fuck"); // Returns... When there is no string in the string -1
position = s.find("o", 5);
cout << position << endl;
// The first character position that matches any character in the specified string
position = s.find_first_of("oau");
cout << position << endl;
// The last character position that matches any character in the specified string
position = s.find_last_of("oau");
cout << position << endl;
// The first character position that does not match all characters in the specified string
position = s.find_first_not_of("oau");
cout << position << endl;
// The last character position that does not match all characters in the specified string
position = s.find_last_not_of("oau");
cout << position << endl;
}
// Conversion between integer and string
void test5()
{
// Integer to string ------- 45 -- Out --> str( Character stream - Memory ,text) -- Enter into --> s
int num = 45;
string s;
stringstream str;
str << num;
str >> s;
cout << s << endl;
str.clear(); // Empty the string buffer , You can also redeclare a character stream
// String to integer ------- t -- Out --> str( Character stream ) -- Enter into --> mun
string t = "56";
int mun;
str << t;
str >> mun;
cout << mun << endl;
}
// Split string ------- str( Memory ) -- Enter into --> s
void test6()
{
string str = "how are you";
string s;
istringstream in(str);
while (!in.eof()) { // Execute a loop when the string stream does not reach the end
in >> s;
cout << s << endl;
}
cout << endl;
// The separator is a comma
string rts = "how,are,you";
istringstream ni(rts);
while (!ni.eof()) {
getline(ni, s, ','); // Use comma as separator , Stream characters (ni) The substring in is assigned to the string variable (s)
cout << s << endl;
}
}
// Remove spaces at both ends of the string
void test7()
{
string s = " Hello World ";
cout << s << endl;
s.erase(0, s.find_first_not_of(' '));
s.erase(s.find_last_not_of(' ') + 1);
cout << s << endl;
}
int main()
{
// test1(); // Creation and initialization of strings
// test2(); // String basic operation
// test3(); // Insert 、 Delete
// test4(); // Inquire about
// test5(); // Conversion between integer and string
// test6(); // Split string
// test7(); // Remove spaces at both ends of the string
return 0;
}
Created on initialization
- Default structure
- Copy structure
- Operator overloading
- iterator
Basic operation
- Reading data :
s[0]; s.at(); - length :
s.size(); - Substring :
s.substr(); - Replace :
s.replace(); - In exchange for :
s.swap(); - Compare :
< <= > >= == !=
Insert 、 Delete 、 Inquire about
- Insert :
s.insert(); - Delete :
s.erase(); - Inquire about :
s.find();
flow
The various streams in the standard template library can be regarded as the channels of data from one data store to another ( medium ), One of the buffers is usually a program . therefore , It can be constructed by “ flow ” Object to establish the channel between the program and other storage areas , Through the unique characteristics of each flow , It can simply complete some functions .

In the next article , We will introduce C++ Iterators in
边栏推荐
- After watching the latest interview with big manufacturers, these six JVM interview questions were asked
- deep报错
- [analysis of GPIO register (crl/crh) configuration of STM32]
- STL notes (I): knowledge system
- epoll的实现原理
- ES6 -- Methods and extensions of array objects, traversal of arrays, and extension methods of strings
- [wechat applet] label (86/100)
- 38 lines of PHP code free import database analysis Linux access log
- Leetcode55. Jumping game
- How to judge whether it is attacked by DDoS
猜你喜欢

龙蜥社区发布首个 Anolis OS 安全指南 为用户业务系统保驾护航

教你三招从让性能从20s优化到500ms

Zhongchuang computing power won the recognition of "2022 technology-based small and medium-sized enterprises"

Ownership in rust -- introduction of rust language Xiaobai 11

Unity LOD

Getting started with scratch

Introduction to base ring tree

Unity LOD

Dragon Dragon community released the first Anolis OS Security Guide to escort users' business systems

Redis的三个必知必会的问题
随机推荐
Zhongchuang computing power won the recognition of "2022 technology-based small and medium-sized enterprises"
Browser cache HTTP cache CDN cache localstorage / sessionstorage / cookies
After watching the latest interview with big manufacturers, these six JVM interview questions were asked
Blog Description & message board
I will write some Q & A blogs recently, mainly focusing on the points that are easy to have doubts.
2022-7-15 summary
使用getifaddrs获取本机网口IP地址
Baklib: share some methods about building enterprise knowledge management (km)
The 6th "Blue Hat Cup" National College Students' Cyber Security Skills Competition writeup
The market is right
Get the parameters of the browser address bar
Document collaboration tool recommendation
Logu p3398 hamsters find sugar solution
龙蜥社区发布首个 Anolis OS 安全指南 为用户业务系统保驾护航
Gbase 8A about no suitable driver
Wechat official account all article download links to get
Harbor installation
rhcsa暑假第二天
Three must know and know problems of redis
[small program practice] first day