Push_front()替代地图的矢量和运算符++

时间:2015-04-02 04:11:30

标签: c++ vector

我在这些函数上遇到编译器错误:

编译器抱怨我的vector上的push_front(),以及我试图在我的地图上使用的operator ++。

我只想在前面添加一个矢量元素并分别添加一个新的地图条目。

 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];
                                                     ^

1 个答案:

答案 0 :(得分:4)

std::vector没有push_front,因为在向量的开头添加元素会非常低效(这会将所有现有元素向右移动一个位置以腾出空间新元素)。另一种方法是使用std::dequeue代替std::vector。它具有与std::vector类似的接口,但它不能保证所有元素都在连续的内存区域中,因此可以有效地实现插入。

关于你的第二个问题,我不明白你想要用map和一个增量运算符做什么(并且没有你在上面的代码中使用的所有类的定义,很难猜到)。

++是一元运算符,其约定是它在操作数上执行增量(无论增量对于特定类型的操作数是什么意思) 。基本上++object应该与object = object + 1意思相同。我不确定此操作对std::map的含义是什么。你提到了有关插入地图的内容,但插入了什么? operator++是一元的,所以你不能给它一个参数告诉它要插入什么。