views:

101

answers:

0

Hello all, I'm having some trouble trying to read in a file that consists of two columns. The first column is a document ID # (int) and the second (separated by whitespace) is the word itself (string). What I'm doing is reading it into a vector, then going through the vector and using even/odd mod, assigning the respective variables their values. Very brute force I know, right now I just want to get past this part... if you know a more graceful way, I'm all ears. BUT, what I'm having problems with is inserting 2 variables into a C++ STL Multimap. Below is my code:

#include <iostream>
#include <string>
#include <fstream>
#include <map>
#include <utility>
#include <vector>
#include <algorithm>

using namespace std;

int main (int argc, char *argv[])
{ 
 //create a map using the STL, with the docId being inserted as the unique key.
 multimap<int,string> docxx;
 pair<int,string> docxxPair;
 vector<string> tokens;
 unsigned int docId;
 string data;

 //read in words.txt
 ifstream ifs("words.txt");
 if("words.txt")
 { 
  string str;
  while(getline(ifs, str, ' '))
  {
   tokens.push_back(str);
   str.clear();
  }
  for(unsigned int i=0; i < tokens.size(); i++)
  {
   if((i%2) == 0)
    docId = atoi(tokens[i].c_str());
   else
    data = tokens[i;
   docxx.insert(make_pair(docId, data));
   docId.clear();
   data.clear();
  }
  tokens.clear();
 }
 else 
  cout << "\nUnable to open file. . .\n";
 cout << "Vector size: " << tokens.size() << "\n";
 cout << "Map size: " << docxx.size() << "\n";
}

Please let me know if you know the solution to this. Thanks!