C++ 标准字符串std函数

回顾一些std中string类型的成员函数:

allocator_type :作为一种类型;
append :追加字符串(可以追加整个字符串,也可以追加字符串的一部分);
assign :字符串初始化或者赋值;
at :获取字符串中的第i个字符;
back:返回字符串的最后一个字符; 返回的不是迭代器,这个和begin() 不同 ;
basic_string :字符串的类型;
begin :返回的是首字母的迭代器iterator,类似指针;
c_str : 返回C-style String;Converting a string to a C-style string
capacity : 返回当前字符串的内存空间; 
clear:Erases all elements of a string.
char_type :
compare :字符串比较,可以比较两个串,也可以比较其中一部分,s3p 可以是string 也可以是char*返回0表示相等;
    comp3a = s3o.compare (  2 , 3 , s3p , 1 , 3 );
    comp2b = s2o.compare (  0 , 3 , s2p );
    comp1 = s1o.compare ( s1p );

basic_string::const_iterator :provides a random-access iterator that can access and read a const element in the string.
const_iterator :类似指向const字符串中字符的指针;
const_pointer :const字符串指针
const_reference :其实就是 char;
const_reverse_iterator :
copy : provides a pointer to a const element in a string.
data : 其实和c_str 是一样的;Converting a string to an array of characters
crbegin: Returns a const iterator that addresses the first element in a reversed string.
crend: Returns a const iterator that addresses the location succeeding the last element in a reversed string.
difference_type : 其实就是unsigned char ,A type that provides the difference between two iterators that refer to elements within the same string.
empty :Tests whether the string contains characters or not.
end :和begin是相对的,Returns an [iterator] that addresses the location succeeding the last element in a string.
erase :Removes an element or a range of elements in a string from a specified position.

find :查找,可以是查找单个字符,也可以查找字符串matches a specified sequence of characters,注意,是完全匹配,而不是只需要是字串中的某一个字符(find_first_of);

find_first_not_of :从目标串中找到第一个不属于参数串中字符的位置;
        Searches through a string for the first character that (is not an element of a specified string).
find_first_of :从目标串中找到参数串中【某个字符】(any element of a substring)出现的位置;
        Searches through a string for the first character that matches any element of a specified string.

find_last_not_of :反方向【从目标串中找到第一个不属于参数串中字符的位置】;
find_last_of :反方向,从目标串中找到参数串中【某个字符】(any element of a substring)出现的位置;

front: 返回字符串的第一个字符,和back相对;
get_allocator :Returns a copy of the allocator object used to construct the string.
insert :Inserts an element or a number of elements or a range of elements into the string at a specified position.
iterator :迭代器,比指针安全,const类型成员; A type that provides a random-access iterator that can access and read a const element in the string.
length :返回字符串长度
max_size :Returns the maximum number of characters a string could contain.
npos : -1
operator+=  :Appends characters to a string.
operator= :Assigns new character values to the contents of a string.
operator[] :Provides a reference to the character with a specified index in a string.
pointer :A type that provides a pointer to a character element in a string or character array.
pop_back: Erases the last element of the string.将最后一个字符去掉,类似栈区抛出最后一个字符;
push_back:Adds an element to the end of the string.
rbegin :Returns an iterator to the first element in a reversed string.其实begin = rend, end = rbegin;
reference :A type that provides a reference to an element stored in a string.
rend : Returns an iterator to the end element in a reversed string. 
replace :Replaces elements in a string at a specified position with specified characters or characters copied from other ranges or strings or C-strings.
        
reserve :Sets the capacity of the string to a number at least as great as a specified number.
resize : Specifies a new size for a string, appending or erasing elements as required.
reverse_iterator : 
rfind :这种r开头的表示的就是reverse;
size : Returns the current number of elements in a string.
size_type : An unsigned integer type that can represent the number of elements and indices in a string.
substr : Copies a substring of at most some number of characters from a string beginning from a specified position.
swap : Exchange the contents of two strings.
traits_type : 
value_type: char

常见函数:

int strcmp(char* s1, char* s2)
{
	while (*s1 != '\0' && *s2 != '\0')
	{
		if (*s1 != *s2)
		{
			return 1;
		}
		s1++;
		s2++;
	}
	if (*s1 == '\0' && *s2 == '\0')
	{
		return 0;
	}
	else
	{
		return 1;
	}
}
char* strcpy(char* dest, char* src)
{
	while ((*(dest++) = *(src++)) != 0);

	return dest;
}
int strlen(char* s)
{
	int len = 0;
	while (*(s) != 0)
	{
		len++;
		s++;
	}
	return len;
}
char* strcat(char* dest, char* src)
{
	while (*dest != '\0')
		dest++;
	while ((*dest++ = *src++) != '\0');

	return dest;
}

void Function()
{
	char* x = "china";

	char y[] = "china"; //

	*(x + 1) = 'A'; // 常量区,不能修改

	y[1] = 'A';

}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值