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

你可能感兴趣的文章
Objective-C实现euclidean distance欧式距离算法(附完整源码)
查看>>
Objective-C实现Euclidean GCD欧几里得最大公约数算法(附完整源码)
查看>>
Objective-C实现euclideanDistance欧氏距离算法(附完整源码)
查看>>
Objective-C实现euler method欧拉法算法(附完整源码)
查看>>
Objective-C实现euler modified变形欧拉法算法(附完整源码)
查看>>
Objective-C实现eulerianPath欧拉路径算法(附完整源码)
查看>>
Objective-C实现Eulers TotientFunction欧拉函数算法(附完整源码)
查看>>
Objective-C实现eulers totient欧拉方程算法(附完整源码)
查看>>
Objective-C实现EulersTotient欧拉方程算法(附完整源码)
查看>>
Objective-C实现eval函数功能(附完整源码)
查看>>
Objective-C实现even_tree偶数树算法(附完整源码)
查看>>
Objective-C实现Exceeding words超词(差距是ascii码的距离) 算法(附完整源码)
查看>>
Objective-C实现exchange sort交换排序算法(附完整源码)
查看>>
Objective-C实现ExponentialSearch指数搜索算法(附完整源码)
查看>>
Objective-C实现extended euclidean algorithm扩展欧几里得算法(附完整源码)
查看>>
Objective-C实现ExtendedEuclidean扩展欧几里德GCD算法(附完整源码)
查看>>
Objective-C实现external sort外排序算法(附完整源码)
查看>>
Objective-C实现Factorial digit sum阶乘数字和算法(附完整源码)
查看>>
Objective-C实现factorial iterative阶乘迭代算法(附完整源码)
查看>>
Objective-C实现factorial recursive阶乘递归算法(附完整源码)
查看>>