I know it's been about 14 years since your original question, but I too had a need for a C++ pivot table implementation and thought I would share my code. My focus was more on reducing RAM usage (at least for the function, and the functions are limited in what kind of input data they take, but they do allow for an arbitrary number of index and value fields.
Within my GitHub project, which is available under the MIT license, you can find two pivot table functions within the pivot_compressors.cpp file. The first, scan_to_pivot()
, generates pivot table data by iterating through a .csv file row by row, thus limiting your overall memory needs. (It makes extensive use of Vince La's CSV parser, both for input and for output.)
The second function, in_memory_pivot()
, allows data already in RAM to be processed. It requires, though, that the input data take the form of a vector of maps of string-variant pairs. Within this structure, each vector entry represents a row; each string represents a field name, and each variant represents a string- or double-typed cell value. It shouldn't be too hard to modify this setup to fit your needs, though.
To create pivot_tables, in_memory_pivot()
first groups the values corresponding to the index fields passed by the caller into single strings. It then uses these strings as keys within a map; the values are themselves maps with value field names as keys and structs as values. (These structs allow sum, count, and mean data to get stored for each value field.) The main benefit of this implementation is that it allows for as many index or value fields to get incorporated into your pivot table as your computer can handle.
As an alternative (and undoubtedly better-tested option), I should also note that Hossein Moein's C++ DataFrame library, available under the BSD-3_Clause license, offers pivot-table functionality in the form of a set of groupby() functions. I believe that these support up to three index fields, but I might ask whether he could allow for an arbitrary number of fields to get passed to the function.