C++——string的简介及使用

string类对象的常见构造

类对象的常见构造
在这里插入图片描述

实例:

	string s1;						// 构造空字符串
	string s2("hello world");		// 拷贝"hello world"
	string s3("hello world", 4);	// 拷贝"hello world"前4个字符
	string s4(s2);					// 拷贝构造s2

string类对象的容量操作

在这里插入图片描述
注意:

  1. size()与length()方法底层实现原理完全相同,引入size()的原因是为了与其他容器的接口保持一致,一般情况下基本都是用size()。
  2. clear()只是将string中有效字符清空,不改变底层空间大小。
  3. resize(size_t n) 与 resize(size_t n, char c)都是将字符串中有效字符个数改变到n个,不同的是当字符个数增多时:resize(n)用0来填充多出的元素空间,resize(size_t n, char c)用字符c来填充多出的元素空间。注意:resize在改变元素个数时,如果是将元素个数增多,可能会改变底层容量的大小,如果是将元素个数减少,底层空间总大小不变。
  4. reserve(size_t res_arg=0):为string预留空间,不改变有效元素个数,当reserve的参数小于string的底层空间总大小时,reserver不会改变容量大小。
void main()
{
	// string类对象支持直接用cin和cout进行输入和输出
	string s("hello, bit!!!");
	cout << s.size() << endl;     
	cout << s.length() << endl;
	cout << s.capacity() << endl;
	cout<<endl;

	// 将s中的字符串清空,注意清空时只是将size清0,不改变底层空间的大小
	s.clear();
	cout << s.size() << endl;
	cout << s.capacity() << endl;
}

在这里插入图片描述

int main()
{
	// 将s中有效字符个数增加到10个,多出位置用'a'进行填充
	// “aaaaaaaaaa”
	string s("hello, bit!!!");
	s.resize(10, 'a');
	cout << s.size() << endl;
	cout << s.capacity() << endl;
	cout << endl;

	// 将s中有效字符个数增加到15个,多出位置用缺省值'\0'进行填充
	// "aaaaaaaaaa\0\0\0\0\0"
	// 注意此时s中有效字符个数已经增加到15个
	s.resize(15);
	cout << s.size() << endl;
	cout << s.capacity() << endl;
	cout << endl;

	// 将s中有效字符个数缩小到5个,底层空间没变
	s.resize(5);
	cout << s.size() << endl;
	cout << s.capacity() << endl;
}

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

void TestPushBack()
{
	string s;
	size_t sz = s.capacity();
	cout << "making s grow:\n" << sz << endl;
	for (int i = 0; i < 100; ++i)
	{
		s.push_back('c');
		if (sz != s.capacity())
		{
			sz = s.capacity();
			cout << "capacity changed: " << sz << '\n';
		}
	}
}
void TestPushBackReserve()
{
	string s;
	// 利用reserve提高插入数据的效率,避免增容带来的开销
	s.reserve(100);
	size_t sz = s.capacity();
	cout << "making s grow:\n";
	for (int i = 0; i < 100; ++i)
	{
		s.push_back('c');
		if (sz != s.capacity())
		{
			sz = s.capacity();
			cout << "capacity changed: " << sz << '\n';
		}
	}
}
int main()
{
	TestPushBack();
	TestPushBackReserve();
}

在这里插入图片描述

reserve和resize的区别

reserve的作用,如果我们知道需要多少空间,直接一次性开好, 避免增容,提高效率。
resize的作用,既要开好空间,还要对这些空间初始化。

string类对象的访问及遍历操作

在这里插入图片描述

void Teststring()
{
	string s("hello world");
	// 3种遍历方式:
	// 需要注意的以下三种方式除了遍历string对象,还可以遍历是修改string中的字符,
	// 另外以下三种方式对于string而言,第一种使用最多
	// 1. for+operator[]
	for (size_t i = 0; i < s.size(); ++i)
		cout << s[i] ;
	cout << endl;

	// 2.迭代器
	string::iterator it = s.begin();
	while (it != s.end())
	{
		cout << *it;
		++it;
	}
	cout << endl;

	// 从后往前遍历
	string::reverse_iterator rit = s.rbegin();
	while (rit != s.rend())
	{
		cout << *rit ;
		rit++;
	}
	cout << endl;
	// 3.范围for
	for (auto ch : s)
		cout << ch ;
	cout << endl;
}

在这里插入图片描述

string类对象的修改操作

在这里插入图片描述
注意:

  1. 在string尾部追加字符时,s.push_back(c) / s.append(1, c) / s += 'c’三种的实现方式差不多,一般情况下string类的+=操作用的比较多,+=操作不仅可以连接单个字符,还可以连接字符串。
  2. 对string操作时,如果能够大概预估到放多少字符,可以先通过reserve把空间预留好。
void Teststring()
{
	string str("hello");
	str.push_back(' ');		// 在str后插入空格
	str.append("world");	// 在str后追加一个字符串"world"
	str += '!';				// 在str后追加一个字符'!'
	cout << str << endl;
	cout << str.c_str() << endl;  // 以C语言的方式打印字符串
	cout << endl;

	// 获取file的后缀
	string file1("string.cpp");
	size_t pos = file1.rfind('.');// 从后往前,找到.的位置
	cout << pos << endl;
	string suffix(file1.substr(pos, file1.size() - pos));// 获取file的后缀
	cout << suffix << endl;
	cout << endl;

	// npos是string里面的一个静态成员变量
	// static const size_t npos = -1;

	// 取出url中的协议,域名
	string url("http://www.cplusplus.com/reference/string/string/find/");
	cout << url << endl;
	size_t il = url.find("://");
	if (il != string::npos)
	{
		string protocol = url.substr(0, il - 0);// 找出协议
		cout << "protocol:" << protocol << endl;
	}

	size_t i2 = url.find('/', il + 3);// 从il+3开始找到'/'
	if (i2 != string::npos)
	{
		string domain = url.substr(il + 3, i2 - (il + 3));// 找出域名
		cout << "domain:" << domain << endl;
	}

	string uri = url.substr(i2);// 从i2找到结尾
	cout << "uri:" << uri<<endl;
}

在这里插入图片描述

除了上述操作外还有insert(插入)/erase(删除)等操作

void test_string5()
{
	string s1("world");

	// 能不用就不要用,因为insert和erase在头部或者中间等位置插入删除
	// 需要挪动数据,效率低下,尽量少用。了解即可
	s1.insert(0, "hello ");// 在头部插入数据
	cout << s1 << endl;
	cout << "------------------------------------" << endl;
	
	string s2("hello world");
	cout << s2 << endl;
	s2.erase(5, 2);// 删除第5个元素后面2个元素
	cout << s2 << endl;
	s2.erase(5, 20);// 删除第5个元素后面20个元素
	cout << s2 << endl;
	s2.erase(5);// 删除第5个元素后面所有元素
	cout << s2 << endl;
}

在这里插入图片描述

getline函数

使用>>进行输入操作时,当>>读取到空格便会停止读取,所以我们不能用>>将一串含有空格的字符串读入到string对象中。

int main()
{
	string s;
	cin >> s; 
	cout << s << endl; 
	return 0;
}

在这里插入图片描述
如何解决这个问题呢?
这时就要用到getline函数了,getline函数可以完成一串含有空格的字符串的读取操作

用法一:

istream& getline (istream& is, string& str);

getline函数将从is中提取到的字符存储到str中,直到读取到换行符’\n’为止。

int main()
{
	string s;
	getline(cin, s); 
	cout << s << endl;
	return 0;
}

在这里插入图片描述
用法二:

istream& getline (istream& is, string& str, char delim);

getline函数将从is中提取到的字符存储到str中,直到读取到分隔符delim为止。

int main()
{
	string s;
	getline(cin, s, 'r'); 
	cout << s << endl; 
	return 0;
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值