博客
关于我
阿里钉钉面试题
阅读量:409 次
发布时间:2019-03-06

本文共 765 字,大约阅读时间需要 2 分钟。

定义一个类对list进行封装,使其可以通过下标对list进行操作

在C++编程中,为了实现对list的封装操作,可以定义一个类来将list的元素通过数组样式的方式访问。以下是实现方法:

#include 
#include
#include
#include
using namespace std;class Test{public: Test(); ~Test(); Test(list
strList); string &operator[](int n);};Test::Test(list
strList){ m_strList = strList;}string &Test::operator[](int n){ list
::iterator i = m_strList.begin(); vector
::iterator> vecStrIter; for (; i != m_strList.end(); i++) { vecStrIter.push_back(i); } return *(vecStrIter[n]);}int main(){ string str[4] = {"a", "li", "ba", "ba!"}; list
strList(str, str + 4); Test test(strList); cout << test[1] << endl; return 0;}

通过这种方式,可以通过类似数组的方式访问list中的元素。编译环境不会报告错误,且运行时不会出现未定义的行为。

转载地址:http://fbbkz.baihongyu.com/

你可能感兴趣的文章
PyCharm 插件工具使用
查看>>
PyCharm 搜索与导航栏详解
查看>>
PyCharm 搭建 Selenium + Python 的自动化测试环境
查看>>
PyCharm 数据库操作详解
查看>>
PyCharm 版本与管理详解
查看>>
PyCharm 界面排版详解
查看>>
pycharm 配置qt
查看>>
Pycharm+Django搭建第一个Python Web程序
查看>>
pycharm+pyqt5配置
查看>>
PyCharm不突出显示错误
查看>>
pycharm中windows找不到chrome解决办法
查看>>
Pycharm中基于Ollama和Proxy AI使用本地算力进行AI辅助编程
查看>>
Pycharm中配置项目的打开方式(This Window,New Window)
查看>>
PyCharm为什么这么牛?
查看>>
Pycharm出现 Cannot find declaration to go to 解决方法(全)
查看>>
pycharm出现Can‘t get remote credentials for deployment server 的解决方法
查看>>
pycharm可视化数据库
查看>>
pytorch 中的 #@save的意思
查看>>
Pycharm如何取消自动换行
查看>>
PyCharm安装及使用说明
查看>>