我對這些功能讓編譯器錯誤向量和操作員++: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];
^
我知道它沒有push_front()。這就是我尋找替代品的原因。 – NewToThis 2015-04-02 04:38:24
[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
[Here's a link](http:/ /en.cppreference.com/w/cpp/container)到C++標準庫容器的文檔 - 底部附近有一個表格,顯示哪些容器支持哪些操作。 – 2015-04-02 05:29:07