2015-04-02 68 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++是一元的,所以你不能给它一个参数告诉它插入什么。