0

I'd like to get from a vector of collected Data (each has a time stamp) only the elements which are newer than a specified time. savedPublishedData is a map where i look for the id. savedPublishedData.second is thn a vector of Data (Custom Class), each Data is for one time stamp.

std::vector<PublisherData>&
DataModel::getSavedPublishedData(const unsigned int id, seconds newerThan)
{
    auto dataIterator = savedPublishedData.find(id);
    if (dataIterator == savedPublishedData.end()) {
        return; //type?
    }
    auto timeNow = system_clock::now();

    std::vector<PublisherData>& dataRawRef = dataIterator->second;

    auto is_newer = [&timeNow, &newerThan](const PublisherData& pD) ->bool
    {
        return (pD.getTimeGenerated() + newerThan) > timeNow;
    };

    auto dataIsNewer = dataRawRef | std::views::filter(is_newer);
    
    return dataIsNewer; //How to return the view?
}

My compiler can't determine the type of dataIsNewer (), so I don't know how to return the found values properly.

cpplearner
  • 13,776
  • 2
  • 47
  • 72
Mitch
  • 27
  • 5
  • 1
    A view is not a vector, either return `auto`, or create a copy of the range into a vector (and return by value, not by ref). – Jarod42 May 21 '21 at 08:09
  • I try (in order to avoid copying) to return a view iterator which iterates over the filterd vector – Mitch May 21 '21 at 08:37
  • Type of view is "complex", so using `auto` would be the simpler. – Jarod42 May 21 '21 at 08:42
  • I tried this before, but auto does not work in this case (compiler error: 'return type': all return expressions must deduce to the same type: previously it was 'return type') – Mitch May 21 '21 at 08:54
  • 1
    view is not vector. `return std::vector(dataIsNewer.begin(), dataIsNewer.end())` but if so, the return type should not be a ref type. – ZhiyuanLck May 21 '21 at 09:05
  • Maybe you also need common range to transform a view to a vector. – ZhiyuanLck May 21 '21 at 09:07
  • Okay I see that works, thank. But I think data is now copied, right? – Mitch May 21 '21 at 09:17

0 Answers0