值未存储在向量中 - 不确定这里发生了什么

Values not being stored in a vector - not sure what's going on here

本文关键字:不确定 这里 发生了 什么 存储 向量      更新时间:2023-10-16

我正在解决一个问题,我必须将一些值存储在嵌套在地图中的向量中,但这些值没有被存储。地图结构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);
}
}

通过引用传递mapvoid storeInVector(string sequence, char letter, Map<Vector<char> >& map)

正如代码一样,每次调用storeInVector都会创建一个 Map> 的副本,您的原始副本将保持不变。

编辑:

不要忘记更改函数声明(感谢@user3175411)