forked from andreacasalino/csvcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadMe.cpp
45 lines (37 loc) · 1.19 KB
/
ReadMe.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <csvpp/Write.h>
#include <csvpp/Read.h>
#include <csvpp/TypedProcessor.h>
#include <unordered_map>
int main() {
{
std::string destination = "some/path/foo.csv";
csvpp::Writer<std::string, std::string, int, float> writer{
// destination file
destination,
// headers
"name", "surname", "age", "height"};
// write the file row by row
writer.add("Paolo", "Rossi", 24, 180.5f);
writer.add("Luigi", "Neri", 32, 176.8f);
// and so on
}
{
std::string location = "some/path/foo.csv";
csvpp::Reader
reader; // default separator is assuemd to be ',' but you can change it
std::unordered_map<std::string, std::pair<int, float>>
people; //<name-surname, <age, height>>
csvpp::TypedProcessor<std::string, std::string, int, float>::process(
reader,
// source file
location,
// process action, called for each line
[&people](std::string name, std::string surname, int age,
float height) {
people.emplace(name + "-" + surname, std::make_pair(age, height));
},
// headers
"name", "surname", "age", "height");
}
return EXIT_SUCCESS;
}