Project Euler 22: Names scores by Loki Started working through Project Euler this weekend.  

Project Euler 22


  Using names.txt (right click and 'Save Link/Target As...'), a 46K text
  file containing over five-thousand first names, begin by sorting it
  into alphabetical order. Then working out the alphabetical value for
  each name, multiply this value by its alphabetical position in the
  list to obtain a name score.
  
  For example, when the list is sorted into alphabetical order, COLIN,
  which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the
  list. So, COLIN would obtain a score of 938 × 53 = 49714.
  
  What is the total of all the name scores in the file?


#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;iterator&gt;
#include &lt;string&gt;
#include &lt;set&gt;
#include &lt;numeric&gt;
#include "PunctFacet.h"

using ThorsAnvil::Util::PunctFacet;

long scoreName(std::string const&amp; name)
{
    return std::accumulate(std::begin(name), std::end(name), 0L, 
                           [](long v1, char x){return v1 + x - 'A' + 1;});
}

int main()
{
    // Open a file that considers " and , as space and thus ignores them
    std::ifstream   data;
    data.imbue(std::locale(std::locale(), new PunctFacet(std::locale(), "\",")));
    data.open("euler/e22.data");

    // read all the names in a set (its sorted) 
    std::set&lt;std::string&gt; names{std::istream_iterator&lt;std::string&gt;(data),
                                std::istream_iterator&lt;std::string&gt;()};

    // Calculate the result
    long    score   = 0;
    long    loop    = 1;
    for(auto name: names) {
        score += (loop * scoreName(name));
        ++loop;
    }
    std::cout &lt;&lt; score &lt;&lt; "\n";
}


PunctFacet.h

#ifndef THORSANVIL_UTIL_PUNCT_FACET_H
#define THORSANVIL_UTIL_PUNCT_FACET_H

#include &lt;locale&gt;
#include &lt;string&gt;
#include &lt;sstream&gt;

namespace ThorsAnvil
{
    namespace Util
    {

// This is my facet that will treat the characters in `extraSpace`
// as space characters and thus ignore them with formatted input
class PunctFacet: public std::ctype&lt;char&gt;
{
    public:
        typedef std::ctype&lt;char&gt;    base;
        typedef base::char_type     char_type;

        PunctFacet(std::locale const&amp; l, std::string const&amp; extraSpace)
            : base(table)
        {
            std::ctype&lt;char&gt; const&amp;  defaultCType  = std::use_facet&lt;std::ctype&lt;char&gt; &gt;(l);

            // Copy the default value from the provided locale
            static  char data[256];
            for(int loop = 0;loop &lt; 256;++loop) { data[loop] = loop;}
            defaultCType.is(data, data+256, table);

            // Modifications to default to include extra space types.
            for(auto space: extraSpace) {
                table[space]  |= base::space;
            }
        }
    private:
        base::mask  table[256];
};

    }
}

#endif


Results

&gt; g++ -O3 -std=c++14 euler/e22.cpp
&gt; time ./a.out
871198282

real    0m0.018s
user    0m0.009s
sys     0m0.007s
#include &lt;numeric&gt;!

I see no reason why individual score is computed via std::accumulate, but the total uses a loop. I recommend std::inner_product there, at least for consistency.

Similarly, the 

        for(int loop = 0;loop &lt; 256;++loop) { data[loop] = loop;}


is

        std::iota(data, data + 256, 0);


Of course, a magic 256 shall be replaced by a symbolic name.

I don't feel comfortable with std::set used just to avoid sorting. It  obscures the intention. You seem to have the same feeling, and put the clarifying comment. I recommend to be explicit: read the names into a vector, and sort it. BTW, it would also reduce space requirements.