为什么我不能在范围-v3中获得范围的大小?

时间:2016-03-20 09:01:04

标签: c++ c++11 clang c++14 range-v3

我想获得名称以“T”开头的人数:

public ActionResult TekenGrafiek(ApplicationUser user, string naam)
    {
        Product p = user.Verlanglijst.Producten.First(pr=>pr.Artikelnaam == naam);
        int totaal = p.AantalInCatalogus;
        DateTime[] weken = new DateTime[12];
        int[] aantal = new int[12];
        DateTime nu = DateTime.Now.StartOfWeek(DayOfWeek.Monday);
        for (int j = 0; j < 11; j++)
        {
            weken[j] = nu;
            aantal[j] = p.GeefAantalReserveerbaarInPeriode(nu, nu.AddDays(7));
            nu = nu.AddDays(7);
        }
        Bitmap image = new Bitmap(300, 50);
        Graphics g = Graphics.FromImage(image);
        Chart c = new Chart();
        ChartArea a = new ChartArea();

        a.AxisX.MajorGrid.Enabled = false;
        a.ShadowColor = Color.DeepSkyBlue;
        a.BackColor = Color.AliceBlue;
        c.Titles.Add("Aantal beschikbaar komende weken");
        c.ChartAreas.Add(a);
        c.Width = 1000;
        c.Height = 250;
        Series mySeries = new Series();
        mySeries.Points.DataBindXY(weken, aantal);
        mySeries.IsValueShownAsLabel = true;
        c.Series.Add(mySeries);

        MemoryStream imageStream = new MemoryStream();
        c.SaveImage(imageStream, ChartImageFormat.Png);
        c.TextAntiAliasingQuality = TextAntiAliasingQuality.SystemDefault;
        Response.ContentType = "image/png";
        imageStream.WriteTo(Response.OutputStream);
        g.Dispose();
        image.Dispose();
        return null;
    }

但是我收到了很大的编译错误:

#include <iostream>
#include <string>
#include <range\v3\all.hpp>

using namespace ranges;

int main()
{
    const auto names = std::vector<std::string> {"Tony", "Peter"};

    std::cout << size(names | view::filter([](const auto& s) {return s[0] == 'T';}));
}

顺便说一句,我在Visual Studio 2015 Update 1中使用了clang 3.7。 那么,怎么了?

2 个答案:

答案 0 :(得分:16)

使用distance,而不是size。后者用于可以在恒定时间内检索其大小的范围。

答案 1 :(得分:12)

您最好使用count_if算法。

auto cnt = ranges::count_if( names, [](const auto& s) {return s[0] == 'T';} )