博客
关于我
阿里钉钉面试题
阅读量:408 次
发布时间: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/

你可能感兴趣的文章
numpy 或 scipy 有哪些可能的计算可以返回 NaN?
查看>>
numpy 数组 dtype 在 Windows 10 64 位机器中默认为 int32
查看>>
numpy 数组与矩阵的乘法理解
查看>>
NumPy 数组拼接方法-ChatGPT4o作答
查看>>
numpy 用法
查看>>
Numpy 科学计算库详解
查看>>
Numpy.fft.fft和numpy.fft.fftfreq有什么不同
查看>>
Numpy.ndarray对象不可调用
查看>>
numpy、cv2等操作图片基本操作
查看>>
numpy判断对应位置是否相等,all、any的使用
查看>>
Numpy如何使用np.umprod重写range函数中i的python
查看>>
numpy数组替换其中的值(如1替换为255)
查看>>
numpy数组索引-ChatGPT4o作答
查看>>
numpy转PIL 报错TypeError: Cannot handle this data type
查看>>
NutzCodeInsight 2.0.7 发布,为 nutz-sqltpl 提供友好的 ide 支持
查看>>
NUUO网络视频录像机 css_parser.php 任意文件读取漏洞复现
查看>>
NVelocity标签使用详解
查看>>
Nvidia Cudatoolkit 与 Conda Cudatoolkit
查看>>
nvidia-htop 使用教程
查看>>
oauth2-shiro 添加 redis 实现版本
查看>>