string类及一些常用接口

string中的常用构造方法
  1. string(); 构造一个空字符串。
  2. string(const char* s);用C风格字符串构造string类的对象
  3. string(size_t n,char c);构造一个含有n个字符c的字符串。
  4. string(const string& s);拷贝构造方法。
//构造方法
void Teststring1() {
	string s;
	
	string s1("hello world");
	cout << s1 << endl;
	
	const char* p = "hello world";
	string s2(p);
	cout << s2 << endl;
	
	string s3(s1);
	cout << s3 << endl;
	
	string s4(10, 'A');
	cout << s4 << endl;
}
遍历的常用方法
  1. 下标访问操作符
void Teststring2() {
	string s1("hello");
	for (size_t i = 0; i < s1.size(); i++) {
		cout << s1[i];
	}
	cout << endl;
}
  1. 迭代器
void Teststring3() {
	//正向迭代器
	string s1("hello");
	string::iterator it = s1.begin();
	while (it != s1.end()) {
		cout << *it;
		it++;
	}
	cout << endl;

	//反向迭代器
	string::reverse_iterator rit = s1.rbegin();
	while (rit != s1.rend())
	{
		cout << *rit;
		++rit;
	}
	cout << endl;
}
  1. 范围for
void Teststring4() {
	//范围for
	string s1("hello");
	for (auto e : s1) {
		cout << e;
	}
	cout << endl;
}
string中对容量操作的常用方法
  1. size_t size() const返回字符串的有效长度.
  2. size_t length() const也是返回字符串的有效字符长度
  3. size_t capacity()const返回的是对象总容量的大小
  4. bool empt()const检测字符串是否为空
  5. void clear()清空字符串中的有效字符
  6. void resize(size_t n , char c)将字符串中的有效字符改成n个,多余的用字符c填充,也可以不传默认用\0填充。
  7. void reverse(size_t n = 0)将字符串容量大小调整到n
void Teststring5() {
	string s1("hello");
	cout << s1.size() << endl;
	cout << s1.capacity() << endl;
	
	if (s1.empty()) {
		cout << "空"<< endl;
	}
	else {
		cout << "不空"<< endl;
	}
	
	s1.clear();
	//size被清空 capacity没变
	cout << s1.size() << endl;
	cout << s1.capacity() << endl;
}
void Teststring6() {
	string s1("hello world");
	cout << s1 << endl;
	cout << s1.size() << endl;
	cout << s1.capacity() << endl;	
	
	s1.resize(20, '!');
	cout << s1 << endl;
	cout << s1.size() << endl;
	cout << s1.capacity() << endl;
	
	s1.resize(60, '!');
	cout << s1 << endl;
	cout << s1.size() << endl;
	cout << s1.capacity() << endl;
}

容量不够时会自动扩容,不同编译器内部扩容规则不太一样,容量够不会改变capacity的大小。

void Teststring7() {
	string s1("hello");
	cout << s1 << endl;
	cout << s1.size() << endl;
	cout << s1.capacity() << endl;
	
	s1.reserve(10);
	cout << s1 << endl;
	cout << s1.size() << endl;
	cout << s1.capacity() << endl;
	
	s1.reserve(20);
	cout << s1 << endl;
	cout << s1.size() << endl;
	cout << s1.capacity() << endl;
	
	s1.reserve(100);
	cout << s1 << endl;
	cout << s1.size() << endl;
	cout << s1.capacity() << endl;
	
	//15
	s1.reserve(15);
	cout << s1 << endl;
	cout << s1.size() << endl;
	cout << s1.capacity() << endl;
	cout << sizeof(string) << endl;
}

接收的参数大于内部的容量时会扩容,如果是缩减时,当参数大于15时,内部容量不会改变,当参数小于等于15时,容量缩小为15。(vs2019)

string改变字符串内容的常用方法
  1. void push_back (char c);在末尾插入字符c

  2. string& append (const string& str);
    string& append (const char* s);
    string& append (const char* s, size_t n);
    string& append (size_t n, char c);
    在字符串末尾追加字符或者字符串

  3. operator+= 在字符串末尾追加字符或者字符串

  4. size_t find (const string& str, size_t pos = 0) const;
    size_t find (char c, size_t pos = 0) const从pos
    从pos位置向后查找字符或者字符串,返回在原字符串中的位置。

  5. size_t rfind (const string& str, size_t pos = npos) const;
    size_t rfind (char c, size_t pos = npos) const
    从pos位置向前查找字符或者字符串,返回在原字符串中的位置。

  6. string substr (size_t pos = 0, size_t len = npos) const;
    从pos位置开始截取n个字符返回截取到的字符串。

  7. string& erase (size_t pos = 0, size_t len = npos);
    从pos位置开始删除多长的元素。

void Teststring8() {
	string s1("hello");
	s1 += 'c';
	cout << s1 << endl;
	s1 += "world";
	cout << s1 << endl;
	s1.append(3,'!');
	cout << s1 << endl;
	string s2("world");
	s2 = s2.append(s2, 1, 3);
	cout << s2 << endl;
	string s4("hello");
}
//insert
void Teststring9() {
	string s("world");
	s.insert(0, "hello");
	cout << s << endl;
	s.insert(4, 1, ' ');
	cout << s << endl;
}
//erase
void Teststring10() {
	string s("hello");
	s.erase(0,1);
	cout << s << endl;
	s.erase(s.begin());
	cout << s << endl;
	s.erase(s.begin(), s.end());
	cout << s << endl;
}
void Teststring11()
{
	string s("12345");
	//c风格字符串
	int value = atoi(s.c_str());
	cout << value << endl;
}
void Teststring12() {
	string s("lol.exe");
	string suffix = s.substr(s.find('.')+1);
	cout << suffix << endl;
	string s1("F:\\Game\\lol.exe");
	size_t pos1 = s1.rfind('\\')+1;
	size_t pos2 = s1.rfind('.')-1;
	string filename = s1.substr(pos1, pos2-pos1);
	cout << filename << endl;
}
void Teststring13() {
	string s1("abcdefgh");
	size_t n = s1.find("efg", 0);
	cout << n << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值