7.string

目录

学库一定会看文档

1.Member functions

string介绍

2.迭代器iterator

1.正向迭代器

2.反向迭代器

3.反向const

4.反向const

3.容量capacity

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

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

2.clear()

2.1缩容shrink_to_fit()

3.reserve(扩容)

4.resize()

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


学库一定会看文档

下面推荐个网站

cplusplus.comicon-default.png?t=N7T8https://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);

(先不看)

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()

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

迭代器遍历vector

	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

	for (auto e : s1)
	{
		cout << e;
	}
	cout << endl;
	for (auto e : v)
	{
		cout << e;
	}
	cout << endl;

2.反向迭代器

reverse_iterator

rbegin()

rend()

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

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

3.反向const

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

4.反向const

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()

	//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++对比)

	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()

	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

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.取头尾字符

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

operator[]和at的区别

void string_1()
{
	string s1("hello1world!");
	cout << s1[5] << endl;
	cout << s1.at(5) << endl;


	try
	{
		s1.at(15);
        //s1[15];
	}
	catch (const std::exception& e)
	{
		cout << e.what() << 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);
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);
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;
}

另一种玩法:

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;

反着找

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;
	}
}

一个题目介绍getline

字符串最后一个单词的长度牛客题霸牛客网 (nowcoder.com)icon-default.png?t=N7T8https://www.nowcoder.com/practice/8c949ea5f36f422594b306a2300315da?tpId=37&&tqId=21224&rp=5&ru=/activity/oj&qru=/ta/huawei/question-ranking

#include<iostream>
#include <string>
using namespace std;

int main() {
    string s1;
    getline(cin, s1);

    size_t i = s1.rfind(' ');
    cout << s1.size() - (i + 1) << endl;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

对自己好点儿i

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值