string
#include <iostream>
#include <string>
#if 0
using std::string;
using std::cout;
using std::endl;
void TestString() {
string s1;
string s2("hello");
string s3(s2);
}
void TestString1() {
string s("hello zdz !");
cout << s.size() << endl;
cout << s.length() << endl;
cout << s.capacity() << endl;
cout << s << endl;
s.clear();
cout << s.size() << endl;
cout << s.length() << endl;
cout << s.capacity() << endl;
cout << s << endl;
s.resize(15, 'a');
cout << s.size() << endl;
cout << s.length() << endl;
cout << s.capacity() << endl;
cout << s << endl;
s.resize(15);
cout << s.size() << endl;
cout << s.length() << endl;
cout << s.capacity() << endl;
cout << s << endl;
s.resize(5);
cout << s.size() << endl;
cout << s.length() << endl;
cout << s.capacity() << endl;
cout << s << endl;
}
void TestString2() {
string s;
s.reserve(100);
cout << s.size() << endl;
cout << s.capacity() << endl;
s.reserve(50);
cout << s.size() << endl;
cout << s.capacity() << endl;
}
void TestPushBack() {
string s;
size_t sz = s.capacity();
cout << sz << endl;
cout << "making s grow :" << endl;
for (size_t i = 0; i < 100; i++) {
s.push_back((i + '0'));
if (sz != s.capacity()) {
sz = s.capacity();
cout << "capacity changed: " << sz << endl;
}
}
}
void TestPushBackReserve() {
string s;
s.reserve(100);
size_t sz = s.capacity();
cout << sz << endl;
cout << "making s grow: \n";
for (size_t i = 0; i < 100; i++) {
s.push_back((i + '0'));
if (sz != s.capacity()) {
sz = s.capacity();
cout << "capacity changed : " << sz << endl;
}
}
}
#if 0
int main() {
return 0;
}
#endif
void TestString3() {
string s1("zdz handsome");
const string s2("zdz handsome");
cout << s1 << " " << s2 << endl;
cout << s1[0] << " " << s2[0] << endl;
s1[0] = 'Z';
cout << s1 << endl;
}
void TestString4() {
string s("zdz handsome");
for (size_t i = 0; i < s.size(); i++) {
cout << s[i];
}
cout << endl;
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;
for (auto c : s) {
cout << c;
}
cout << endl;
}
#if 0
int main() {
TestString3();
TestString4();
return 0;
}
#endif
void TestString5() {
string str;
str.push_back(' ');
str.append("hello");
str += 'a';
str += "ab";
cout << str << endl;
cout << str.c_str() << endl;
string file1("string.cc");
size_t pos = file1.rfind('.');
string suffix(file1.substr(pos, file1.size() - pos));
cout << suffix << endl;
string url("http://zdz-it.top/learnJava/zdz/niub/");
cout << url << endl;
size_t start = url.find("://");
if (start == string::npos) {
cout << "invalid url" << endl;
return;
}
start += 3;
size_t finish = url.find('/', start);
string address = url.substr(start, finish - start);
cout << address << endl;
pos = url.find("://");
url.erase(0, pos + 3);
cout << url << endl;
}
#if 0
int main() {
TestString5();
return 0;
}
#endif
#if 0
namespace pz {
class string {
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 tmp(s._str);
this->swap(tmp);
}
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; }
void push_back(char c) {
if (_size == _capacity) {
Reserve(_capacity * 2);
}
_str[_size++] = c;
_str[_size] = 0;
}
string& operator+= (char c) {
push_back(c);
return *this;
}
void clear() {
_size = 0;
_str[_size] = 0;
}
void swap(string& s) {
swap(_str, &s._str);
swap(_size, &s._size);
swap(_capacity, &s._capacity);
}
const char* c_str() const {
return _str;
}
void resize(size_t newSize, char c = 0) {
if (newSize > _size) {
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);
delete[] _str;
_str = str;
_capacity = newCapacity;
}
}
private:
size_t _size;
size_t _capacity;
char* _str;
};
}
#endif
#endif
lsit
#include <iostream>
#include <list>
#include <vector>
using std::cout;
using std::endl;
#if 0
int main() {
std::list<int> l1;
std::list<int> l2(4, 100);
std::list<int> l3(l2.begin(), l2.end());
std::list<int> l4(l3);
int arr[] = { 16,2,77,19 };
std::list<int> l5(arr, arr + sizeof(arr) / sizeof(int));
for (std::list<int>::iterator it = l5.begin(); it != l5.end(); it++) {
cout << *it << " ";
}
cout << endl;
for (auto& e : l5) {
cout << e << " ";
}
return 0;
}
#endif
void print_list(const std::list<int>& list) {
for (std::list<int>::const_iterator it = list.begin(); it != list.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
#if 0
int main() {
int arr[] = { 1,2,3,4,5,6,7,8,9 };
std::list<int> list(arr, arr + sizeof(arr) / sizeof(arr[0]));
for (std::list<int>::iterator it = list.begin(); it != list.end(); it++) {
cout << *it << " ";
}
cout << endl;
for (std::list<int>::reverse_iterator it = list.rbegin(); it != list.rend(); it++) {
cout << *it << " ";
}
cout << endl;
return 0;
}
#endif
void TestList1() {
int arr[] = { 1,2,3,4 };
std::list<int> list(arr, arr + sizeof(arr) / sizeof(arr[0]));
list.push_back(5);
list.push_front(0);
print_list(list);
list.pop_back();
list.pop_front();
print_list(list);
}
void TestList2() {
int arr[] = { 1,2,3,4 };
std::list<int> list(arr, arr + sizeof(arr) / sizeof(arr[0]));
auto pos = ++list.begin();
cout << *pos << endl;
list.insert(pos, 4);
print_list(list);
list.insert(pos, 5, 22);
std::vector<int> v{ 8,3,5,7,9 };
list.insert(pos, v.begin(), v.end());
print_list(list);
list.erase(pos);
print_list(list);
list.erase(list.begin(), list.end());
print_list(list);
}
void TestList3() {
int arr[] = { 1,2,3,4 };
std::list<int> l1(arr, arr + sizeof(arr) / sizeof(arr[0]));
std::list<int> l2;
print_list(l1);
l2.swap(l1);
print_list(l1);
print_list(l2);
l2.clear();
cout << l2.size() << endl;
}
#if 0
int main() {
TestList1();
TestList2();
TestList3();
return 0;
}
#endif
void TestListIterator1() {
int arr[] = { 1,2,3,4 };
std::list<int> l(arr, arr + sizeof(arr) / sizeof(arr[0]));
auto it = l.begin();
while (it != l.end()) {
++it;
}
}
void TestListIterator2() {
int arr[] = { 1,2,3,4,5,6,7,8,9,0 };
std::list<int> l(arr, arr + sizeof(arr) / sizeof(arr[0]));
auto it = l.begin();
while (it != l.end()) {
l.erase(it++);
}
}
#if 0
int main() {
TestListIterator1();
TestListIterator2();
return 0;
}
#endif
#if 0
namespace pz {
template<class T>
struct ListNode {
ListNode(const T& val = T()) :_pPre(nullptr),_pNext(nullptr),_val(val){}
ListNode<T>* _pPre;
ListNode<T>* _pNext;
T _val;
};
template<class T,class Ref,class Ptr>
class ListIterator {
typedef ListNode<T>* PNode;
typedef ListIterator<T, Ref, Ptr> Self;
public:
ListIterator(PNode pNode = nullptr) :_pNode(pNode){}
ListIterator(const Self& l):_pNode(l._pNode){}
T& operator*() { return _pNode->_val; }
T* operator->() { return &(operator*()); }
Self& operator++() {
_pNode = _pNode->_pNext;
return *this;
}
Self& operator++(int) {
Self tmp(*this);
_pNode = _pNode->_pNext;
return tmp;
}
Self& operator--();
Self& operator--(int);
bool operator!=(const Self& l) { return _pNode != l._pNode; }
bool operator==(const Self& l) { return _pNode == l._pNode; }
PNode _pNode;
};
template<class T>
class list {
typedef ListNode<T> Node;
typedef Node* PNode;
public:
typedef ListItreator<T, T&, T*> iterator;
typeder ListIterator<T, const T&, const T&> const_iterator;
public:
list() {
CreateHead();
}
list(int n,const T& value = T()) {
CreateHead();
for (int i = 0; i < n; i++) {
push_back(value);
}
}
template<class Iterator>
list(Iterator first, Iterator last) {
CreateHead();
while (first != last) {
push_back(*first);
++first;
}
}
list(const list<T>& l) {
CreateHead();
list<T> tmp(l.begin(), l.end());
this->swap(tmp);
}
list<T>& operator=(const list<T> l) {
this->swap(l);
return *this;
}
~list() {
clear();
delete _pHead;
_pHead = nullptr;
}
iterator begin() { return iterator(_pHead->pNext); }
iterator end() { return iterator(_pHead); }
const_iterator begin() { return const_iterator(_pHead->_pNext); }
const_iterator end() { return const_iterator(_pHead); }
size_t size()const;
bool empty() const;
T& front();
const T& front() const;
T& back();
const T& back() const;
void push_back(const T& val) { insert(begin(), val); }
void pop_back() { erase(--end()); }
void push_front(const T& val) { insert(begin(), val); }
void pop_front() { erase(begin()); }
iterator insert(iterator pos, const T& val) {
PNode pNewNode = new Node(val);
PNode pCur = pos._pNode;
pNewNode->_pPre = pCur->_pPre;
pNewNode->_pNext = pCur;
pNewNode->_pPre->_pNext = pNewNode;
pCur->_pPre = pNewNode;
return iterator(pNewNode);
}
iterator erase(iterator pos) {
PNode pDel = pos._pNode;
PNode pRet = pDel->_pNext;
pDel->_pPre->_pNext = pDel->_pNext;
pDel->_pNext->_pPre = pDel->_pPre;
delete pDel;
return iterator(pRet);
}
void clear();
void swap(List<T>& l);
private:
void CreateHead() {
_pHead = new Node;
_pHead->_pPre = _pHead;
_pHead->_pNext = _pHead;
}
private:
PNode _pHead;
};
}
#endif