赞
踩
C语言中,字符串是以’\0’结尾的一些字符的集合,为了操作方便,C标准库中提供了一些str系列的库函数,但是这些库函数与字符串是分离开的,不太符合OOP的思想,而且底层空间需要用户自己管理,稍不留神可能还会越界访问,所以由于C语言中的库函数使用起来较为麻烦,C++的STL中提供了一个容器——string
在使用string类时,必须包含#include头文件以及using namespace std;
void Teststring()
{
string s1; // 构造空的string类对象s1
string s2("hello bit"); // 用C格式字符串构造string类对象s2
string s3(s2); // 拷贝构造s3
}
注意:
- size()与length()方法底层实现原理完全相同,引入size()的原因是为了与其他容器的接口保持一致,一般情况下基本都是用size()
- clear()只是将string中有效字符清空,不改变底层空间大小
- resize(size_t n) 与 resize(size_t n, char c)都是将字符串中有效字符个数改变到n个,不同的是当字符个数增多时:resize(n)用0来填充多出的元素空间,resize(size_t n, char c)用字符c来填充多出的元素空间
注意:resize在改变元素个数时,如果是将元素个数增多,可能会改变底层容量的大小,如果是将元素个数减少,底层空间总大小不变- reserve(size_t res_arg=0):为string预留空间,不改变有效元素个数,当reserve的参数小于string的底层空间总大小时,reserver不会改变容量大小
注意:
- 在string尾部追加字符时,s.push_back ( c ) / s.append(1, c) / s += 'c’三种的实现方式差不多,一般情况下string类的+=操作用的比较多,+=操作不仅可以连接单个字符,还可以连接字符串
- 对string操作时,如果能够大概预估到放多少字符,可以先通过reserve把空间预留好
#include <iostream> #include <assert.h> using namespace std; namespace Tlzns { class string { friend ostream& operator<<(ostream& _cout, const Tlzns::string& s); friend istream& operator>>(istream& _cin, Tlzns::string& s); public: typedef char* iterator; typedef const char* const_iterator; string(const char* str = "") :_size(strlen(str)) ,_capacity(_size) { _str = new char[_capacity + 1]; strcpy(_str, str); } string(const string& s) :_str(nullptr) ,_size(s._size) ,_capacity(s._capacity) { _str = new char[_capacity + 1]; strcpy(_str, s._str); } string& operator=(const string& s) { string tmp(s); swap(tmp); return *this; } ~string() { _size = 0; _capacity = 0; delete[] _str; _str = nullptr; } // // iterator iterator begin() { return _str; } iterator end() { return _str + _size; } iterator begin() const { return _str; } iterator end() const { return _str + _size; } // modify void push_back(char c) { if (_size == _capacity) { reserve(_capacity == 0 ? 4 : _capacity * 2); } _str[_size] = c; _size++; _str[_size] = '\0'; } string& operator+=(char c) { push_back(c); return *this; } //void append(const char* str) //{ // size_t len = strlen(str); // if (_size + len > _capacity) // { // reserve(_size + len); // } // strcpy(_str + _size, str); // _size += len; // //} void append(const char* str) { insert(_size, str); } string& operator+=(const char* str) { append(str); return *this; } void clear() { _str[0] = '\0'; _size = 0; } void swap(string& s) { std::swap(_str, s._str); std::swap(_size, s._size); std::swap(_capacity, s._capacity); } const char* c_str()const { return _str; } // capacity size_t size()const { return _size; } size_t capacity()const { return _capacity; } bool empty()const { if (_size == 0) { return true; } else { return false; } } void resize(size_t n, char c = '\0') { if (n > _capacity) { reserve(n); while (_size < n) { _str[_size] = c; _size++; } _str[_size] = '\0'; } else { _size = n; _str[_size] = '\0'; } } void reserve(size_t n) { if (n > _capacity) { char* tmp = new char[n + 1]; strcpy(tmp, _str); delete[]_str; _str = tmp; _capacity = n; } } // access char& operator[](size_t index) { assert(index < _size); return _str[index]; } const char& operator[](size_t index)const { assert(index < _size); return _str[index]; } //relational operators bool operator<(const string& s) { return (strcmp(_str, s._str) < 0); } bool operator<=(const string& s) { return (*this < s || *this == s); } bool operator>(const string& s) { return (!(*this <= s)); } bool operator>=(const string& s) { return (!(*this < s)); } bool operator==(const string& s) { return (strcmp(_str, s._str) == 0); } bool operator!=(const string& s) { return (!(*this == s)); } // 返回c在string中第一次出现的位置 size_t find(char c, size_t pos = 0) const { for (size_t i = pos; i < _size; i++) { if (_str[i] == c) { return i; } } return npos; } // 返回子串s在string中第一次出现的位置 size_t find(const char* s, size_t pos = 0) const { char* p = strstr(_str, s); if (p) { return p - _str; } else { return npos; } } // 在pos位置上插入字符c/字符串str,并返回该字符的位置 string& insert(size_t pos, char c) { assert(pos <= _size); if (_size == _capacity) { reserve(_capacity == 0 ? 4 : _capacity * 2); } size_t end = _size + 1; while (end > pos) { _str[end] = _str[end - 1]; end--; } _str[pos] = c; _size++; return *this; } string& insert(size_t pos, const char* str) { assert(pos <= _size); size_t len = strlen(str); if (_size + len > _capacity) { reserve(_size + len); } size_t end = _size + len; while (end > pos) { _str[end] = _str[end - len]; end--; } strncpy(_str + pos, str, len); _size += len; return *this; } private: char* _str; size_t _size; size_t _capacity; public: const static size_t npos; }; const size_t Tlzns::string::npos = -1; ostream& operator<<(ostream& _cout, const Tlzns::string& s) { for (char ch : s) { cout << ch; } return _cout; } //istream& operator>>(istream& _cin, Tlzns::string& s) //{ // s.clear(); // s.reserve(128); // // char ch = _cin.get(); // // while (ch != ' ' && ch != '\n') // { // s += ch; // ch = _cin.get(); // } // // return _cin; //} istream& operator>>(istream& _cin, Tlzns::string& s) { s.clear(); char c[129] = "0"; char ch = _cin.get(); int i = 0; while (ch != ' ' && ch != '\n') { c[i++] = ch; ch = _cin.get(); if (i == 128) { c[i] = '\0'; s += c; i = 0; } } if (i != 0) { c[i] = '\0'; s += c; } return _cin; } void test_string1() { string s1("hello world"); cout << s1.c_str() << endl; string s2; cout << s2.c_str() << endl; for (size_t i = 0; i < s1.size(); i++) { cout << s1[i] << " "; } cout << endl; string::iterator it = s1.begin(); while (it != s1.end()) { (*it)++; cout << *it << " "; ++it; } cout << endl; for (auto ch : s1) { ch++; cout << ch << " "; } cout << endl; cout << s1.c_str() << endl; } void test_string2() { string s1("hello world"); cout << s1.c_str() << endl; s1.push_back(' '); s1.append("hello bit hello bit"); cout << s1.c_str() << endl; s1 += '#'; s1 += "*********************"; cout << s1.c_str() << endl; string s2; s2 += '#'; s2 += "*********************"; cout << s2.c_str() << endl; } void test_string3() { string s1("hello world"); cout << s1.c_str() << endl; s1.insert(5, '%'); cout << s1.c_str() << endl; s1.insert(s1.size(), '%'); cout << s1.c_str() << endl; s1.insert(0, '%'); cout << s1.c_str() << endl; } void test_string4() { string s1("hello world"); string s2("hello world"); cout << (s1 >= s2) << endl; s1[0] = 'z'; cout << (s1 >= s2) << endl; cout << s2 << endl; cin >> s1; cout << s1 << endl; //char ch1, ch2; //cin >> ch1 >> ch2; //cout << ch1 << endl; //cout << ch2 << endl; } void test_string6() { string s1("hello world"); cout << s1 << endl; s1.resize(5); cout << s1 << endl; s1.resize(25, 'x'); cout << s1 << endl; } void test_string8() { string s1("hello world"); string s2 = s1; cout << s1 << endl; cout << s2 << endl; string s3("xxxxxxxxxxxxxxxxxxx"); s2 = s3; cout << s2 << endl; cout << s3 << endl; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。