forked from tobbez/string-splitting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
split7.cpp
58 lines (45 loc) · 1.6 KB
/
split7.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
46
47
48
49
50
51
52
53
54
55
56
57
58
// Contributed by Arash Partow (http://www.partow.net/)
// Requires StrTk (http://www.partow.net/programming/strtk/index.html)
#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>
#include <vector>
#include <iterator>
#include "strtk.hpp"
int main()
{
std::string input_line;
typedef std::pair<const char *, const char *> StrLimits;
std::vector<StrLimits> spline;
long count = 0;
timespec start;
clock_gettime(CLOCK_MONOTONIC, &start);
std::cin.sync_with_stdio(false); //disable synchronous IO
const strtk::single_delimiter_predicate<std::string::value_type> delimiter(' ');
std::size_t numWords = 0;
std::size_t numChars = 0;
spline.reserve(100);
while(std::getline(std::cin, input_line))
{
spline.clear();
numWords += strtk::split(delimiter,
input_line,
std::back_inserter(spline));
for (std::vector<StrLimits>::const_iterator iter = spline.begin(); iter != spline.end(); ++iter)
numChars += iter->second - iter->first;
count++;
};
timespec end;
clock_gettime(CLOCK_MONOTONIC, &end);
const double sec = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) * 1e-9;
std::cerr << "C++ : Saw " << count << " lines (" << numWords << " words/" << numChars << " chars) in " << std::fixed << std::setprecision(1) << sec << " seconds." ;
if (sec > 0)
{
const double lps = count / sec;
std::cerr << " Crunch speed: " << std::fixed << std::setprecision(1) << lps << std::endl;
}
else
std::cerr << std::endl;
return 0;
}