string中常用函数总结三(substr、compare和replace)(C++11)

substr(复制子串)

(1)basic_string substr(size_type __pos = 0, size_type __n = npos)
//从pos处开始复制长度为n的子字符串,pos默认为0,n默认为npos

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string a="0123456789",b;
	b=a.substr(0,3);
	cout<<b;//输出012 
}

compare(比较大小)

(1)int compare(const basic_string& __str)
//和str按字典序比较大小,若小于str返回小于0的值,若等于str返回0,若大于str返回大于0的值

(2)int compare(size_type __pos, size_type __n, const basic_string& __str)
//从pos位置开始长度为n的子字符串和str按字典序比较大小,返回值同上

(3)int compare(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2)
//从pos1位置开始长度为n1的子字符串和str从pos2位置开始长度为n2的子字符串按字典序比较大小,返回值同上

(4)int compare(const _CharT* __s)
//同(1)

(5)int compare(size_type __pos, size_type __n1, const _CharT* __s)
//同(2)

(6)int compare(size_type __pos, size_type __n1, const _CharT* __s,size_type __n2)
//同(3)

replace(替换字符(串))

(1)basic_string& replace(size_type __pos, size_type __n, const basic_string& __str)
//从pos位置开始长度为n的子字符串替换为字符串str,str长度不必等于n

(2)basic_string& replace(size_type __pos1, size_type __n1, const basic_string& __str, size_type __pos2, size_type __n2)
//从pos1位置开始长度为n1的子字符串替换为字符串str从pos2位置开始长度为n2的子字符串

(3)basic_string& replace(size_type __pos, size_type __n1, const _CharT* __s,size_type __n2);
//从pos位置开始长度为n1的子字符串替换为s的前n2个字符

(4)basic_string& replace(size_type __pos, size_type __n1, const _CharT* __s)
//同(1)

(5)basic_string& replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
//从pos位置开始长度为n1的子字符串替换为n2个字符c

(6)basic_string& replace(iterator __i1, iterator __i2, const basic_string& __str)
//i1到i2间的子字符串替换为字符串str(左闭右开,下同)

(7)basic_string& replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
//i1到i2间的子字符串替换为字符串str的前n个字符

(8)basic_string& replace(iterator __i1, iterator __i2, const _CharT* __s)
//同(6)

(9)basic_string& replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
//i1到i2间的子字符串替换为n个字符c

(10)basic_string& replace(iterator __i1, iterator __i2,_InputIterator __k1, _InputIterator __k2)
//i1到i2间的子字符串替换为k1到k2间的子字符串

(11)basic_string& replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
//同(10)

(12)basic_string& replace(iterator __i1, iterator __i2, const_iterator __k1, const_iterator __k2)
//同(10)

(13)basic_string& replace(iterator __i1, iterator __i2, initializer_list<_CharT> __l)
//initializer_list是C++11提供的新类型,用于表示某种特定类型的值的数组,和vector一样,initializer_list也是一种模板类型
     
     

猜你喜欢

转载自blog.csdn.net/qq_40889820/article/details/81603550