从阵列到矢量接口的转换与C库

时间:2022-05-26 00:55:44

I need to use some low-level C functions provided by a library, to wrap them and provide a 'more high-level layer' ; in this case , my problem is to get a data contained within a buffer, and at least to learn how to do it right, I would like to know what you think would be the thing to do in C++03 AND in C++11.

我需要使用库提供的一些低级C函数来包装它们并提供“更高级的层”;在这种情况下,我的问题是将数据包含在缓冲区中,并且至少要学习如何正确执行,我想知道您认为在c++ 03和c++ 11中应该做什么。

FYI, I am working under Red Hat Linux, with GCC 4.4.7 (so not really C++11-compliant, https://gcc.gnu.org/gcc-4.4/cxx0x_status.html).

FYI,我在Red Hat Linux下工作,使用GCC 4.4.7(所以不是真正的C+ 11兼容,https://gcc.gnu.org/gcc-4.4/cxx0x_status.html)。

Here is a snippet of what I am trying to do :

以下是我正在尝试做的事情的一个片段:

#define DATA_BLOCKS 4096 // the numbers of 16-bit words within the buffer

std::vector<uint16_t> myClass::getData()
{
    uint16_t buffer[DATA_BLOCKS];
    getDataBuf(fd, dma, am, buffer[]); //C-function provided by the library

    // pushing buffer content into vector
    std::vector <uint16_t> myData;
    for(int i=0; i<DATA_BLOCKS; i++)
         myData.pushback(buffer[i]);
    return myData;
}

Within the link I provided, I am not able to find if it's a good idea to proceed like in C++11 to return the 'whole' vector.

在我提供的链接中,我无法找到是否应该像c++ 11那样继续返回“整个”向量。

For a vector, is there a best method to fill 'myData' than using the method 'pushback()' within a loop ?

对于一个向量,是否有比在循环中使用方法“pushback()”更好的方法来填充“myData”?

2 个解决方案

#1


4  

You can do that, it's safe :

你可以这么做,这是安全的:

std::vector<uint16_t> myClass::getData()
{
    std::vector <uint16_t> myData(DATA_BLOCKS);
    getDataBuf(fd, dma, am, myData.data()); //C-function provided by the library
    // Old interface, before c++11 : getDataBuf(fd, dma, am, &myData[0]);

    return myData;
}

or if you want fill given vector:

或者如果你想填充给定的向量:

void myClass::getData(std::vector<uint16_t> &myData)
{
    myData.resize(DATA_BLOCKS);
    getDataBuf(fd, dma, am, myData.data()); //C-function provided by the library
    // Old interface, before c++11 : getDataBuf(fd, dma, am, &myData[0]);
}

Personnally, I have no opinion about returning vector (which will probably use move semantic) or fill a given vector

就个人而言,我对返回向量(可能使用移动语义)或填充给定的向量没有意见

EDIT

编辑

instead of using vector, because you know precisly the size, you can use std::array<std::uint8_t, DATA_BLOCKS> container (new in C++11). This use is same as vector in my examples

不使用vector,因为您知道精确的大小,所以可以使用std::array <::uint8_t data_blocks=""> container(新的c++ 11)。这种用法与我的例子中的向量相同 ,>

EDIT2

EDIT2

vector and array use contiguous storage locations (reference for vector class), so if you get address from first elements, you can access to the second one by incrementing address. The only dangerous point for vector is to be sure to have allocated memory. In both case, I manage to have enough allocated memory : in first example, vector is instanciate with the fill constructor, and in second one I resize vector to corresponding size. This method is describe in the book "Effective STL - 50 Specific Ways to Improve Your Use of the Standard Template Library" [Scott Meyers]. For array, no problem (on condition to declare array with enough memory of course).

向量和数组使用连续的存储位置(向量类的引用),所以如果您从第一个元素获得地址,您可以通过递增地址访问第二个元素。向量唯一的危险点是确保已经分配了内存。在这两种情况下,我都设法拥有足够的分配内存:在第一个示例中,vector是用fill构造函数实例化的,在第二个示例中,我将vector调整为相应的大小。这种方法在《有效的STL - 50特定的方法来改进标准模板库的使用》一书中描述了[Scott Meyers]。对于数组,没有问题(当然,条件是声明具有足够内存的数组)。

#2


1  

data() came with C++11. Using it makes the code more ledgibe in my opinion, and aparently others though so too or it wouldn't have been added.

数据()和c++ 11。在我看来,使用它可以使代码更ledgibe,而其他代码也可以使用它,否则就不会添加它。

#1


4  

You can do that, it's safe :

你可以这么做,这是安全的:

std::vector<uint16_t> myClass::getData()
{
    std::vector <uint16_t> myData(DATA_BLOCKS);
    getDataBuf(fd, dma, am, myData.data()); //C-function provided by the library
    // Old interface, before c++11 : getDataBuf(fd, dma, am, &myData[0]);

    return myData;
}

or if you want fill given vector:

或者如果你想填充给定的向量:

void myClass::getData(std::vector<uint16_t> &myData)
{
    myData.resize(DATA_BLOCKS);
    getDataBuf(fd, dma, am, myData.data()); //C-function provided by the library
    // Old interface, before c++11 : getDataBuf(fd, dma, am, &myData[0]);
}

Personnally, I have no opinion about returning vector (which will probably use move semantic) or fill a given vector

就个人而言,我对返回向量(可能使用移动语义)或填充给定的向量没有意见

EDIT

编辑

instead of using vector, because you know precisly the size, you can use std::array<std::uint8_t, DATA_BLOCKS> container (new in C++11). This use is same as vector in my examples

不使用vector,因为您知道精确的大小,所以可以使用std::array <::uint8_t data_blocks=""> container(新的c++ 11)。这种用法与我的例子中的向量相同 ,>

EDIT2

EDIT2

vector and array use contiguous storage locations (reference for vector class), so if you get address from first elements, you can access to the second one by incrementing address. The only dangerous point for vector is to be sure to have allocated memory. In both case, I manage to have enough allocated memory : in first example, vector is instanciate with the fill constructor, and in second one I resize vector to corresponding size. This method is describe in the book "Effective STL - 50 Specific Ways to Improve Your Use of the Standard Template Library" [Scott Meyers]. For array, no problem (on condition to declare array with enough memory of course).

向量和数组使用连续的存储位置(向量类的引用),所以如果您从第一个元素获得地址,您可以通过递增地址访问第二个元素。向量唯一的危险点是确保已经分配了内存。在这两种情况下,我都设法拥有足够的分配内存:在第一个示例中,vector是用fill构造函数实例化的,在第二个示例中,我将vector调整为相应的大小。这种方法在《有效的STL - 50特定的方法来改进标准模板库的使用》一书中描述了[Scott Meyers]。对于数组,没有问题(当然,条件是声明具有足够内存的数组)。

#2


1  

data() came with C++11. Using it makes the code more ledgibe in my opinion, and aparently others though so too or it wouldn't have been added.

数据()和c++ 11。在我看来,使用它可以使代码更ledgibe,而其他代码也可以使用它,否则就不会添加它。