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 59 60 61 62 63 64 65 66 67 68 69 70
|
#include <iostream>
#include <locale>
#include <algorithm>
#include <iterator>
#include <fstream>
#include <sstream>
#include<string>
#include <vector>
using namespace std;
//From cppreference.com (mostly): Class ctype encapsulates character classification features. All stream input operations performed
//through std::basic_istream<charT> use the std::ctype<charT> of the locale imbued in the stream to identify whitespace characters
//for input tokenization. A locale, in turn, includes a ctype facet that classifies character types. Such a facet, incorporating
//further characters, could be as follows:
class my_ctype : public ctype<char>
{
private:
mask my_table[table_size]; //unspecified bitmask type;
public:
my_ctype(size_t refs = 0) : std::ctype<char>(&my_table[0], false, refs)
{
copy_n(classic_table(), table_size, my_table);
my_table['-'] = (mask)space; //casts the delimiters to space;
my_table['\''] = (mask)space;
my_table['('] = (mask)space;
my_table[')'] = (mask)space;
my_table['!'] = (mask)space;
my_table[','] = (mask)space;
my_table['/'] = (mask)space;
my_table['.'] = (mask)space;
my_table['%'] = (mask)space;//sample array; can be expanded/modified depending on type of delimiters being handled;
}
};
int main()
{
fstream File;
vector<string>words;
File.open("F:\\test0.txt");
if(File.is_open())
{
while(!File.eof())
{
string line;
getline(File, line);
stringstream stream(line);
locale x(locale::classic(), new my_ctype);
//locale ctor using the classic() and my_ctype facet; locale destructor deletes the raw pointer;
stream.imbue(x);//imbue sets the locale of the stream object;
copy(istream_iterator<string>(stream),istream_iterator<string>(),back_inserter(words));
//copies all elements in the range into the vector<string>;
//derived, stringstream class, uses istream iterator;
// std::ostream_iterator<std::string>(std::cout, "\n")//in case you want to print to screen;
}
}
int sum{};
for(auto& itr : words)
{
if(itr.length()%2 != 0)
{
sum++;
}
else
{
sum+=2;
}
}
cout<<sum;
}
|