如何使用 std::copy 当类有"无符号字符和运算符[]"时
How to use std::copy when class has `unsigned char &operator[]`
我正在使用一个名为Buffer的类,它具有以下运算符:
unsigned char &operator[](size_t index)
我正在尝试像这样从中复制:
std::copy(buf[96], buf[96 + 32], my_uint32_t);
我认为它不起作用
/usr/include/c++/7/bits/stl_algobase.h: In instantiation of '_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = unsigned char; _OI = unsigned int]':
/usr/include/c++/7/bits/stl_algobase.h:422:45: required from '_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = unsigned char; _OI = unsigned int]'
/usr/include/c++/7/bits/stl_algobase.h:455:8: required from '_OI std::copy(_II, _II, _OI) [with _II = unsigned char; _OI = unsigned int]'
/home/project/SimplePacketCrafter.h:36:95: required from here
/usr/include/c++/7/bits/stl_algobase.h:377:57: error: no type named 'value_type' in 'struct std::iterator_traits<unsigned char>'
typedef typename iterator_traits<_II>::value_type _ValueTypeI;
^~~~~~~~~~~
/usr/include/c++/7/bits/stl_algobase.h:378:57: error: no type named 'value_type' in 'struct std::iterator_traits<unsigned int>'
typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
^~~~~~~~~~~
/usr/include/c++/7/bits/stl_algobase.h:379:64: error: no type named 'iterator_category' in 'struct std::iterator_traits<unsigned char>'
typedef typename iterator_traits<_II>::iterator_category _Category;
^~~~~~~~~
/usr/include/c++/7/bits/stl_algobase.h:383:9: error: no type named 'value_type' in 'struct std::iterator_traits<unsigned char>'
const bool __simple = (__is_trivial(_ValueTypeI)
~~~~~~~~~~~~~~~~~~~~~~~~~~
&& __is_pointer<_II>::__value
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
&& __is_pointer<_OI>::__value
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
&& __are_same<_ValueTypeI, _ValueTypeO>::__value);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/7/bits/stl_algobase.h:386:44: error: no type named 'iterator_category' in 'struct std::iterator_traits<unsigned char>'
return std::__copy_move<_IsMove, __simple,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
_Category>::__copy_m(__first, __last, __result);
因为我想我应该在前两个中传递一个地址。还有
unsigned char *index(size_t index)
但它也不起作用
方法可以在这里看到
std:copy()
接受迭代器,而不是引用。 这就是您收到错误的原因。
原始指针可以用作迭代器,前提是所指向的元素按顺序存储在内存中。 因此,假设缓冲区的operator[]
返回对单个内存缓冲区中元素的引用,则可以使用&
地址运算符获取operator[]
引用的char
的内存地址,然后std::copy()
可以在这两个地址之间迭代:
std::copy(&buf[96], &buf[96 + 32], my_uint32_t);
或者,缓冲区的index()
和c_index()
方法返回等效指针:
std::copy(buf.index(96), buf.index(96 + 32), my_uint32_t);
std::copy(buf.c_index(96), buf.c_index(96 + 32), my_uint32_t);
相关文章:
- 如何在C++中正确重载运算符转换字符*?
- 如何使用 std::copy 当类有"无符号字符和运算符[]"时
- 字符 * 未从重载运算符或内存管理问题正确返回
- iStream 运算符跳过空格字符
- C++11+=当RHS有多个字符串或字符时,运算符不起作用
- 将文本文件数据读入字符数组时提取运算符的歧义
- C++11 无符号字符在使用运算符 = 时变为 int
- CPP 中的 ++ 运算符对字符是否有任何重载?
- 为什么使用 .replace 运算符时字符串的长度不超过 16 个字符?
- C++ 输出流运算符 <<() 中的字符何时加宽?
- 重载>>运算符和读取字符
- 如何将字符字符串用作数学运算符
- 如何使用字符串字符数组中的 ++ 运算符访问下一个字符串
- 用户输入逻辑运算符符号或数字,如果输入字符或字符串则无效
- 运算符 != "seems"不使用字符
- 是否可以超载[]运算符在不定义类的情况下访问特定的字符
- C++ 字符 - 关系运算符
- 将作为数学运算符的字符添加到随机字符串中
- 字符指针包装器的运算符>>
- 帮助理解 C++ 中的常量字符* 运算符