模拟实现string——下

类对象的访问以及遍历操作

在这里插入图片描述

operator[]

由于string类的底层是连续的空间,因此可以实现使用[]+下标来访问并修改string对象的内容,需要注意一下的是最好需要重载一个const类型的成员函数,针对传入类型为const变量时使用。

		char& operator[](size_t pos)//模拟实现[]
		{
   
			assert(pos < _size);
			return _str[pos];
		}

		const char& operator[](size_t pos) const//模拟实现[]
		{
   
			assert(pos < _size);

			return _str[pos];
		}

访问和遍历string对象操作

for+[]下标访问

void Teststring()
{
   
	string s("hello Bit");
	// 3种遍历方式:
	// 需要注意的以下三种方式除了遍历string对象,还可以遍历是修改string中的字符,
	// 另外以下三种方式对于string而言,第一种使用最多
	// 1. for+operator[]
	for (size_t i = 0; i < s.size(); ++i)
	{
   
		cout << s[i] << " ";
	}
}

在这里插入图片描述

迭代器访问

void Teststring()
{
   
	string s("hello Bit");
	// 2.迭代器
	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++;
	}
}

可以使用反向迭代器进行逆序访问
在这里插入图片描述

范围for

void Teststring()
{
   
	string s("hello Bit");
	 //3.范围for
	for (auto ch : s)
	{
   
		cout << ch << " ";
	}
}

实际上范围for的底层还是由迭代器实现的,只有先实现了string类的迭代器才能正常使用范围for,下面会介绍如何实现string的迭代器。
在这里插入图片描述

at

at和[]功能类似,都是借助下标来访问string对象的内容,不同的是他们处理错误的方式不同,[]处理越界的方式是断言报错,at采用的方式是抛异常

void test_string5()
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,让我们来模拟一个共享书店的例子,并使用静态成员来实现。 首先,我们需要一个`Book`类表示书籍,其中包括书名和作者: ```cpp class Book { public: Book(string title, string author) : title_(title), author_(author) {} string getTitle() const { return title_; } string getAuthor() const { return author_; } private: string title_; string author_; }; ``` 接下来,我们需要一个`Bookstore`类来表示书店。我们将使用静态成员变量来存储所有书籍的列表。每当有新书加入或旧书出售时,这个列表都会被更新。 ```cpp class Bookstore { public: static void addBook(const Book& book) { books_.push_back(book); } static void sellBook(const Book& book) { for (auto it = books_.begin(); it != books_.end(); ++it) { if (it->getTitle() == book.getTitle() && it->getAuthor() == book.getAuthor()) { books_.erase(it); break; } } } static void printBooks() { cout << "Books in stock:" << endl; for (const auto& book : books_) { cout << book.getTitle() << " by " << book.getAuthor() << endl; } } private: static vector<Book> books_; }; vector<Book> Bookstore::books_; ``` 在这个例子中,`books_`是一个静态成员变量,它是一个`vector`,存储所有在书店中的书籍。 现在我们可以使用`Bookstore`类来添加、出售和打印书籍。下面是一个例子: ```cpp int main() { Bookstore::addBook(Book("The Great Gatsby", "F. Scott Fitzgerald")); Bookstore::addBook(Book("To Kill a Mockingbird", "Harper Lee")); Bookstore::addBook(Book("1984", "George Orwell")); Bookstore::printBooks(); Bookstore::sellBook(Book("To Kill a Mockingbird", "Harper Lee")); Bookstore::printBooks(); return 0; } ``` 输出: ``` Books in stock: The Great Gatsby by F. Scott Fitzgerald To Kill a Mockingbird by Harper Lee 1984 by George Orwell Books in stock: The Great Gatsby by F. Scott Fitzgerald 1984 by George Orwell ``` 可以看到,我们成功地使用了静态成员变量来存储所有书籍的列表,并在添加和出售书籍时更新了它。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值