std::从向量的位置复制到向量的位置
std::copy from vector's position to vector's position
这可能是我非常累,但我无法弄清楚如何将向量的一部分复制到新向量中。
我正在尝试做的是在 std::vector(其中 char 被类型化为字节)中找到起始标记所在的位置,并从那里复制数据,直到结束标记(在末尾,长度为 7 个字符)。
typedef char byte;
std::vector<byte> imagebytes;
std::vector<byte> bytearray_;
for ( unsigned int i = 0; i < bytearray_.size(); i++ )
{
if ( (i + 5) < (bytearray_.size()-7) )
{
std::string temp ( &bytearray_[i], 5 );
if ( temp == "<IMG>" )
{
// This is what isn't working
std::copy( std::vector<byte>::iterator( bytearray_.begin() + i + 5 ),
std::vector<byte>::iterator( bytearray_.end() - 7 )
std::back_inserter( imagebytes) );
}
}
}
我知道这个循环看起来很可怕,我愿意接受建议!请注意,bytearray_包含图像或音频文件的原始字节。因此向量。
答案很简单:只是复制,不要循环。循环已经在 std::copy
内
typedef char byte;
std::vector<byte> imagebytes;
std::vector<byte> bytearray_;
// Contents of bytearray_ is assigned here.
// Assume bytearray_ is long enough.
std::copy(bytearray_.begin() + 5,
bytearray_.end() - 7,
std::back_inserter( imagebytes) );
除了复制,您还可以直接从现有向量构造一个新向量:
std::vector<byte> imagebytes(bytearray_.begin() + i + 5, bytearray_.end() - 7);
相关文章:
- 将值指定给向量(2D)的向量中的某个位置
- 使用迭代器时如何访问对象在向量中的位置?
- 如何将元素添加到向量的字符串位置
- 将一个向量插入另一个向量的某个位置
- 向量的内存位置不连续
- 创建一个类来访问和指定向量类型,并构建一个获取位置并为其分配区域的类
- 如何从字符串向量中选择第 n 个位置?
- 在多维向量中的特定位置添加一个值
- 如果不初始化结构中的向量,它会自动为空还是具有随机内存位置的值?
- 如何在向量中保存位置以供以后插入?
- 查找 2D 向量的位置,该向量是类C++的一个元素
- 使用向量的函数崩溃,调试器说访问冲突读取位置
- 如何使用向量 c++ 迭代文件中单词的位置
- 在 c++ 中检查 if 条件中的向量位置范围
- 这是查找向量中元素位置的合法方法吗?
- 如何在C 2D向量中找到对象(而不是int)的位置
- C 从特定位置插入其他向量中的载体
- 向量如何在特定位置调用复制构造函数
- 如何在特定位置不大的推动_back向量
- OpenGL位置向量