Standard C ++ String class

First, Shajiao string class?

It is the string class header file <string> supported (note that header <string.h> and <cstring>
support for C-style string manipulation functions in the C library string, but does not support the string class).

string class contains a large number of methods, includingSeveral constructor for assigning the string, combined strings, compare stringswithAccess overloaded operators of each elementas well asIt used to find the characters and the substring in the string instrumentWait. In short, a lot of content contained in the string class.

Second, a constructor

  • Basic Operations
#include<iostream>
#include<string>
using namespace std;
int main() 
{
	string str;	//构造名为str的空字符串
	cout<<str<<endl;//检测为空
	
	string str1("i like");//为字符串str1赋值
	cout<<str1<<endl;

	str1=str1+" ACM"; //在str1后面添加字符串
	cout<<str1<<endl;
	
	string str2(str1);//将字符串str2初始化为str1,相当于复制
	cout<<str2<<endl;
	
	string str3(10,'A'); //将str3初始化为由10个A组成的字符串
	cout<<str3<<endl; 
	
	string str4("Kobe Byrant",4); //访问前四个元素
	string str5("Kobe Byrant",5,6);//访问从第五个开始往后的6个元素
	cout<<str4<<" "<<str5<<endl;
	 
}

operation result:
Here Insert Picture Description

  • Overload [] operator

Calculating the output element specified by standard, attention span

	string str=("Kobe Byrant");
	cout<<str4[0]<<str4[1]<<str4[2]<<str4[3];

Results:
Here Insert Picture Description
may output a single character:

  • String concatenation
	string s;
 	s=str1+str2;
 	cout<<s;

The results are:
Here Insert Picture Description

  • Copy the C-style strings
	char alls[] = "You can really dance!";
	string str6(alls,22);
	cout<<str6;

The results are:
Here Insert Picture Description

  • Determining whether the string is empty
	string str,str1;
	cout<<str.empty()<<endl;
  
	str1="hello world!";
  	cout<<str1.empty()<<endl;

If it is empty, it returns a non-null return 0

Three, string-based input

For the C style, there are three input methods:

	char s[100];
	cin>>s;
	cin.getline(s,100); 
	cin.get(s,100);

For string objects, there are two ways:

	string str;
	cin>>str;
	getline(cin,str);

Four, string library functions

  1. String length:length();
	string s("To be or not to be");
 	cout<<s.length();
  1. Number of characters:size();
	string s("To be or not to be");
	cout<<s.size();

More than two functions accomplish the same task.

  1. capacity

If the string is increasing, exceeding the size of the memory block, the program will assign a new memory block size twice the original, to provide a large enough space to avoid continually allocate a new memory block.

(1)capacity();
Returns the size of the current block of memory allocated to a character string

//输出内存大小
	string s("To be or not to be");
	cout<<s.capacity();

The result is 23.

(2)reserve();
Allows the operator to request a minimum length of the memory block, i.e. the capacity becomes large, but not small.

//修改内存大小
	s.reserve(50);//如果输入s.reserve(17),也就是小于原来的18,容量值还是18
 	cout<<s.capacity()<<endl;

The result is 50.

  1. Re-set the number of characters:resize();
	s.resize(8);
 	cout<<s.capacity()<<endl;
 	cout<<s.length()<<endl;
 	cout<<s.size()<<endl;
 	cout<<s<<endl;

Unchanged capacity size, character length change.

  1. Output / Modify a single character function:at();

Export

	string s("To be or not to be");
 	cout<<s.at(1)<<endl;

The result is o
modified

	string s("To be or not to be");
	s.at(1)='t';
	cout<<s.c_str()<<endl;

It was replaced with t o

  1. Output all characters:c_str
	string s1="That is a question";
	cout<<s1.c_str();
  1. Insert Function

(1) at any position of the insert :insert();

	s1.insert(0,"          ");
 	cout<<s1<<endl;

Here inserted more spaces.
(2) the end of the insertion / add:append();

	s.append(s1);
	cout<<s<<endl;

The output is
Here Insert Picture Description

  1. Specified function:assign();
	s1.assign("what are you saying",5);
 	cout<<s1<<endl;

Five characters before output.

  1. Compare strings Size:compare();

(1) a whole string comparison

	string str1="abcd";
 	string str2="bcdefg";
 	cout<<str1.compare(str2)<<endl;
 	//整体比较
 //如果str1>str2,返回 1,str1=str2返回 0 ,str1<str2返回 -1.下同

(2) is compared with a whole string portion

	string str1="abcd";
 	string str2="cdefg";
 	cout<<str1.compare(12,str2)<<endl;
 	//str1的前两个与str2比较

(3) Comparison with some portion

	string str1="abcd";
 	string str2="cdefg";
 	cout<<str1.compare(12,str2,12)<<endl;
 	//str1的前两个与str2的前两个比较
  1. Copy function:copy();

The portion of an object within an array of characters copied into:

	char a[10]={0};
 	str1.copy(a,4,0);
 	//4表示选取的个数,0是位置下标
 	cout<<a<<endl;

The result is abcd

  1. Find function:find();

(1) Find a substring

	cout<<(int)s.find("question")<<endl;

 	cout<<(int)s.find(s1,2)<<endl;
 	// //若找到返回子串第一个元素的下标
 	//没有找到返回-1

(2) Find character

	cout<<(int)s.find('t')<<endl;
	//返回第一个被找到的元素下标
	//未找到返回-1
  1. Returns the substring function :substr();
	cout<<s.substr(38,46);
	//返回38~46之间的子串
	//输出结果为question
  1. To delete a character :erase;
	s.erase(18,8);
	//删除从第18个元素开始的8个字符
  	cout<<s<<endl;
  1. Replace characters:replace;
	s.replace(0,2,"To");
  	cout<<s<<endl;
//把0~2的字符替换为 “To"

Five, string library functions overall code implementation

#include<iostream>
#include<string>
using namespace std;
int main()
{
	 cout<<"求串的长度和大小:\n";
	 string s("To be or not to be!!!!!");
	 cout<<s.length()<<endl;
	 cout<<s.size()<<endl;
	 cout<<"\n";
	 
	 cout<<"求容量:\n";
	 cout<<s.capacity()<<endl;
	 cout<<"\n";
	 
	 cout<<"扩容:\n";
	 s.reserve(50);
	 cout<<s.capacity()<<endl;
	 cout<<"\n";
	 
	 cout<<"修改容量:\n";
	 s.resize(18);
	 cout<<s.capacity()<<endl;
	 cout<<s.length()<<endl;
	 cout<<s.size()<<endl;
	 cout<<s<<endl; 
	 cout<<"\n";
	 
	 cout<<"输出/修改单个字符:\n";
	 cout<<s.at(1)<<endl;
	 s.at(1)='O';
	 cout<<s.c_str()<<endl; 
	 cout<<"\n";
	 
	 cout<<"全输出:\n";
	 string s1="That is a question";
	 cout<<s1.c_str();
	 cout<<"\n";
	 
	 cout<<"任意位置插入:\n";
	 s1.insert(0,"          ");
	 cout<<s1<<endl;
	 cout<<"\n";
	 
	 cout<<"末尾插入:\n";
	 s.append(s1);
	 cout<<s<<endl;
	 cout<<"\n";
	 
	 cout<<"指定输出:\n";
	 string s2;
	 s2.assign("what are you saying",5);
	 cout<<s2<<endl;
	 cout<<"\n";
	 
	 cout<<"比较函数:\n";
	 string str1="abcd";
	 string str2="cdefg";
	 cout<<str1.compare(str2)<<endl;
	 cout<<"\n";
	 
	 cout<<"复制函数:\n";
	 char a[10]={0};
	 str1.copy(a,4,0);
	 cout<<a<<endl;
	 cout<<"\n";
	 
	 cout<<"查找函数:\n";
	 cout<<(int)s.find("question")<<endl;
	 cout<<(int)s.find(s1,2)<<endl;
	 cout<<"\n";
	  
	  cout<<"查找字符:\n";
	 cout<<(int)s.find('t')<<endl;
	 cout<<"\n";
	  
	  cout<<"返回子串:\n";
	 cout<<s.substr(38,46);
	 cout<<"\n";
	  
	  cout<<"删除部分子串:\n";
	 s.erase(18,8);
	 cout<<s<<endl;
	 cout<<"\n";
	  
	 cout<<"最终结果为:\n"; 
	 s.replace(0,2,"To");
	 cout<<s<<endl; 
	}

The output is:
Here Insert Picture Description
Continued
Here Insert Picture Description


There are a lot of string functions, while studying the future accumulation

Published 34 original articles · won praise 85 · views 4607

Guess you like

Origin blog.csdn.net/weixin_45895026/article/details/104074638