2015-04-02 24 views
0

我對這些功能讓編譯器錯誤向量和操作員++:Push_front()的替代,用於地圖

編譯器抱怨push_front()在我的載體,同時也對運營商++我試圖使用上我的地圖。

我只是想在前面添加一個向量元素並分別添加一個新的映射條目。

LanguageModel countSequence(LanguageModel &model, vector<string> &Seq) {  
    //append start and end to the vector of strings  
    vector<string>::iterator v = Seq.begin();  
    Seq.front();  
    Seq.("<START>");  
    v = Seq.end();  
    Seq.push_back("<END");  
    v = Seq.begin(); 


    //create word pairs  
    vector<string> pairs;  
    for (int n = 0; n <= Seq.size(); n++) {  
    pairs.at(n) = buildPair(Seq[n], Seq[n+1]); 

    } 


    //feed each word to map 1 with number of times it was seen  
    for (int m = 0; m <= Seq.size(); m++) {  
    ++model.firstCounts[Seq[m]];  
    model.firstCounts[Seq[m]] = count(Seq.begin(), Seq.end(), Seq[m]); 

    } 


    //feed each word pair and the number of times it was seen  
    for (int k = 0; k <= Seq.size(); k++) {  
    ++model.pairCounts[Seq[k]];  
    model.pairCounts[Seq[k]] = count(Seq.begin(), Seq.end(), Seq[k]);  
    } 


    //feed each unique first word in a word pair and the second words in the pairs 

    for (int l = 0; l <= Seq.size(); l++) {  
    istringstream iss(pairs[l]);  
    string sub;  
     iss >> sub;  
    string sub2;  
     iss >> sub2; 


     if (Seq[l] = sub) {  
      ++model.follows[sub];  
      model.follows[sub].push_back(sub2);  
     } 


    } 


return model;  
} 


string genNext(LanguageModel &model2, string &testWord, int Number) {  
    //use results of countSequence  
    string numbers[20]= model2.follows[testWord];  
    return numbers[Number]; 

} 

這些都是錯誤的:

LangModel.cpp: In function ‘LanguageModel countSequence(LanguageModel&, std::vector<std::basic_string<char> >&)’: 
LangModel.cpp:38:6: error: ‘class std::vector<std::basic_string<char> >’ has no member named ‘push_front’ 
Seq.push_front("<START>"); 

l.cpp:69:25: error: could not convert ‘(&(& Seq)->std::vector<_Tp, _Alloc>::operator[]<std::basic_string<char>, std::allocator<std::basic_string<char> > >(((std::vector<std::basic_string<char> >::size_type)l)))->std::basic_string<_CharT, _Traits, _Alloc>::operator=<char, std::char_traits<char>, std::allocator<char> >((*(const std::basic_string<char>*)(& sub)))’ from ‘std::basic_string<char>’ to ‘bool’ 
if (Seq[l] = sub) { 
            ^
LangModel.cpp:70:10: error: no match for ‘operator++’ (operand type is ‘std::map<std::basic_string<char>, std::list<std::basic_string<char> > >::mapped_type {aka std::list<std::basic_string<char> >}’) 
++model.follows[sub]; 
        ^
LangModel.cpp: In function ‘std::string genNext(LanguageModel&, std::string&, int)’: 
LangModel.cpp:81:45: error: conversion from ‘std::map<std::basic_string<char>, std::list<std::basic_string<char> > >::mapped_type {aka std::list<std::basic_string<char> >}’ to non-scalar type ‘std::string {aka std::basic_string<char>}’ requested 
string numbers[20]= model2.follows[testWord]; 
                ^
+0

我知道它沒有push_front()。這就是我尋找替代品的原因。 – NewToThis 2015-04-02 04:38:24

+0

[Inserting into a vector at the front](http://stackoverflow.com/questions/4226606/inserting-into-a-vector-at-the-front) – tomvodi 2015-04-02 04:42:01

+0

[Here's a link](http:/ /en.cppreference.com/w/cpp/container)到C++標準庫容器的文檔 - 底部附近有一個表格,顯示哪些容器支持哪些操作。 – 2015-04-02 05:29:07

回答

4

std::vector沒有push_front,因爲這將是非常低效的矢量開始添加元素(這將涉及移動現有的所有元素一個位置右側爲新元素騰出空間)。替代方法是使用std::dequeue而不是std::vector。它具有與std::vector類似的接口,但它不能保證所有元素都位於連續的內存區域中,因此可以高效地實現插入。

關於你的第二個問題,我不明白你想要用一個映射和一個增量操作符做什麼(並且沒有你在上面的代碼中使用的所有類的定義很難猜到)。

++是一元運算符和慣例是,它的增量上操作數(無論增量裝置,用於將特定類型的操作數的)。基本上++object應該和object = object + 1的意思一樣。我不確定這個操作對於std::map有什麼意義。你提到了一些關於插入地圖的內容,但插入了什麼? operator++是一元的,所以你不能給它一個參數告訴它插入什麼。