Vector of list of strings : list vector « list « C++ Tutorial






#include <list>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
#include <fstream>
#include <iostream>
#include <sstream>
using namespace std;

list<string>
getTotalEnrollment(const vector<list<string> >& classLists,const list<string>& droppedStudents)
{
  list<string> allStudents;

  for (size_t i = 0; i < classLists.size(); ++i) {
    allStudents.insert(allStudents.end(), classLists[i].begin(),classLists[i].end());
  }

  allStudents.sort();

  allStudents.unique();

  for (list<string>::const_iterator it = droppedStudents.begin();
       it != droppedStudents.end(); ++it) {
    allStudents.remove(*it);
  }

  return (allStudents);
}

void readStudentList(list<string> &students, ifstream &istr)
{
  string name;
  while (getline(istr, name)) {
    cout << "Read name " << name << endl;
    students.push_back(name);
  }
}

void readCourseLists(vector<list<string> > &lists){
  for(int i = 1; ; i++) {
    ostringstream ostr;
    ostr << "course" << i << ".txt";

    ifstream istr(ostr.str().c_str());
    if (!istr) {
      cout << "Failed to open " << ostr.str() << endl;
      break;
    }
    list<string> newList;
    readStudentList(newList, istr);
    lists.push_back(newList);
  }
}

void readDroppedStudents(list<string> &students){
  ifstream istr("dropped.txt");
  readStudentList(students, istr);
}

int main(int argc, char **argv)
{
  vector<list<string> > courseLists;
  list<string> droppedStudents;

  readCourseLists(courseLists);
  readDroppedStudents(droppedStudents);

  list<string> finalList = getTotalEnrollment(courseLists, droppedStudents);

  copy(finalList.begin(), finalList.end(), ostream_iterator<string>(cout, "\n"));
}








17.19.list vector
17.19.1.assign a list to a vector
17.19.2.create an empty vector of int's and assign a list of doubles to it
17.19.3.Vector of list of strings
17.19.4.make a vector from a list with vector_copy
17.19.5.Transfer vector to a list and remove all negative numbers with bind2nd and less