【STL】string的常用函数用法总结

目录

string常见构造

string容量操作

 string遍历操作

 string对象的修改操作

 string查找操作


string常见构造

string()构造空的string类对象,即空指针string s1;
string(const char* s)用c_string构造string类对象string s1("hello world");
string(const string&s)拷贝构造函数string s2(s1);
string(size_t n,char c)strring类对象中包含n个字符cstring s1(5,'1');
//构造函数
string::string(const char* str) 
	:_str(new char(strlen(str)+1))
	,_size(strlen(str))
	,_capacity(strlen(str))
{
	strcpy(_str, str);
}
//析构函数
string::~string()
{
	delete[]_str;
	_str = nullptr;
	_size = 0;
	_capacity = 0;
}

//拷贝构造函数
string::string(const string& s)
{
	_str = new char(s._capacity + 1);
	strcpy(_str, s._str);
	_size = s._size;
	_capacity = s._capacity;
}

string容量操作

size返回字符串有效字符长度
length返回字符串有效字符长度
capacity返回空间总大小

注:在string类中size和length是等效的;

string s1("hello world");
cout << s1.size() << endl;
cout << s1.length() << endl;
cout << s1.capacity() << endl;

运行结果:

 

reserve为字符串预留空间(扩容)
resize将有效字符的个数为n个,多出的空间用字符c填充(扩容+尾插)

 reserve:

reserve(size_t res_arg=0):为string预留空间,不改变有效元素个数,当reserve的参数小于
string的底层空间总大小时,reserver不会改变容量大小。

	string s1("hello world");

	cout << s1 << endl;
	cout << s1.capacity() << endl;

	s1.reserve(100);
	cout << s1 << endl;
	cout << s1.capacity() << endl;

 运行结果:

resize:

resize(size_t n) 与 resize(size_t n, char c)都是将字符串中有效字符个数改变到n个,不同的是当字
符个数增多时:resize(n)用0来填充多出的元素空间,resize(size_t n, char c)用字符c来填充多出的
元素空间。注意:resize在改变元素个数时,如果是将元素个数增多,可能会改变底层容量的大
小,如果是将元素个数减少,底层空间总大小不变。

	string s1("hello world");

	cout << s1 << endl;
	cout << s1.capacity() << endl;

	s1.resize(20, 'x');
	cout << s1 << endl;
	cout << s1.capacity() << endl;

运行结果:

 string遍历操作

 

operator[]返回pos位置的字符
begin,endbegin获取一个字符的迭代器 + end获取最后一个字符下一个位置的迭代器
范围forauto
//operator[]
for (size_t i = 0; i < s1.size(); i++) {
	cout << s1[i] << " ";
	}
	cout << endl;
//迭代器
string::iterator it = s1.begin();
	while (it != s1.end()) {
		cout << *it << " ";
		it++;
	}
	cout << endl;
//返回for
for (auto e : s1) {
	cout << e << " ";
	}
	cout << endl;

 运行结果:

 string对象的修改操作

push_back尾插一个字符
append尾插一个字符串
insert选择位置插入
operator+=在字符串后加字符串str
	string s1("hello world");
	cout << s1 << endl;

	s1.push_back('x');
	cout << s1 << endl;

	s1.append("aaaa");
	cout << s1 << endl;

	s1.insert(3, "X");
	cout << s1 << endl;

	s1.insert(1, "WWW");
	cout << s1 << endl;

	s1 += "QQ";
	cout << s1 << endl;

运行结果:

 string查找操作

find+npos从字符串pos位置开始往找字符c,返回该字符在字符串中的位置
rfind从字符串pos位置开始往找字符c,返回该字符在字符串中的位置
substr从str中pos位置开始,截取n个字符,然后将其返回

 find:

	string s1("hello world AXC AA IS da");
	cout << s1 << endl;

	size_t t = s1.find("o");
	cout << t << endl;

	t = s1.find("o",6);
	cout << t << endl;

 运行结果:

 rfind:

	string s1("hello world AXC AA IS da o");
	cout << s1 << endl;

	size_t t = s1.rfind("o",5);
	cout << t << endl;

	t = s1.rfind("o", 10);
	cout << t << endl;

	t = s1.rfind("o");
	cout << t << endl;

 运行结果:

 substr:

	string s1("hello world AXC AA IS da o");
	cout << s1 << endl;

	string s2 = s1.substr(3, 6);
	cout << s2 << endl;

运行结果:

 手动实现一个简单的string类:

string.h:

#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;

namespace bit {
	class string {
	public:
		string(const char* str="");
		~string();
		const char* c_str() const;
		
		size_t size() const;
		char& operator[](size_t pos);

		typedef char* iterator;
		iterator begin();
		iterator end();

		void reserve(size_t n);
		void push_back(char ch);
		void append(const char* str);

		string& operator+=(char ch);
		string& operator+=(const char* str);

		void insert(size_t pos, char ch);
		void insert(size_t pos, const char* str);

		void erase(size_t pos,size_t len=npos);
		const static size_t npos = -1;

		size_t find(char ch, size_t pos = 0);
		size_t find(const char* str, size_t pos = 0);

		string(const string& s);

		string& operator=(const string& s);

		void swap(string& s);

		string substr(size_t pos = 0, size_t len=npos);

		bool operator<(const string& s)const;
		bool operator>(const string& s)const;
		bool operator==(const string& s)const;
		bool operator<=(const string& s)const;
		bool operator>=(const string& s)const;
		bool operator!=(const string& s)const;
	private:
		char* _str;
		size_t _size;
		size_t _capacity;
	};
}
istream& operator >>(istream& is, string& str);
ostream& operator<<(ostream& os, const string& str);

 string.cpp:

#include"string.h"

namespace bit {
	string::string(const char* str) 
		:_str(new char[strlen(str)+1])
		,_size(strlen(str))
		,_capacity(strlen(str))
	{
		strcpy(_str, str);
	}
	string::~string() {
		delete[] _str;
		_str = nullptr;
		_size = 0;
		_capacity = 0;
	}
	const char* string::c_str() const
	{
		return _str;
	}
	size_t string::size() const
	{
		return _size;
	}
	char& string::operator[](size_t pos)
	{
		assert(pos < _size);
		return _str[pos];
	}


	string::iterator string::begin()
	{
		return _str;
	}
	string::iterator string::end()
	{
		return _str + _size;
	}


	void string::reserve(size_t n)
	{
		if (n > _capacity) {
			//扩容
			char* tmp = new char[n + 1];
			strcpy(tmp, _str);
			delete[]_str;
			_str = tmp;
			_capacity = n;
		}
	}

	void string::push_back(char ch)
	{
		size_t newcapacity = _capacity == 0 ? 4 : _capacity * 2;
		reserve(newcapacity);
		_str[_size] = ch;
		_str[_size + 1] = '\0';
		++_size;
	}
	void string::append(const char* str)
	{
		size_t len = strlen(str);
		if (_size + len > _capacity)
		{
			reserve(_size + len);
		}
		//strcat(_str, str);
		strcpy(_str + _size, str);
		_size += len;
	}

	string& string::operator+=(char ch)
	{
		push_back(ch);
		return *this;
	}

	string& string::operator+=(const char* str)
	{
		append(str);
		return *this;
	}

	void string::insert(size_t pos, char ch)
	{
		if (_size == _capacity) {
			size_t newcapacity = _capacity == 0 ? 4 : _capacity * 2;
			reserve(newcapacity);
		}
		//int end = _size;
		//while (end >= (int)pos) {
		//	_str[end+1] = _str[end];
		//	end--;
		//}
		size_t end = _size+1;
		while (end > pos) {
			_str[end] = _str[end-1];
			end--;
		}
		_str[pos] = ch;
		++_size;
	}

	void string::insert(size_t pos, const char* str)
	{
		size_t len = strlen(str);
		if (_size + len > _capacity) {
			reserve(_size + len);
		}
		int end = _size;
		while (end >=(int)pos) {
			_str[end+len] = _str[end];
			end--;
		}
		memcpy(_str + pos, str, len);
		_size += len;
	}

	void string::erase(size_t pos, size_t len)
	{
		if (len == npos||pos+len>=_size) {
			_str[pos] = '\0';
			_size = pos;
		}
		else {
			strcpy(_str + pos, _str + pos + len);
			_size -= len;
		}
	}

	size_t string::find(char ch, size_t pos)
	{
		for (size_t i = pos; i < _size; i++) {
			if (_str[i] == ch)return i;
		}
		return npos;
	}

	size_t string::find(const char* str, size_t pos)
	{
		char*p=strstr(_str + pos, str);
		return p-_str;
	}

	string::string(const string& s)
	{
		_str = new char[s._capacity + 1];
		strcpy(_str, s._str);
		_size = s._size;
		_capacity = s._capacity;
	}

	string& string::operator=(const string& s)
	{
		char* tmp = new char[s._capacity + 1];
		strcpy(tmp, s._str);
		delete[] _str;
		_str = tmp;
		
		_size = s._size;
		_capacity = s._capacity;

		return* this;
	}

	void string::swap(string& s)
	{
		std::swap(_str, s._str);
		std::swap(_size, s._size);
		std::swap(_capacity, s._capacity);
	}

	string string::substr(size_t pos, size_t len)
	{
		//大于后面字符串的长度,有多少取多少;
		if (len + pos > _size)
		{
			string s1(_str + pos);
			return s1;
		}
		else {
			string s1;
			s1.reserve(len);
			for (size_t i = pos; i < pos + len; i++)
			{
				s1 += _str[i];
			}
			return s1;
		}
	}

	bool string::operator<(const string& s) const
	{
		return strcmp(_str, s._str)<0;
	}

	bool string::operator>(const string& s) const
	{
		return !(*this<=s);
	}

	bool string::operator==(const string& s) const
	{
		return strcmp(_str, s._str)==0;
	}

	bool string::operator<=(const string& s) const
	{
		return (*this < s || *this == s);
	}

	bool string::operator>=(const string& s) const
	{
		return !(*this<s);
	}

	bool string::operator!=(const string& s) const
	{
		return !(*this == s);
	}


}

 

以上就是string类的内容!!

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值