简体   繁体   English

为std :: vector赋值<user-defined>

[英]Assigning values to std::vector<user-defined>

I am working with images and computing radon transform for an object. 我正在处理图像并计算对象的ra变换。 The function that computes the transform accepts vector as input: 计算转换的函数接受向量作为输入:

pixel p;
vector<pixel> segObj  //segObj is the object segmneted from the image

pixel is a user-defined struct defined as: pixel是用户定义的结构,定义为:

struct pixel
{
float x, y;  //x,y coordinates of the pixel
};

Currently, I am doing an element wise assignment to the vector: 目前,我正在对向量进行元素明智的分配:

for(int ix=0; ix < element_count; ix++)
{
f.x = xCoordArray[ix];
f.y = yCoordArray[ix];
segObj.push_back(f);
//xCoordArray and yCoordArray are computed separately
}

The for loop makes it slow when dealing with large images. for循环会使处理大图像时变慢。 Is there a way to assign xCoordArray and yCoordArray directly to vector<pixel>segObj 有没有一种方法可以直接将xCoordArrayyCoordArray分配给vector<pixel>segObj

I am not expereinced with the use of vectors so any help would be appreciated. 我对使用向量没有经验,因此将不胜感激。
Also, If xCoordArray , yCoordArray can be computed as vectors, is there a way to join them so that each vector index as two values. 另外,如果xCoordArrayyCoordArray可以作为向量计算,则有一种方法可以将它们连接起来,以使每个向量索引为两个值。

One reason of being it slow is the reallocation (and copy) done by the vector as the for loop executes. 变慢的原因之一是向量在for循环执行时由向量进行了重新分配(和复制)。 In your case, you could do this: 就您而言,您可以执行以下操作:

segObj.reserve(element_count); //DO THIS

for(int ix=0; ix < element_count; ix++)
{
  f.x = xCoordArray[ix];
  f.y = yCoordArray[ix];
  segObj.push_back(f);
}

This at least will improve the code a little bit, as there is one allocation before the loop, and no reallocation and copy is done when the for loop executes. 至少会稍微改善一下代码,因为在循环之前有一个分配,并且在for循环执行时不会进行重新分配和复制。

If your compiler supports C++11, then you could try this: 如果您的编译器支持C ++ 11,则可以尝试以下操作:

struct pixel
{
  float x,y;
  pixel(float x, float y) : x(x), y(y) {}   //add this constructor
};

segObj.reserve(element_count); //DO THIS

for(int ix=0; ix < element_count; ix++)
{
  segObj.emplace_back(xCoordArray[ix], yCoordArray[ix]);
}

Note that it calls emplace_back , not push_back . 请注意,它调用emplace_back ,而不是push_back This member function is available with C++11 only; 该成员函数仅在C ++ 11中可用。 it constructs the pixel objects in-place. 它就地构建像素对象。 No copy of pixel object is made when adding to the vector: 添加到向量中时,不复制像素对象:

template< class... Args >
void emplace_back( Args&&... args ); (since C++11)

Appends a new element to the end of the container. 将新元素附加到容器的末尾。 The element is constructed in-place, ie no copy or move operations are performed . 元素是就地构造的,即, 不执行复制或移动操作 The constructor of the element is called with exactly the same arguments, as supplied to the function. 使用与提供给函数的参数完全相同的参数调用元素的构造函数。

You can create an input iterator producing pixel objects and assign those directly. 您可以创建一个生成pixel对象的输入迭代器,然后直接分配它们。 You could initialize the objects directly using something along the lines of this: 您可以使用类似于以下内容的方法直接初始化对象:

pixel iterator::operator*() const {
    pixel rc = {
        xCoordArray[index],
        tCoordArray[index]
    };
    return rc;
}

That said, I somewhat doubt that this is indeed the bottleneck: did you profile your code or are you suspecting that this is the problem? 也就是说,我有点怀疑这确实是瓶颈:您是否配置了代码,或者您怀疑这是问题所在?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM