非整数数组索引
来源:互联网 发布:mac安装ps序列号 编辑:程序博客网 时间:2024/05/06 05:21
代码如下:
#include <string>#include <vector>class AssociativeArray {public:AssociativeArray();virtual ~AssociativeArray();int &operator[](const std::string &key);const int &operator[](const std::string &key) const;protected:class Element {public:Element(const std::string &key, int value) :mKey(key), mValue(value) {}std::string mKey;int mValue;};std::vector<Element> mData;};
#include "Array.h"#include <stdexcept>using namespace std;AssociativeArray::AssociativeArray() {}AssociativeArray::~AssociativeArray(){}int &AssociativeArray::operator[](const std::string &key){for (std::vector<Element>::iterator iter = mData.begin(); iter != mData.end(); ++iter) {if (iter->mKey == key) {return iter->mValue;}}return mData.insert(mData.end(), Element(key, 0))->mValue;}const int &AssociativeArray::operator[](const std::string &key) const{for (auto iter = mData.cbegin(); iter != mData.cend(); ++iter) {if (iter->mKey == key) {return iter->mValue;}}throw invalid_argument("Key '" + key + "' doest`t exist");}
#include "Array.h"#include <iostream>using namespace std;int main(){AssociativeArray myArray;myArray["Key 1"] = 11;myArray["Key 2"] = 22;myArray["Key 3"] = 33;try {cout << myArray["Key 1"] << endl;cout << myArray["Key 2"] << endl;// Test const operator[]const AssociativeArray& c = myArray;cout << c["Key 3"] << endl;cout << c["Key 4"] << endl;}catch (const invalid_argument& ex) {cout << "Caught exception: " << ex.what() << endl;}return 0;}
0 0
- 非整数数组索引
- 数组第一个没出现的非负整数
- 数组中第一个没出现的非负整数
- 给定一个非负整数数组,最初定位在数组的第一个索引处。组中的每个元素表示您在该位置的最大跳跃长度。求最小跳数
- 一个数组内的非负整数,能组合成的最大整数
- 给定一个整数数组,返回数组中两个元素的和为target的索引
- 整数数组
- 高精度非负整数
- 非整数背包
- 给定一个整数数组,返回两个数字的索引,使它们相加得到一个特定目标值
- 非聚集索引,聚集索引
- 聚集索引,非聚集索引
- 聚集索引,非聚集索引
- 聚集索引,非聚集索引
- 聚集索引非聚集索引
- 聚集索引&非聚集索引
- 聚集索引,非聚集索引
- 一个能将给定非负整数数组中的数字排列成最大数字的函数
- 无锁环形队列,volatile和乱序执行
- 64位Windows7使用VS2010编译wxWidgets
- 2.3变量大小和sizeof操作符
- C++ Primer 4th 读书笔记(第二部分)
- 打算se碍事儿算
- 非整数数组索引
- TableView unable to dequeue a cell with identifier Cell - must register a class or Nib
- 回文字符串
- [总结]使用WPF路由事件过程中遇到的一些小问题
- ultraedit显示函数列表的步骤
- 数学建模高级方法(九):数据分析之组内一致与组间差异
- 栈与递归-汉诺塔
- OllyDbg 使用笔记 (十六)
- POSIX线程的同步