博客
关于我
阿里钉钉面试题
阅读量: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实现radianToDegree弧度到度算法(附完整源码)
查看>>
Objective-C实现radix sort基数排序算法(附完整源码)
查看>>
Objective-C实现rail fence围栏密码算法(附完整源码)
查看>>
Objective-C实现randomized heap随机堆算法(附完整源码)
查看>>
Objective-C实现rayleigh quotient瑞利商算法(附完整源码)
查看>>
Objective-C实现RC4加解密算法(附完整源码)
查看>>
Objective-C实现RC4加解密算法(附完整源码)
查看>>
Objective-C实现recursive bubble sor递归冒泡排序算法(附完整源码)
查看>>
Objective-C实现recursive insertion sort递归插入排序算法(附完整源码)
查看>>
Objective-C实现recursive quick sort递归快速排序算法(附完整源码)
查看>>
Objective-C实现RedBlackTree红黑树算法(附完整源码)
查看>>
Objective-C实现redis分布式锁(附完整源码)
查看>>
Objective-C实现regular-expression-matching正则表达式匹配算法(附完整源码)
查看>>
Objective-C实现relu线性整流函数算法(附完整源码)
查看>>
Objective-C实现restful api服务(附完整源码)
查看>>
Objective-C实现reverse letters反向字母算法(附完整源码)
查看>>
Objective-C实现ReverseNumber反转数字算法 (附完整源码)
查看>>
Objective-C实现ReversePolishNotation逆波兰表示法算法 (附完整源码)
查看>>
Objective-C实现RGB Hsv 转换算法(附完整源码)
查看>>
Objective-C实现RGB和HSV相互转换算法(附完整源码)
查看>>