值没有存储在向量中 - 不知道这里发生了什么

时间:2014-02-01 17:28:59

标签: c++

我正在研究一个问题,我必须在嵌套在地图中的向量中存储一些值,但这些值不会被存储。地图结构为Map <Vector<char> >

我正在尝试仅粘贴适合问题的代码,因为它位于更大的上下文中,但我怀疑我错过了一些重要的东西。任何帮助将不胜感激。

感谢。

编辑:我在这个问题中使用Stanford 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界面中,因此为它们添加......

     * 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);

}
}

1 个答案:

答案 0 :(得分:1)

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

正如代码所示,每次调用storeInVector都会创建一个地图&gt;副本,原始内容将保持不变。

编辑:

不要忘记更改功能声明(感谢@ user3175411)