SVN / public / code / vorlesung-09 / StlDemo.cpp

Revision 11340
Date
Committerhb1003
Download
// Copyright 2016, University of Freiburg,
// Chair of Algorithms and Data Structures.
// Author: Hannah Bast <bast@cs.uni-freiburg.de>.

#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <fstream>
#include "./StlDemo.h"

// _____________________________________________________________________________
void StlDemo::readMovieInfo(std::string fileName) {
  std::ifstream file(fileName.c_str());
  std::string line;
  while (true) {
    std::getline(file, line);
    if (file.eof()) break;
    // Parse line into title, year, popularity.
    size_t pos1 = line.find("\t");
    if (pos1 == std::string::npos) continue;
    size_t pos2 = line.find("\t", pos1 + 1);
    if (pos2 == std::string::npos) continue;
    Movie movie;
    movie.title = line.substr(0, pos1);
    movie.year = atoi(line.substr(pos1 + 1, pos2 - pos1).c_str());
    movie.popularity = atoi(line.substr(pos2 + 1).c_str());
    // Insert into map.
    moviesByYear[movie.year].push_back(movie);
    // std::cout << "\"" << movie.title << "\", "
    //   << movie.year << ", " << movie.popularity << std::endl;
  }
}

// _____________________________________________________________________________
void StlDemo::sortByPopularity() {
  for (auto& pair : moviesByYear) {
    auto& movies = pair.second;
    std::sort(movies.begin(), movies.end(), [](const Movie& x, const Movie& y)
        { return x.popularity > y.popularity; });
  }
}