小编Leo*_*eon的帖子

在C++中返回unique()函数

我遇到了以下函数,它对main()传递的数组进行排序,删除重复项,并返回唯一元素的数量.这是最后一点我很难缠头.

int reduce(long ar[], int n) {
  sort(ar, ar + n);
  return unique(ar, ar + n) - ar; // ??? 
}
Run Code Online (Sandbox Code Playgroud)

据我所知,unique()返回一个指向段末尾的指针,该段存储数组中的唯一值.但我不明白为什么从迭代器中减去数组名会导致int等于唯一元素的数量,或者为什么unique(ar, ar+n)不能对int进行类型转换以获得相同的结果.

c++ arrays iterator stl unique

3
推荐指数
2
解决办法
1423
查看次数

C中的按位逻辑

我对按位操作有一定的了解,但这个功能只是我的头脑.

void binary_print(unsigned int value) {
  unsigned int mask = 0xff000000;   // Start with a mask for the highest byte.
  unsigned int shift = 256*256*256; // Start with a shift for the highest byte.
  unsigned int byte, byte_iterator, bit_iterator;

  for (byte_iterator=0; byte_iterator < 4; byte_iterator++) {
    byte = (value & mask) / shift; // Isolate each byte.
    printf(" ");

    for (bit_iterator=0; bit_iterator < 8; bit_iterator++) {
      // Print the byte's bits.
      if (byte & 0x80) // If the highest bit …
Run Code Online (Sandbox Code Playgroud)

c binary bit operator-keyword

2
推荐指数
1
解决办法
5088
查看次数

标签 统计

arrays ×1

binary ×1

bit ×1

c ×1

c++ ×1

iterator ×1

operator-keyword ×1

stl ×1

unique ×1