【发布时间】:2018-04-01 03:11:01
【问题描述】:
我很难弄清楚如何解决这个错误。我到处寻找,似乎找不到解决方案。
我的程序是使用向量向量的键/值对算法。它接受一个键和值,然后将该值添加到向量向量中位置“键”处的向量。我使用 vPos 和 vNeg 作为我的向量向量,并根据 key 是正数还是负数来使用它们。
size 只返回向量向量中“key”位置处序列的大小。
data 返回一个指针,指向在“key”位置指定的序列中的第一个元素。
insert 在“key”位置向序列中添加一个新值。
我认为我的私有变量可能没有正确初始化,但我已经尝试了我能想到的所有方法,因此不胜感激。
#include <algorithm>
#include <list>
#include <vector>
using namespace std;
class key_value_sequences {
public:
int size(int key) const{
if (key < 0){
return vNeg.at(-key).size();
}
return vPos.at(key).size();
}
const int* data(int key) const{
if (key < 0){
if (vNeg.at(-key).size()==0) return nullptr;
return vNeg.at(-key).data();
}
if (vPos.at(key).size() == 0) return nullptr;
return vPos.at(key).data();
}
void insert(int key, int value){
if (key < 0){
vNeg.at(-key).push_back(value);
}
else{
vPos.at(key).push_back(value);
}
}
private:
vector<vector<int>> vPos;
vector<vector<int>> vNeg;
}; // class key_value_sequences
#endif // A3_HPP
【问题讨论】:
-
你从未初始化它们。
-
试着向你的橡皮鸭解释为什么
vPos.at(key)应该不扔掉。