-1

I need to read a multimap<int,string> from a file and i can't figure out how to do it.

ifstream in ("words.txt");
multimap<int, string> words;
int count = 0;
while (!in.eof()) {
        getline(in, words[count]);
        count++;
}

When i run it i get this error error: no match for ‘operator[]’ (operand types are ‘std::multimap<int, std::__cxx11::basic_string<char> >’ and ‘int’) getline(in, words[count]); I tried with in >> words[count], and it doesn't work too. How should i fix this ?

Papanash
  • 59
  • 7
  • Not directly related to your error, but before it bites you : [do not use `eof()` as a loop condition](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – Quentin May 19 '16 at 13:15
  • a) dont use while (!in.eof), google it youll find an answer on stackoverflow b) multimap [doesnt seem to have that operator](http://en.cppreference.com/w/cpp/container/multimap) – Borgleader May 19 '16 at 13:15

3 Answers3

1

The multimap doesn't have operator[].

You can use the insert method of multimap, or emplace if you use C++11.

See here for emplace documentation and example.

Mayhem
  • 487
  • 2
  • 5
  • 13
0

Use .insert() method:

cplusplus reference

Also if you want each pair at subsequent index you might want to use std::vector and push_back()

MaciekGrynda
  • 583
  • 2
  • 13
0

There is no [] operator for std::multimap. You must read a line as a string and then use insert.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69