带有嵌套矢量的地图

时间:2013-07-02 22:33:41

标签: c++ vector maps

由于某种原因,编译器不允许我从我创建的地图中检索整数向量,我希望能够用新向量覆盖此向量。编译器给我的错误是荒谬的。谢谢你的帮助!!

编译器不喜欢我的代码的这一部分:

line_num = miss_words[word_1];

错误:

[Wawiti@localhost Lab2]$ g++ -g -Wall *.cpp -o lab2
main.cpp: In function ‘int main(int, char**)’:
main.cpp:156:49: error: no match for ‘operator=’ in ‘miss_words.std::map<_Key, _Tp, _Compare, _Alloc>::operator[]<std::basic_string<char>, std::vector<int>, std::less<std::basic_string<char> >, std::allocator<std::pair<const std::basic_string<char>, std::vector<int> > > >((*(const key_type*)(& word_1))) = line_num.std::vector<_Tp, _Alloc>::push_back<int, std::allocator<int> >((*(const value_type*)(& line)))’
main.cpp:156:49: note: candidate is:
In file included from /usr/lib/gcc/x86_64-redhat->linux/4.7.2/../../../../include/c++/4.7.2vector:70:0,
                 from header.h:19,
                 from main.cpp:15:
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/vector.tcc:161:5: note: std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = int; _Alloc = std::allocator<int>]
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/vector.tcc:161:5: note:   no known conversion for argument 1 from ‘void’ to ‘const std::vector<int>&’

CODE:

map<string, vector<int> > miss_words;       // Creates a map for misspelled words
string word_1;                  // String for word;
string sentence;                // To store each line;
vector<int> line_num;               // To store line numbers

ifstream file;                  // Opens file to be spell checked
file.open(argv[2]);

int line = 1;
while(getline(file, sentence))          // Reads in file sentence by sentence
{
    sentence=remove_punct(sentence);    // Removes punctuation from sentence
    stringstream pars_sentence;     // Creates stringstream
    pars_sentence << sentence;      // Places sentence in a stringstream
    while(pars_sentence >> word_1)      // Picks apart sentence word by word
    {
        if(dictionary.find(word_1)==dictionary.end())
        {
            line_num = miss_words[word_1]; //Compiler doesn't like this
            miss_words[word_1] = line_num.push_back(line);
        }       
    }
line++;                     // Increments line marker
}

3 个答案:

答案 0 :(得分:1)

不,编译器按照你的想法抱怨这条线:

miss_words[word_1] = line_num.push_back(line);

函数std::vector::push_back()返回void。您无法将其分配给vector

不是将矢量复制出来,推送一个值,然后将其复制回来,为什么不直接插入它:

miss_words[word_1].push_back(line);

答案 1 :(得分:0)

你正在制作载体的副本,这是不必要的。

line_num = miss_words[word_1];

应改为:

std::vector<int> &v = miss_words[word_1];
v.push_back(line);

这也应该解决RyanMck指出的同样问题。

答案 2 :(得分:0)

line_num = miss_words[word_1]; // use assignment operator to assign value at key word_1 to line_num
miss_words[word_1] = line_num.push_back(line); // trying to reset the value at key word_1

正如RyanMcK所说,push_back返回void。

重写以上两行(更简单,更正确,并且您不使用赋值运算符复制任何数据):

miss_words[word_1].push_back(line) // directly access the value at key word_1

这是有效的,因为std :: map :: operator []返回对映射值的引用。

来源:

http://www.cplusplus.com/reference/vector/vector/push_back/

http://www.cplusplus.com/reference/map/map/operator[]/