C++/SystemC:从 C++ 中的数组返回特定​​范围的值

C++/SystemC: Returning a specific range of values from an array in C++

这就是我正在尝试的。我已将结构传递给函数。在函数中,我将结构的值存储在一个数组中。在 returning 时,我只想发送那些根据特定条件在数组中定义的值。例如,假设我有一个 10 的数组定义,我想在函数中根据条件从该数组中 return 只有 5 个值。这是一个示例代码:

sc_uint<8> *arrayfill(struct){
sc_uint<8> array[10];

 array[1] = struct.a;
 array[2] = struct.b;
 ...
 if (struct.trigger == false){
  array[10] =0;
 }
 else 
 {
   array[10] = struct.j;
 }

return array;
}

因为您不能 return 函数中的自动存储数组,所以我建议 returning 一个 std::vector<sc_uint<8>>。它本质上只是将您的 sc_uint<8> 值包装在一个易于使用和移动的动态数组中。

然后根据您的条件 push_back 将您想要 return 的值 vector 简单地


例如:

std::vector<sc_uint<8>> arrayfill(struct){
    std::vecotr<sc_uint<8>> array;
    array.reserve(10); // Reserves space for 10 elements.

    array.push_back(struct.a); // This will be the first element in array, at index 0.
    array.push_back(struct.b); // This will be the second element at index 1.
    ...
    if (struct.trigger == false){
      array.push_back(0);
    }
    else 
    {
      array.push_back(struct.j);
    }
    // At this point, array will have as many elements as push_back has been called.
    return array;
}

使用std::vector::insert添加一个值范围:

array.insert(array.end(), &values[3], &values[6]);

其中 values 是一些数组。上面将在 values 中插入索引 3 到索引 5 的值(不包括范围,不会插入索引 6 处的值)到 array.

的末尾