值未存储在向量中 - 不确定这里发生了什么
Values not being stored in a vector - not sure what's going on here
我正在解决一个问题,我必须将一些值存储在嵌套在地图中的向量中,但这些值没有被存储。地图结构Map <Vector<char> >
我尝试只粘贴适合该问题的代码,因为它位于更大的上下文中,但我怀疑我错过了一些重要的东西。任何帮助将不胜感激。
谢谢。
编辑:我在这个问题中使用斯坦福CS106B库,它为Map,Vector,getValue和add提供了接口:
* Constructor: Map
* Usage: Map<int> map;
* Map<int> map(500);
* Map<string> *mp = new Map<string>;
* -----------------------------------------
* The constructor initializes a new empty map. The optional
* argument is a hint about the expected number of entries
* that this map will hold, which allows the map to configure
* itself for efficiency at that size. If not specified, a
* reasonable default is used and the map will adapt as entries
* are added. The explicit keyword is used to prevent
* accidental construction of a Map from an integer.
* Raises an error if sizeHint is negative.
*/
explicit Map(int sizeHint = 101);
* Constructor: Vector
* Usage: Vector<int> vec;
* Vector<student> dormlist(200);
* Vector<string> *vp = new Vector<string>;
* -----------------------------------------------
* The constructor initializes a new empty vector. The optional
* argument is a hint about the expected number of elements that
* this vector will hold, which allows vector to configure itself
* for that capacity during initialization. If not specified,
* it is initialized with default capacity and grows as elements
* are added. Note that capacity does NOT mean size, a newly
* constructed vector always has size() = 0. A large starting
* capacity allows you to add that many elements without requiring
* any internal reallocation. The explicit keyword is required to
* avoid accidental construction of a vector from an int.
*/
explicit Vector(int sizeHint = 0);
getValue 位于 Map 界面中,因此它们两个的 add 也是如此......
* Member function: getValue
* Usage: value = map.getValue(key);
* ---------------------------------
* If key is found in this map, this member function returns the
* associated value. If key is not found, raises an error. The
* containsKey member function can be used to verify the presence
* of a key in the map before attempting to get its value.
*/
ValueType getValue(string key);
并为地图和矢量添加...
* Member function: add
* Usage: map.add(key, value);
* ---------------------------
* This member function associates key with value in this map.
* Any previous value associated with key is replaced by this new
* entry. If there was already an entry for this key, the map's
* size is unchanged; otherwise, it increments by one.
*/
void add(string key, ValueType value);
/*
* Member function: add
* Usage: vec.add(value);
* ----------------------
* This member function adds an element to the end of this vector.
* The vector's size increases by one.
*/
void add(ElemType elem);
/* Private Instance Variables */
int seed_length;
char letter;
//Map <Vector<char> > map;
/* Function Prototypes */
string promptUserForFile(ifstream & infile);
char nextCharacter(char letter);
void storeInVector(string sequence, char letter, Map<Vector<char> > map);
int main() {
ifstream infile;
promptUserForFile(infile);
// Ask what order of Markov model to use.
cout << "What order of Markov model should we use? ";
cin >> seed_length;
// Store sequences in a map.
string sequence;
Map<Vector<char> > map;
for (int i = 0; i < seed_length; i++) {
letter = infile.get();
sequence += nextCharacter(letter);
}
while (infile.eof() == false) {
letter = infile.get();
storeInVector(sequence, letter, map);
sequence.erase(0,1);
char next_letter = nextCharacter(letter);
sequence += next_letter;
}
}
函数storeInVector是导致问题的原因...
void storeInVector(string sequence, char letter, Map<Vector<char> > map) {
cout << sequence << endl;
cout << letter << endl;
Vector<char> vector(200); // I tried to specify size because it wasn't being populated... so I thought this may help
if (map.containsKey(sequence)) {
// Insert the sequence that comes after it into the vector.
map.getValue(sequence).add(letter);
} else {
// Create a new key and vector.
map.add(sequence, vector);
map.getValue(sequence).add(letter);
}
}
通过引用传递map
:void storeInVector(string sequence, char letter, Map<Vector<char> >& map)
。
正如代码一样,每次调用storeInVector
都会创建一个 Map> 的副本,您的原始副本将保持不变。
编辑:
不要忘记更改函数声明(感谢@user3175411)
相关文章:
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 不确定如何装饰我的C++库代码以在 C 中使用
- C++功能泄漏内存,我是C++新手,不确定如何解决
- "错误 C0000:语法错误,令牌"<EOF>"处出现意外$end,并且不确定
- 代码在 CodeSignal 中工作不正确。不确定这是否是我的代码缺陷
- 为什么以下代码是不确定的?
- 编译错误:"lvalue required as left operand of assignment" 不确定原因
- 误用指针会产生分段错误。我仍然不确定如何纠正它
- 我不确定为什么代码没有运行,它在编译时抛出错误,说它无法找到 PrintArray()
- 不确定出了什么问题(字符串输入)
- C++睡眠时间不确定
- 在C++中做一个场景问题,我不确定如何处理布尔和if循环
- 代码输出随机符号,我不确定出了什么问题
- 可变值变化,不确定原因
- 有没有办法让C++从 cin 中接收不确定数量的字符串?
- 尝试一次将单个字符读取到大小不确定的数组中
- 我不确定如何引用此神经网络训练方法中的权重
- 函数参数:不确定排序还是未排序?
- 值未存储在向量中 - 不确定这里发生了什么
- gcc 4.7 中的错误?还是不是,我不确定,所以我在这里问