数组(向量)中元素的起始索引和结束索引,它们大于值

时间:2014-04-17 15:27:57

标签: c++ intel-ipp

给出这样的数组:

{1, 3, 11, 2, 24, 13, 5....}

数组长度可能大于1,000。

如果元素的值不合适,例如大于10,则应将其替换为适当的值。在这种情况下,通过线性插值计算适当的值。

例如:

  

Arr = {1,3,11,2,24,13,5 ......};

新数组应为:

  

NewArr = {1,3,3 +(2-3)/ 2,2,2 +(5-2)/ 3,2 + 2 *(5-2)/ 3,5,......}

为了做到这一点,我必须知道不合适元素的起始和结束索引

  

起始和结束指数应为(2,2),表示" 11" (4,5)表示" 24,13"

我试过了for loop。但效率不高。 然后我搜索IPP API并没有得到结果。 :(

有更好的主意吗?

感谢您的帮助,:)。

BTW IPP API将是更好的选择。

更新

示例代码:

int arr[] = {1, 3, 11, 2, 24, 13, 5....};

/// find the starting index and ending index of inappropriate values
/// (2,2) (4,5).
int i = 0; 
std::map<int,int> Segments;
if(arr[i] > Threshold)
{
    int b = i;
    while(arr[i] > Threshold )
        i ++;
    int e = i;
    Segments.insert(std::map<int,int>::value_type(b,e));
}

/// linear interpolation
for(std::map<int,int>::iterator i = 0; i != Segments.end(); i ++) /// len means the number of inappropriate segments  
{
    //// linear interpolation of each segments
    int b = i->first;
    int e = i->second;
    int num = e - b + 1;
    float step = (arr[e+1]-arr[b-1]) / num; // For short. The case that b=0 or e=len-1 is not considered. 
    for(int j = b; j <= e; j ++)
        arr[j] = arr[j-1] + step;
}

UPDATE2 :  感谢你的帮助。但基于这些问题的答案:Speed accessing a std::vector by iterator vs by operator[]/index?Why use iterators instead of array indices?,两种形式(对于vs迭代器)的效率几乎相同。所以iterator可能不够好。

我通常使用SIMDIPP API作为优化选项。但我没有弄清楚,因为所有find API只获得指定元素的第一次出现

如果我知道某一天,我会更新解决方案。 :)

3 个答案:

答案 0 :(得分:1)

如果要搜索特定值,并从符合特定条件的向量中替换项,则可以使用transform()在一行中完成。

也可以使用replace_if(),但考虑到你的问题的模糊描述,我不知道替换值是否需要根据原始值而变化(replace_if需要一个恒定的替换值)。那么现在让我们使用std :: transform()。

#include <algorithm>
#include <vector>

struct Transformer 
{
   bool ThisNumberNeedsTransformation(int num) { 
     // you fill this in.  Return true if number needs to be changed, false otherwise
   }

   int TransformNumber(int num) {
      // you fill this in.  Return the changed number, given the original number.
   }

   int operator()(int num)
   {
      if ( ThisNumberNeedsTransformation(num) )
          return TransformNumber(num);
      return num;
   }
};

int main()
{
     std::vector<int> intVector;
     //...
     std::transform(intVector.begin(), intVector.end(), intVector.begin(), Transformer());
}

基本上,struct用作函数对象。对于intVector中的每个项目,函数对象将对该数字进行操作。如果数字符合条件,则转换并返回数字,否则返回原始数字。

由于您没有真正阐明更改数字的标准,因此这种方法可以为您的问题提供更灵活的解决方案。您需要做的只是填写我在Transformer结构中打开的两个函数,然后事情应该正常工作。

如果您的需求更复杂,可以扩展函数对象Transformer以包含成员变量,或者只是简单地放置,可以像您想要的那样复杂。

另外请记住,如果您正在计算这些内容,计划发布时间,优化构建。不要计时“调试”或未优化的构建。

答案 1 :(得分:0)

我不完全确定“不合适元素的起始和结束索引”是什么意思,所以我假设你只是指索引

在这里使用矢量将是一个很好的方法:

std::vector<int> the_stuff {1, 3, 11, 2, 24, 13, 5, ... };
std::vector<int>::iterator it = the_stuff.begin();

while (it != the_stuff.end()
{
   if (*it > 10) { // do stuff };
   etc.
}

你明白了。使用vector,它应该让事情变得更容易。在闲暇时搜索/插入/获取索引/删除/等。

答案 2 :(得分:0)

如果将数字存储在std :: vector中,则可以通过迭代器迭代数组。一旦找到满足条件且需要删除的元素,就可以将其删除,同时将迭代器分配给下一个元素。这将是最有效的方式:

以下是您的代码:

std::vector<int> intVector;
for(auto it = intVector.begin(); it != intVector.end(); ++it)
{
    if (*it > 10)
    {
        it = intVector.erase(it);
    }
}