7.string

目录

学库一定会看文档

[1.Member functions](#1.Member functions)

string介绍

2.迭代器iterator

1.正向迭代器

2.反向迭代器

3.反向const

4.反向const

3.容量capacity

0.size(),capacity(),max_size(),length()

[1.扩容机制(vs和Linux g++对比)](#1.扩容机制(vs和Linux g++对比))

2.clear()

2.1缩容shrink_to_fit()

3.reserve(扩容)

4.resize()

[4.Element access:](#4.Element access:)

1.捕获某个字符

2.取头尾字符

5.Modifiers

1.push_back(字符)

​编辑

2.append(字符串)

3.operator+=

4.assign(基本用不着)

5.insert

6.erase

7.replace

8.pop_back(c++11)

[6.String operations](#6.String operations)


学库一定会看文档

下面推荐个网站

cplusplus.comhttps://cplusplus.com/

std::string

Strings are objects that represent sequences of characters.

strings 是表示字符序列的对象。

1.Member functions

string介绍

| default (1) | string(); |
| copy (2) | string (const string& str); |
| substring (3) | string (const string& str, size_t pos, size_t len = npos); 复制从字符位置pos开始并跨越len个字符的str部分(或者直到str的结尾,如果str太短或len为string::npos |
| from c-string (4) | string (const char* s); 复制由指针s指向的以空字符结尾的字符序列(C字符串) |
| from sequence (5) | string (const char* s, size_t n); 从指向s的字符数组中复制前n个字符 |
| fill (6) | string (size_t n, char c); |

range (7) template <class InputIterator> string (InputIterator first, InputIterator last); (先不看)
cpp 复制代码
void teststring()
{
	string s0;
	string s1("hello world");
	string s2(s1);
	string s3(s1, 5, 3);
	string s4(s1, 5, 10);
	string s5(s1, 5);

	cout << "s0" << s0 << endl;
	cout << "s1" << s1 << endl;
	cout << "s2" << s2 << endl;
	cout << "s3" << s3 << endl;
	cout << "s4" << s4 << endl;
	cout << "s5" << s5 << endl;

	string s6(10, '#');
	cout << s6 << endl;

	s0 = s6;
	cout << s0;
}

2.迭代器iterator

1.正向迭代器

关键:begin()

end()

cpp 复制代码
void stringtest()
{
	string s1("hello lInux");
	string::iterator it1 = s1.begin();
	while (it1 != s1.end())
	{
		cout << *it1 << " ";
		++it1;
	}
}

迭代器遍历vector

cpp 复制代码
	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);
	vector<int>::iterator it2 = v.begin();
	while (it2 != v.end())
	{
		cout << *it2;
		it2++;
	}

范围for

cpp 复制代码
	for (auto e : s1)
	{
		cout << e;
	}
	cout << endl;
	for (auto e : v)
	{
		cout << e;
	}
	cout << endl;

2.反向迭代器

reverse_iterator

rbegin()

rend()

cpp 复制代码
//反向迭代器
void stringtest1()
{
	string s1("hello world !");
	string::reverse_iterator rit = s1.rbegin();
	while (rit != s1.rend())
	{
		cout << *rit << " ";
		++rit;
	}
}

特别注意:底层不是这样的!!!!!!!!!!!!

3.反向const

cpp 复制代码
void stringtest1()
{
	const string s1("hello world !");
	string::const_reverse_iterator rit = s1.rbegin();
	while (rit != s1.rend())
	{
		cout << *rit << " ";
		++rit;
	}
}

4.反向const

cpp 复制代码
void stringtest1()
{
	const string s1("hello world !");
	string::const_iterator rit = s1.begin();
	while (rit != s1.end())
	{
		cout << *rit << " ";
		++rit;
	}
}

3.容量capacity

0.size(),capacity(),max_size(),length()

cpp 复制代码
	//string s1("hello world!");
	//cout << s1.size() << endl;    
	//cout << s1.length() << endl;
	//cout << s1.max_size() << endl;
	//cout << s1.capacity() << endl;

1.扩容机制(vs和Linux g++对比)

cpp 复制代码
	int i = 0;
	for (i = 0; i < 100; i++)
	{
		s.push_back('c');
		if (sz != s.capacity())
		{
			sz = s.capacity();
			cout << "capacity changed : " << sz << '\n';
		} 
	}

g++下面的 (2倍扩容)

vs下面的(除了第一个其他都是1.5倍扩容)

2.clear()

cpp 复制代码
	string s1("hello world!");
	cout << s1 << endl;
	cout << s1.capacity() << endl;
	s1.clear();     
	cout << s1 << endl;   
	cout << s1.capacity() << endl;

clear只清数据,不清空间

capacity 没有变化

2.1缩容shrink_to_fit()

3.reserve(扩容)

s.reserve(100);

Linux下面

vs下面

总结:reserve只有比capacity大才起作用

4.resize()

10 删除 <size

20 插入 size < n < capacity

40 扩容+插入 capacity < n

cpp 复制代码
void stringtest4()
{
	string s1("hello world !!!");
	cout << s1.size() << endl;
	cout << s1.capacity() << endl;

	s1.resize(10);
	cout << s1.size() << endl;
	cout << s1.capacity() << endl <<endl;

	s1.resize(20);
	cout << s1.size() << endl;
	cout << s1.capacity() << endl << endl;

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


}

4.Element access:

string::operator[]

1.捕获某个字符

(1)[]

(2)at()

2.取头尾字符

cpp 复制代码
void test1()
{
	string s1("helloworld!");
	cout << s1[5] << endl;
	cout << s1.at(5) << endl;
	cout << s1.front() << endl;
	cout << s1.back() << endl;
}

5.Modifiers

1.push_back(字符)

复制代码
void push_back (char c);

2.append(字符串)

| string (1) | string& append (const string& str); |
| substring (2) | string& append (const string& str, size_t subpos, size_t sublen); |
| c-string (3) | string& append (const char* s); |
| buffer (4) | string& append (const char* s, size_t n); |
| fill (5) | string& append (size_t n, char c); |

range (6) template <class InputIterator> string& append (InputIterator first, InputIterator last);
cpp 复制代码
void test2()
{
	string s1("hello world");
	s1.push_back('!');
	cout << s1 << endl;
	s1.append("hwh");
	cout << s1 << endl;

	s1.append(10, 'x');
	cout << s1 << endl;

	string s2(" apple ");
	//s1.append(s2);
	//cout << s1 << endl;

	s1.append(++s2.begin(),--s2.end());  //不想要空格
	cout << s1 << endl;
}

3.operator+=

| string (1) | string& operator+= (const string& str); |
| c-string (2) | string& operator+= (const char* s); |

character (3) string& operator+= (char c);

4.assign(基本用不着)

| string (1) | string& assign (const string& str); |
| substring (2) | string& assign (const string& str, size_t subpos, size_t sublen); |
| c-string (3) | string& assign (const char* s); |
| buffer (4) | string& assign (const char* s, size_t n); |
| fill (5) | string& assign (size_t n, char c); |

range (6) template <class InputIterator> string& assign (InputIterator first, InputIterator last);

5.insert

| string (1) | string& insert (size_t pos, const string& str); |
| substring (2) | string& insert (size_t pos, const string& str, size_t subpos, size_t sublen); |
| c-string (3) | string& insert (size_t pos, const char* s); |
| buffer (4) | string& insert (size_t pos, const char* s, size_t n); |
| fill (5) | string& insert (size_t pos, size_t n, char c); void insert (iterator p, size_t n, char c); |
| single character (6) | iterator insert (iterator p, char c); |

range (7) template <class InputIterator> void insert (iterator p, InputIterator first, InputIterator last);

6.erase

| sequence (1) | string& erase (size_t pos = 0, size_t len = npos); |
| character (2) | iterator erase (iterator p); |

range (3) iterator erase (iterator first, iterator last);

7.replace

| string (1) | string& replace (size_t pos, size_t len, const string& str); string& replace (iterator i1, iterator i2, const string& str); |
| substring (2) | string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen); |
| c-string (3) | string& replace (size_t pos, size_t len, const char* s); string& replace (iterator i1, iterator i2, const char* s); |
| buffer (4) | string& replace (size_t pos, size_t len, const char* s, size_t n); string& replace (iterator i1, iterator i2, const char* s, size_t n); |
| fill (5) | string& replace (size_t pos, size_t len, size_t n, char c); string& replace (iterator i1, iterator i2, size_t n, char c); |

range (6) template <class InputIterator> string& replace (iterator i1, iterator i2, InputIterator first, InputIterator last);
cpp 复制代码
void test5()
{
	string s1("hello hello hello hello hello");
	size_t pos = s1.find(' ');
	while (pos != string::npos)
	{
		s1.replace(pos, 1, "%20");
		pos = s1.find(' ');
	}
	cout << s1 << endl;
}

另一种玩法:

cpp 复制代码
void test6() 
{
	string s3;
	string s2("hello hello hello hello hello");
	s3.reserve(s2.size());
	for (auto ch : s2)
	{
		if (ch != ' ')
		{
			s3 += ch;
		}
		else
		{
			s3 += "%20";
		}
	}
	cout << s3 << endl;
	s2.swap(s3);
}

8.pop_back(c++11)

6.String operations

重点find

| string (1) | size_t find (const string& str, size_t pos = 0) const; |
| c-string (2) | size_t find (const char* s, size_t pos = 0) const; |
| buffer (3) | size_t find (const char* s, size_t pos, size_t n) const; |

character (4) size_t find (char c, size_t pos = 0) const;

反着找

cpp 复制代码
void teststring14()
{
	string protocol, domain, uri;
	string s1("https://legacy.cplusplus.com/reference/string/string/find/");
	size_t pos = s1.find(':');
	cout << pos << endl;

	if (pos != string::npos);
	{
		protocol = s1.substr(0, (pos - 0));  //协议
		cout << protocol << endl;
	}

	size_t pos1 = s1.find('/',pos+3);
	if (pos1 != string::npos);
	{
		domain = s1.substr(pos + 3, pos1-(pos + 3));  
		cout << domain << endl;

		uri = s1.substr(pos1 + 1);
		cout << uri << endl;
	}
}
相关推荐
ROC_bird..4 分钟前
STL - vector的使用和模拟实现
开发语言·c++
机器视觉知识推荐、就业指导4 分钟前
C++中的栈(Stack)和堆(Heap)
c++
MavenTalk10 分钟前
Move开发语言在区块链的开发与应用
开发语言·python·rust·区块链·solidity·move
simple_ssn18 分钟前
【C语言刷力扣】1502.判断能否形成等差数列
c语言·算法·leetcode
寂静山林26 分钟前
UVa 11855 Buzzwords
算法
Curry_Math30 分钟前
LeetCode 热题100之技巧关卡
算法·leetcode
ahadee38 分钟前
蓝桥杯每日真题 - 第10天
c语言·vscode·算法·蓝桥杯
XiaoLeisj42 分钟前
【JavaEE初阶 — 多线程】生产消费模型 & 阻塞队列
java·开发语言·java-ee
2401_840192271 小时前
python基础大杂烩
linux·开发语言·python
@东辰1 小时前
【golang-技巧】- 定时任务 - cron
开发语言·golang·cron