C++用std::vector的一些值创建一个新数组

C++ Create a new array off of some values of std::vector?

本文关键字:新数组 数组 一个 std vector C++ 创建      更新时间:2023-10-16

我知道我在这里做的事情大错特错:

void Example( void )
{
    // DECLARE LOCAL VARIABLES
    ::UINT nPosition = 5;
    ::UINT nLength = 5;
    std::vector< ::UINT >vn_VectorA;
    ::UINT *an_ArrayA = new ::UINT[ nLength ];
    // FILL vn_VectorA WITH DECIMAL VALUE OF "HelloWorld!!!"
    // I KNOW THIS IS AN UGLY WAY OF DOING IT. BUT FOR DEMONSTRATION PURPOSES,
    // I CAN CARELESS.
    vn_VectorA.push_back( 72 );     // H
    vn_VectorA.push_back( 101 );    // e
    vn_VectorA.push_back( 108 );    // l
    vn_VectorA.push_back( 108 );    // l
    vn_VectorA.push_back( 111 );    // o
    vn_VectorA.push_back( 87 );     // W
    vn_VectorA.push_back( 111 );    // o
    vn_VectorA.push_back( 114 );    // r
    vn_VectorA.push_back( 108 );    // l
    vn_VectorA.push_back( 100 );    // d
    vn_VectorA.push_back( 33 );     // !
    vn_VectorA.push_back( 33 );     // !
    vn_VectorA.push_back( 33 );     // !
    // Copy the desire values of vn_VectorA to an_ArrayA
    for( ::UINT nCopyIndex = nPosition, nArrayAIndex = 0; nArrayAIndex != nLength; nArrayAIndex ++, nCopyIndex ++ )
    {
        an_ArrayA[ nArrayAIndex ] = vn_VectorA[ nCopyIndex ];
#ifdef DEBUG
        std::cout << an_ArrayA[ nArrayAIndex ] << ' ';
#endif // DEBUG
    }
};

这是我用来复制std::vector:的某些值

    // Copy the desire values of vn_VectorA to an_ArrayA
    for( ::UINT nCopyIndex = nPosition, nArrayAIndex = 0; nArrayAIndex != nLength; nArrayAIndex ++, nCopyIndex ++ )
    {
        an_ArrayA[ nArrayAIndex ] = vn_VectorA[ nCopyIndex ];
#ifdef DEBUG
        std::cout << an_ArrayA[ nArrayAIndex ] << ' ';
#endif // DEBUG
    }

运行代码后,它应该打印:87 111 114 108 100

那么,我该怎么做呢???

为什么不使用std::copy?

std::copy(&*vn_Vector.begin() + nPosition, &*vn_Vector.end(), an_ArrayA);

注意,我只是取消引用vn_Vector.end((,所以它的类型与&vn_Vector.begin((+nPosition。

这个答案主要基于@john.pavan的答案。

#pragma region DOCUMENTATION ON: STD_COPY_VECTOR_TO_ARRAY
//////////////////////////////////////////////////////////////////////////////
// MACRO: STD_COPY_VECTOR_TO_ARRAY( Vector, Position, Length, Array )       //
//                                                                          //
// DESCRIPTION:                                                             //
// A defined macro that is created to copy selected values of std::vector   //
// an array. This defined macro takes 4 arguments.                          //
//                                                                          //
// ARGUMENTS:                                                               //
// Vector - The std::vector that contains the values that will be copied.   //
// Position - Position of the first value from Vector to be copied.         //
// Note: The first value of Position is marked by the value of 0 and not 1. //
// Length - Amount of values to be copied from Vector.                      //
// Array - The Array that the values will be copied to.                     //
//                                                                          //
// TIPS:                                                                    //
// #1 - To copy all values within Vector, set the value of Position to 0    //
// and set the value of Length to the size of Vector (std::vector::size).   //
//////////////////////////////////////////////////////////////////////////////
#pragma endregion
#define STD_COPY_VECTOR_TO_ARRAY( Vector, Position, Length, Array )         
                                  std::copy( Vector.begin( ) + Position,    
                                  Vector.begin( ) + Position + Length ,     
                                  Array )