我在这些函数上遇到编译器错误:
编译器在我的 vector 上提示 push_front(),还有我试图在我的 map 上使用的运算符++。
我只是想分别在前面添加一个 vector 元素和添加一个新的 map 条目。
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];
^
最佳答案
std::vector
没有 push_front
因为在 vector 的开头添加元素效率极低(这将涉及移动所有现有元素向右移动一个位置,为新元素腾出空间)。替代方法是使用 std::dequeue
而不是 std::vector
。它具有与 std::vector
类似的接口(interface),但它不保证所有元素都在连续的内存区域中,因此可以有效地实现插入。
关于你的第二个问题,我不明白你想用映射和增量运算符做什么(如果没有你在上面代码中使用的所有类的定义,很难猜到)。
++
是一元运算符,约定是它对操作数进行增量(无论增量对特定类型意味着什么操作数)。基本上 ++object
应该和 object = object + 1
意思相同。我不确定这个操作对 std::map
有什么意义。你提到了一些关于插入 map 的东西,但是插入什么? operator++
是一元的,所以你不能给它一个参数来告诉它要插入什么。
关于c++ - vector 的 Push_front() 替代方案和 map 的 operator++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29404853/