Skip to content

Instantly share code, notes, and snippets.

@morganwilde
Created December 17, 2014 10:59
Show Gist options
  • Save morganwilde/40d9058f739ea12e4695 to your computer and use it in GitHub Desktop.
Save morganwilde/40d9058f739ea12e4695 to your computer and use it in GitHub Desktop.
#include <sstream>
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
void printVector(vector<string> v)
{
for (int i = 0; i < v.size(); i++) {
cout << v[i] << endl;
}
}
void deleteEmptyCells(vector<string> v)
{
for (int i = 0; i < v.size(); i++) {
if (v[i].length() == 0) {
v.erase(v.begin() + i);
}
}
}
vector<string> &split(const string &s, char delim, vector<string> &elems) {
stringstream ss(s);
string item;
while (getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
vector<string> split(const std::string &s, char delim) {
vector<string> elems;
split(s, delim, elems);
return elems;
}
int main(void) {
// Word by word storage
vector<string> words;
ifstream fin("test.in");
string line;
while (getline(fin, line)) {
vector<string> wordsSplit = split(line, ' ');
words.insert(words.end(), wordsSplit.begin(), wordsSplit.end());
//cout << words.size() << endl;
}
// Create five storage files
ifstream storage1("storage_0_1.out");
ifstream storage2("storage_0_2.out");
ifstream storage3("storage_0_3.out");
ifstream storage4("storage_0_4.out");
ifstream storage5("storage_0_5.out");
int blockSize = 5;
int gapFrom = 0;
int gapTo = blockSize;
vector<string> storage1vector;
vector<string> storage2vector;
vector<string> storage3vector;
vector<string> storage4vector;
vector<string> storage5vector;
while (gapFrom < words.size()) {
storage1vector.insert(storage1vector.begin(), words.begin() + gapFrom, words.begin() + gapTo);
gapFrom += blockSize;
gapTo += blockSize;
if (gapFrom >= words.size()) break;
storage2vector.insert(storage2vector.begin(), words.begin() + gapFrom, words.begin() + gapTo);
gapFrom += blockSize;
gapTo += blockSize;
if (gapFrom >= words.size()) break;
storage3vector.insert(storage3vector.begin(), words.begin() + gapFrom, words.begin() + gapTo);
gapFrom += blockSize;
gapTo += blockSize;
if (gapFrom >= words.size()) break;
storage4vector.insert(storage4vector.begin(), words.begin() + gapFrom, words.begin() + gapTo);
gapFrom += blockSize;
gapTo += blockSize;
if (gapFrom >= words.size()) break;
storage5vector.insert(storage5vector.begin(), words.begin() + gapFrom, words.begin() + gapTo);
gapFrom += blockSize;
gapTo += blockSize;
if (gapFrom >= words.size()) break;
}
//cout << storage1vector.size() << endl;
deleteEmptyCells(storage1vector);
printVector(storage1vector);
/*
printVector(storage2vector);
printVector(storage3vector);
printVector(storage4vector);
printVector(storage5vector);
*/
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment