裹在像阵列STD容器的原始数据,与运行时的大小 [英] Wrap raw data in std container like array, with runtime size

查看:194
本文介绍了裹在像阵列STD容器的原始数据,与运行时的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有这将是固定大小一样的std ::阵列的任何性病的容器,但规模不会编译时间,但运行时?

我想通过一些数据我都保存在的std ::阵列的std :: acculumate 以及类似功能。我不想使用std ::向量(在嵌入式平台上工作),所以我要寻找的东西之间​​。

假设code这样的,我要的是什么到位的情况下使用 array_part

 的#include<阵列GT;
#包括LT&;&算法GT;
#包括LT&;&iostream的GT;
#包括LT&;&数字GT;
#包括LT&;矢量>诠释的main()
{
  的std ::阵列<浮动,100〕 someData;
//填充数据
  INT DATACOUNT = 50;
  的std :: array_part<浮动> partOfData(someData.data(),DATACOUNT)); //<<<<<这里
  常量汽车s_x =的std ::累加(partOfData.begin(),partOfData.end(),0.0);}

如果没有这样的容器,我怎么能换到的std ::积累和其他性病的算法?<原始数据我已经和present他们/ p>

解决方案

的std ::积累需要迭代器。你可以通过它迭代器包含感兴趣的范围:

 自动启动= partOfData.begin()+ 42;
自动结束= partOfData.begin()+ 77;
常量汽车s_x =的std ::累加(开始,结束,0.0);

另外,你可以推出自己的非所属的容器状物体。见<一href=\"http://stackoverflow.com/questions/26346688/how-to-initialize-vector-from-array-without-allocating-more-storage-space/26346941#26346941\">this问题一个例子。

Is there any std container which would be fixed size like std::array, but the size would not be compile time, but runtime?

I want to pass a part of some data I have stored in std::array to std::acculumate and similar functions. I do not want to use std::vector (working on embedded platform), therefore I am looking for something in between.

Assume code like this, what I want is something to be used in place of array_part:

#include <array>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>

int main()
{
  std::array<float,100> someData;
// fill the data
  int dataCount = 50;
  std::array_part<float> partOfData(someData.data(),dataCount)); // <<<<< here  
  const auto s_x  = std::accumulate(partOfData.begin(), partOfData.end(), 0.0);

}

If there is no such container, how can I wrap the raw data I have and present them to std::accumulate and other std algorithms?

解决方案

std::accumulate takes iterators. You can pass it iterators that contain the range of interest:

auto start = partOfData.begin() + 42;
auto end = partOfData.begin() + 77;
const auto s_x  = std::accumulate(start, end, 0.0);

Alternatively, you can roll out your own non-owning container-like object. See this question for an example.

这篇关于裹在像阵列STD容器的原始数据,与运行时的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆