0

我知道我在这里做的事情是非常错误的:

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

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

4

2 回答 2

3

为什么不使用 std::copy?

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

请注意,我只是取消对 vn_Vector.end() 的引用,因此它具有与 &*vn_Vector.begin() + nPosition 相同的类型。

于 2013-02-25T21:28:23.567 回答
0

这个答案主要基于@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 )
于 2013-02-25T23:23:03.183 回答