Skip to content

Instantly share code, notes, and snippets.

@Iainmon
Last active August 30, 2019 21:42
Show Gist options
  • Select an option

  • Save Iainmon/3b32d98ab7e042ad8ecc27e15d5be0ba to your computer and use it in GitHub Desktop.

Select an option

Save Iainmon/3b32d98ab7e042ad8ecc27e15d5be0ba to your computer and use it in GitHub Desktop.
#include <iostream>
#include <map>
#include <vector>
#include <string>
using namespace std;
struct Entry {
int id;
string category;
string field;
Entry(int _id, string _category, string _field) {
id = _id;
category = _category;
field = _field;
}
};
int main() {
vector<Entry*> entries = vector<Entry*>();
int total = 0;
for (int i = 0; i < 20; i++ total++) {
entries.push_back(new Entry(total, "Fruit", "Apple-"+i));
}
for (int i = 0; i < 20; i++ total++) {
entries.push_back(new Entry(total, "Fruit", "Orange-"+i));
}
for (int i = 0; i < 20; i++ total++) {
entries.push_back(new Entry(total, "Vegetable", "Brocolli-"+i));
}
for (int i = 0; i < 20; i++ total++) {
entries.push_back(new Entry(total, "Startch", "Potato-"+i));
}
map<string, vector<Entry*>> catagories = map<string, vector<Entry*>>();
for (int i = 0; i < entries.size(); i++) {
if (categories.count(entries[i]->category) < 1) {
categories.emplace(entries[i]->category, vector<Entry*>());
}
categories[entries[i]->category].push_back(entries[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment