C++ Strings(字符串)

1. 构造函数

语法:

  • string();
  • string(size_type length, char ch);
  • string(const char *str);
  • string(const char *str, size_type length);
  • string(string &str, size_type index, size_type length);
  • string(input_iterator start, input_iterator end);

字符串的构造函数创建一个新字符串,包括:

  • 以length为长度的ch的拷贝(即length个ch)
  • 以str为初值(长度任意)
  • 以index为索引开始的子串,长度为length
  • 以从start到end的元素为初值
#include<iostream>
using namespace std;

int main() {
	string str1(5, 'c');
	string str2("Now is the time...");
	string str3(str2, 11, 4);
	
	cout << str1 << endl;
	cout << str2 << endl;
	cout << str3 << endl;
	/*
	ccccc
	Now is the time...
	time
	*/
} 

2. 添加文本(append)

语法:

  • basic_string &append(const basic_string &str);
  • basic_string &append(const char *str);
  • basic_string &append(const basic_string &str, size_type index, size_type length);
  • basic_string &append(char *str, size_type num);
  • basic_string &append(size_type num, char ch);
  • basic_string &append(input_iterator start, input_iterator end);

append()函数可以完成以下工作:

  • 在字符串的末尾添加str
  • 在字符串的末尾添加str的子串,子串以index索引为开始,长度为len
  • 在字符串的末尾添加str中的num个字符
  • 在字符串的末尾添加num个字符ch
  • 在字符串的末尾添加以迭代器start和end表示的字符序列。
#include<iostream>
using namespace std;

int main() {
	string str = "Hello World";
	str.append(10, '!');  // 添加10个‘!’ 
	cout << str << endl;
	// Hello World!!!!!!!!!! 
	return 0; 
} 

3. 赋值(assign)

语法:

  • basic_string &assign(const basic_string &str);
  • basic_string &assign(const char *str);
  • basic_string &assign(const char *str, size_type num);
  • basic_string &assign(const basic_string &strr, size_type index, size_type length);
  • basic_string &assign(size_type num, char ch);

函数以下列方式赋值:

  • 用str为字符串赋值
  • 用str的开始num个字符为字符串赋值
  • 用str的子串为字符串赋值,子串以index索引开始,长度为length
  • 用num个字符ch为字符串赋值
#include<iostream>
using namespace std;

int main() {
	string str1, str2 = "War and Peace";
	str1.assign(str2, 4, 3);
	cout << str1 << endl;  // and
	return 0; 
} 

4. at

语法:

reference at(size_type index);

at()函数返回一个引用,指向index位置的字符,如果index不在字符串范围内,at()将报告"out of range"错误,并抛出out_of_range异常。

#include<iostream>
using namespace std;

int main() {
	string text = "ABCDEF";
	char ch = text.at(2);
	cout << ch << endl;  // C
	
	char ch2 = text.at(6);
	cout << ch2 << endl;
	/* terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::at: __n (which is 6) >= this->size() (which is 6) */
	return 0; 
} 

5. begin

语法:

iterator begin();

begin()函数返回一个迭代器,指向字符串的第一个元素。

6. c_str

const char *c_str();

c_str()函数返回一个指向正规C字符串的指针,内容与本字符串相同。

7. 容量(capacity)

语法:

size_type capacity();

capacity()函数返回在重新申请更多的空间前字符串可以容纳的字符数,这个数字至少与size()一样大。

8. 比较(compare)

语法:

  • int compare(const basic_string &str);
  • int compare(const char *str);
  • int compare(size_type index, size_type length, const basic_string &str);
  • int compare(size_type index, size_type length, const basic_string &str, size_type index2, size_type length2);
  • int compare(size_type index, size_type length, const char *str, size_type length2);

compare()函数以多种方式比较本字符串str,返回:

返回值                情况

小于0                this < str

0                       this == str

大于0                this > str

不同的函数:

  • 比较自己和str
  • 比较自己的子串和str,子串以index索引开始,长度为length
  • 比较自己的子串和str的子串,其中index2和length2引用str,index和length引用自己
  • 比较自己的子串和str的子串,其中str的子串以索引0开始,长度为length2,自己的子串以index开始,长度为length

9. 拷贝(copy)

语法:

size_type copy(char *str, size_type num, size_type index);

copy()函数拷贝自己的num个字符到str中(从索引index开始)。返回值是拷贝的字符数。

10. data

语法:

const char *data();

data()函数返回指向自己的第一个字符的指针

11. empty

语法:

bool empty();

如果字符串为空,则返回true; 否则,返回false

12. end

语法:

iterator end();

end()函数返回一个迭代器,指向字符串的末尾(最后一个字符的下一个位置)

13. 删除(erase)

语法:

  • iterator erase(iterator pos);
  • iterator erase(iterator start, iterator end);
  • basic_string &erase(size_type index = 0, size_type num = npos);

erase()函数可以:

  • 删除pos指向的字符,返回指向下一个字符的迭代器
  • 删除从start到end的所有字符,返回一个迭代器,指向被删除的最后一个字符的下一个位置
  • 删除从index索引开始的num个字符,返回*this
#include<iostream>
using namespace std;

int main() {
	string s = "So, you like donuts, eh? Well, have all the donuts in the world!";
	cout << "The original string is: " << s << endl;
	s.erase(50, 14);
	cout << "Now th string is:" << s << endl;
	s.erase(24);
	cout << "Now th string is:" << s << endl;
	s.erase();
	cout << "Now th string is:" << s << endl;
	
	/*
	The original string is: So, you like donuts, eh? Well, have all the donuts in the world!
	Now th string is:So, you like donuts, eh? Well, have all the donuts
	Now th string is:So, you like donuts, eh?
	Now th string is:
	*/
	return 0; 
} 

14. 查找(find)

语法:

  • size_type find(const basic_string &str, size_type index);
  • size_type find(const char *str, size_type index);
  • size_type find(const char *str, size_type index, size_type length);
  • size_type find(char ch, size_type index);

find()函数:

  • 返回str在字符串中第一次出现的位置(从index开始查找)。如果没有找到则返回string::npos
  • 返回str在字符串中第一次出现的位置(从index开始查找,长度为length)。如果没有找到就返回string::npos
  • 返回字符ch在字符串中第一次出现的位置(从index开始查找)。如果没找到就返回string::npos
#include<iostream>
using namespace std;

int main() {
	string str = "Alpha Beta Gamma Delta";
	unsigned int loc = str.find("Omega", 0);
	if(loc != string::npos) {
		cout << "Found Omega at:" << loc << endl;
	} else {
		cout << "Didn't find Omega" << endl;
	}
	// Found Omega at:4294967295
	/*
	在 C++ 中,string::npos 是一个常量,通常被定义为无符号整数的最大值,
	表示在字符串查找操作中未找到指定的子串。在大多数系统中,
	string::npos 的值为 4294967295,即无符号整数的最大值。
	因此,当调用 find() 函数在字符串中查找子串"Omega"时,由于"Omega"并不在原始字符串中,
	find() 函数会返回 string::npos,即 4294967295。
	因此,输出结果是 "Found Omega at:4294967295"。
	*/
	return 0; 
} 

15. find_first_of

语法:

  • size_type find_first_of(const basic_string &str, size_type index = 0);
  • size_type find_first_of(const char *str, size_type index = 0);
  • size_type find_first_of(const char *str, size_type index, size_type num);
  • size_type find_first_of(char ch, size_type index = 0);

find_first_of()函数:

  • 查找在字符串中第一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,如果没找到就返回string::npos
  • 查找在字符串中第一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,最多搜索num给字符。如果没找到就返回string::npos
  • 查找在字符串中第一个与ch匹配的字符,返回它的位置。搜索从index开始

16. find_first_not_of

语法:

size_type find_first_not_of(const basic_string &str, size_type index = 0);

size_type find_first_not_of(const char *str, size_type index = 0);

size_type find_first_not_of(const char *str, size_type index, size_type num);

size_type find_first_not_of(char ch, size_type index = 0);

find_first_not_of()函数:

  • 在字符串中查找第一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::npos
  • 在字符串中查找第一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始,最多查找num个字符。如果没找到就返回string::npos
  • 在字符串中查找第一个与ch不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::npos

17. find_last_of

语法:

  • size_type find_last_of( const basic_string &str, size_type index = npos );
  • size_type find_last_of( const char *str, size_type index = npos );
  • size_type find_last_of( const char *str, size_type index, size_type num );
  • size_type find_last_of( char ch, size_type index = npos );

find_last_of()函数:

  • 在字符串中查找最后一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops
  • 在字符串中查找最后一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,最多搜索num个字符。如果没找到就返回string::nops
  • 在字符串中查找最后一个与ch匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops

18. find_last_not_of

语法:

  • size_type find_last_not_of( const basic_string &str, size_type index = npos );
  • size_type find_last_not_of( const char *str, size_type index = npos);
  • size_type find_last_not_of( const char *str, size_type index, size_type num );
  • size_type find_last_not_of( char ch, size_type index = npos );

find_last_not_of()函数:

  • 在字符串中查找最后一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops
  • 在字符串中查找最后一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始,最多查找num个字符如果没找到就返回string::nops
  • 在字符串中查找最后一个与ch不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops

19. get_allocator

语法:

allocator_type get_allocator();

get_allocator()函数返回本字符串的配置器。

20. 插入(insert)

语法:

  • iterator insert(iterator i, const char &ch);
  • basic_string &insert(size_type index, const basic_string &str);
  • basic_string &insert(size_type index, const char *str);
  • basic_string &insert(size_type index1, const basic_string &str, size_type index2, size_type num);
  • basic_string &insert(size_type index, const char *str, size_type num);
  • basic_string &insert(size_type index, size_type num, char ch);
  • void insert(iterator i, size_type num, const char &ch);
  • void insert(iterator i, iterator start, iterator end);

insert()函数的功能非常多:

  • 在迭代器i表示的位置前面插入一个字符ch
  • 在字符串的位置index插入字符串str
  • 在字符串的位置index插入字符串str的子串(从index2开始,长num个字符)
  • 在字符串的位置index插入字符串str的num个字符
  • 在字符串的位置index插入num个字符ch的拷贝
  • 在迭代器i表示的位置前面插入num个字符ch的拷贝
  • 在迭代器i表示的位置前面插入一段字符,从start开始,以end结束

21. 长度(length)

语法:

size_type length();

length()函数返回字符串的长度,这个数字应该和size()返回的数字相同

22. max_size

语法:

size_type max_size();

max_size()函数返回字符串能保存的最大字符数

23. rbegin

语法:

const reverse_iterator rbegin();

rbegin()返回一个逆向迭代器,指向字符串的最后一个字符。

24. rend

语法:

const reverse_iterator rend();

rend()函数返回一个逆向迭代器,指向字符串的开头(第一个字符的前一个位置)。

25. 替换(replace)

语法:

  • basic_string &replace(size_type index, size_type num, const basic_string &str);
  • basic_string &replace(size_type index1, size_type num1, const basic_string &str, size_type index2, size_type num2);
  • basic_string &replace(size_type index, size_type num, const char *str);
  • basic_string &replace(size_type index, size_type num1, const char *str, size_type num2);
  • basic_string &replace(size_type index, size_type num1, size_type num2, char ch);
  • basic_string &replace(iterator start, iterator end, const basic_string &str);
  • basic_string &replace(iterator start, iterator end, const char *str);
  • basic_string &replace(iterator start, iterator end, const char *str, size_type num);
  • basic_string &replace(iterator start, iterator end, size_type num, char ch);

replace()函数:

  • 用str中num个字符替换本字符串中的字符,从index开始
  • 用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,最多num1个字符
  • 用str中的num个字符(从index开始)替换本字符串中的字符
  • 用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,num1个字符
  • 用num2个ch字符替换本字符串中的字符,从index开始
  • 用str中的字符替换本字符串中的字符,迭代器start和end指示范围
  • 用str中num个字符替换本字符串中的内容,迭代器start和end指示范围
  • 用num个ch字符替换本字符串中的内容,迭代器start和end指示范围

26. 保留空间(reserve)

语法:

void reserve(size_type num);

reserve()函数设置本字符串的capacity以保留num个字符空间。

27. resize

语法:

  • void resize(size_type num);
  • void resize(size_type num, char ch);

resize()函数改变本字符串中的大小到num,新空间的内容不确定。也可以指定用ch填充。

28. rfind

语法:

  • size_type rfind(const basic_string &str, size_type index);
  • size_type rfind(const char *str, size_type index);
  • size_type rfind(const char *str, size_type index, size_type  num);
  • size_type rfind(char ch, size_type index);

rfind()函数:

  • 返回最后一个与str中的某个字符匹配的字符,从index开始查找。如果没找到就返回string::npos
  • 返回最后一个与str中的某个字符匹配的字符,从index开始查找,最多查找num个字符。如果没找到就返回string::npos
  • 返回最后一个与ch匹配的字符,从index开始查找。如果没有找到就返回string::npos

29. size

语法:

size_type size();

size()函数返回字符串中现在拥有的字符数。

30. substr

语法:

basic_string substr(size_type index, size_type num = npos);

substr()返回本字符串中的一个子串,从index开始,长num个字符。如果没有指定,将是默认值string::npos。这样,substr()函数将简单的返回从index开始的剩余的字符串。

#include<iostream>
using namespace std;

int main() {
	string s = "What we have here is a failure to communicate";
	string sub = s.substr(21);
	cout << "The original string is: " << s << endl;
	cout << "The substring is: " << sub << endl;
	/* 
	The original string is: What we have here is a failure to communicate
	The substring is: a failure to communicate
	*/
	return 0; 
} 

31. 交换(swap)

语法:

void swap(basic_string &str);

swap()函数把str和本字符串交换。

#include<iostream>
using namespace std;

int main() {
	string first = "This comes first";
	string second = "This comes second";
	first.swap(second);
	cout << "first: " << first << endl;
	cout << "second: " << second <<endl;
	/*
	first: This comes second
	second: This comes first	
	*/
	return 0; 
} 
  • 14
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值