天天看點

STL—string容器使用及模拟實作

一. string介紹。

  • C語言中,字元串是以’\0’結尾的一些字元的集合,為了操作友善,C标準庫中提供了一些str系列的庫函數,但是這些庫函數與字元串是分離開的,不太符合OOP(面向對象的程式設計)的思想,而且底層空間需要使用者自己管理,稍不留神可能還會發生越界通路。

string類:

  1. string是表示字元串的字元串類。
  2. 該類的接口與正常容器的接口基本相同,再添加了一些專門用來操作string的正常操作。
  3. string在底層實際是:basic_string模闆類的别名,typedef basic_string<char, char_traits, allocator>

    string;

  4. 不能操作多位元組或者變長字元的序列。
  5. 在使用string類時,必須包含#include頭檔案以及using namespace std;

二.string類的常用使用接口。

1.string類的對象的常見構造:
-string() : 構造空的string類對象,即空字元串
-string(const char* s) : 用C-string來構造string類對象
-string(size_t n, char c) :string類對象中包含n個字元c
-string(const string&s) : 拷貝構造函數
           
int main()
{
	string str1;
	string str2("hello world!");
	string str3(5, 'a');
	string str4(str2);
	cout << str2 << endl;
	cout << str3 << endl;
	cout << str4 << endl;
	return 0;
}
           
2.tring類對象的容量操作:
-size: 傳回字元串有效字元長度
-length :傳回字元串有效字元長度
-capacity: 傳回空間總大小
-empty : 檢測字元串釋放為空串,是傳回true,否則	傳回false
-clear : 清空有效字元
-reserve : 為字元串預留白間**
-resize : 将有效字元的個數該成n個,多出的空間用字元c填充
           
int main()
{
	string str1{ "hello world" };//C++11 初始化清單。

	cout << str1.size() << endl;// 11
	cout << str1.length() << endl;// 11
	cout << str1.empty() << endl;// 0 

	cout << str1.capacity() << endl;// 15
	str1.reserve(24);
	cout << str1.capacity() << endl; // 31

	str1.resize(40);

	cout << str1.size() << endl; // 40 
	cout << str1.capacity() << endl; // 47
	cout << str1 << endl; // ...
	//從代碼中可以看出string類的擴容和vector的擴容很像。
	 //resize(size_t n) 與 resize(size_t n, char c)都是将字元串中有效字元個數改變到n個,不同的是當字元個數增多時:resize(n)用0來填充多出的元素空間,
	 //resize(size_t n, char c)用字元c來填充多出的素空間。
	 //注意:resize在改變元素個數時,如果是将元素個數增多,可能會改變底層容量的大小,如果是将元素個數減少,底層空間總大小不變
	str1.clear();
	cout << str1 << endl;
	cout << str1.capacity() << endl; // 47
	cout << str1.size() << endl; // 0
	return 0;
}
           

注意:

  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不會改變容量大小。
3.string類對象的通路及周遊操作:
-operator[] :(重點) 傳回pos位置的字元,const string類對象調用
-begin+ end  :  begin擷取一個字元的疊代器 + end擷取最後一個字元下一個位置的疊代器rbegin + rend begin擷取一個字元的疊代器 + end擷取最後一個字元下一個位置的疊代器
-範圍for : C++11支援更簡潔的範圍for的新周遊方式
           
int main()
{
	string str1("I love you!");

	//1.operator[]
	for (int i = 0; i < str1.size(); ++i)
		cout << str1[i];
	cout << endl;

	//2.疊代器。
	//auto 關鍵字
	auto it = str1.begin();
	while (it != str1.end())
	{
		cout << *it;
		it++;
	}
	cout << endl;
	
	//3.c++11 for 通路
	for (auto &e : str1)
		cout << e;
	cout << endl;
	
	return 0;
}
           
4.string類對象的修改操作:
-push_back :在字元串後尾插字元c
-append :在字元串後追加一個字元串
-operator+=: (重點) 在字元串後追加字元串str
-c_str:(重點) 傳回C格式字元串
-find + npos:(重點) 從字元串pos位置開始往後找字元c,傳回該字元在字元串中的位置
-rfind :從字元串pos位置開始往前找字元c,傳回該字元在字元串中的位置
-substr :在str中從pos位置開始,截取n個字元,然後将其傳回
           
int main()
{
	string str1("hello world!");
	string str2("like ");

	//str1.append("aaa");// 在str1 後追加字元串
	str1.append(str2); // 在str1 後追加 str2
	cout << str1 << endl;

	str1.push_back('c');//追加字元
	cout << str1 << endl;

	str2 += str1;//追加字元串str1
	str2 += 'a';//追加字元
	cout << str2 << endl;

	cout << str2.c_str() << endl;//c方式列印

	// 擷取file的字尾
	string file1("string.cpp");
	size_t pos = file1.rfind('.');//找到pos位置
	string suffix(file1.substr(pos, file1.size() - pos));//截取pos後的字元 .cpp
	cout << suffix << endl;

	// npos是string裡面的一個靜态成員變量
	// static const size_t npos = -1;
	// 取出url中的域名

	string url("http://www.cplusplus.com/reference/string/");
	cout << url << endl;
	size_t start = url.find("://");//找到“://”的位置
	if (start == string::npos)
	{
		cout << "invalid url" << endl;
	}

	start += 3;//跳過://
	size_t finish = url.find('/', start);//找到從start開始的“:/”.
	string address = url.substr(start, finish - start);//從start開始找域名

	cout << address << endl;
	return 0;
}
           

注意:

  1. 在string尾部追加字元時,s.push_back© / s.append(1, c) / s += 'c’三種的實作方式差不多,一般情況下string類的+=操作用的比較多,+=操作不僅可以連接配接單個字元,還可以連接配接字元串。
  2. 對string操作時,如果能夠大概預估到放多少字元,可以先通過reserve把空間預留好。
5.string類非成員函數:
-operator+ : 盡量少用,因為傳值傳回,導緻深拷貝效率低
-operator>> :(重點) 輸入運算符重載
-operator<< :(重點) 輸出運算符重載
-getline :(重點) 擷取一行字元串
-relational operators :(重點) 大小比較
           

三.string的模拟實作:

namespace ice
{
	class string
	{
		friend ostream& operator<<(ostream &out, const string &s);
	public:
		typedef char* iterator;//疊代器
		

	public:
		string(const char* str="")//構造函數
		{
			_size = strlen(str);
			_capacity = _size;
			_str = new char[_capacity + 1];
			strcpy(_str, str);
		}
		string(const string& s)//拷貝構造函數;
			:_str(nullptr),
			_size(0),
			_capacity(0)
		{
			string tem(s);
			this->Swap(tem);
		}

		string operator=(string s)//指派語句
		{
			this->Swap(s);
			return *this;
		}
		
		~string()
		{
			if (_str)
			{
				delete[]_str;
				_str = nullptr;
			}
		}
		
		///
		iterator begin()
		{
			return _str;
		}

		iterator end()
		{
			return _str + _size;
		}

		char operator[](size_t i)
		{
			assert(i<_size);
			return _str[i];
		}

		const char operator[](size_t i)const
		{
			assert(i < _size);
			return _str[i];
		}

		void push_back(char c)
		{
			if (_size == _capacity)
				Reserve(_capacity * 2);
			_str[_size++] = c;
			_str[_size] = '\0';
		}

		size_t size()
		{
			return _size;
		}

		size_t capacity()
		{
			return _capacity;
		}

		void Swap(string& s)//交換函數
		{
			swap(_str, s._str);
			swap(_size, s._size);
			swap(_capacity, s._capacity);
		}
		void Resize(size_t newSize, char c = '\0')
		{
			if (newSize > _size)
			{
				// 如果newSize大于底層空間大小,則需要重新開辟空間
				if (newSize > _capacity)
				{
					Reserve(newSize);//擴容
				}
				memset(_str + _size, c, newSize - _size);
			}
			_size = newSize;
			_str[newSize] = '\0';
		}
		void Reserve(size_t newCapacity)//擴容
		{
			// 如果新容量大于舊容量,則開辟空間
			if(newCapacity > _capacity)
			{
				char* str = new char[newCapacity + 1];
				strcpy(str, _str);//把舊空間的内容拷貝到str;
				// 釋放原來舊空間,然後使用新空間
				delete[] _str;
				_str = str;
				_capacity = newCapacity;
			}
		}

		string& operator+=(char c)
			{
			push_back(c);
			return *this;
		}
		
		const char* c_str()const//  c_str  方法;
		{
			return _str;
		}
	private:
		char* _str;
		size_t _capacity;
		size_t _size;
	};
}
ostream& ice::operator<<(ostream &out, const string &s)
{
	out << s._str;
	return out;
}
           

繼續閱讀