C++用std::vector的一些值创建一个新数组
C++ Create a new array off of some values of std::vector?
我知道我在这里做的事情大错特错:
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 )
相关文章:
- 新的整数数组向右移动?
- 标量"新 T "与数组"新 T[1]"
- 如何将标准::字符串内容克隆到新的 char[] 数组
- C++ strcpy_s复制到新的字符数组时引发错误
- 无限数组C++在一个表达式中使用两个新值调整数组大小
- 新动态字符数组末尾的空字符
- c ++新的C数组分配,RAII或简单的shared_ptr/boost::shared_array
- 返回新分配的数组
- 数组的末尾指向新分配的数组C++
- 如何创建新的字符串数组
- 动态数组:将中两个动态数组中元素的值添加到新的动态数组中
- 一组数组添加到一个新数组
- 将新值写入数组
- 按值传递字符数组,并从函数返回一个新的字符数组
- 用不同的指定值初始化新的c++数组
- [C++]在此代码之前不存在的析构函数中引发的内存错误。尝试创建一个新的动态数组并填充它
- 动态分配一个新的整数数组
- 将两个字符串的相似性写入新的动态数组的最简单方法
- 如何在C++中动态创建新的 2D 数组
- 分配新的布尔数组指针