c++ - 创建索引数组的标准库函数,其对应值为给定数字

标签 c++ arrays c++11 c++-standard-library

我有一个名为 board 的 C 风格数组,其中包含一些 char。我正在尝试创建一个 std::arraystd::vector (两者都可以,尽管 std::array 是更可取)来存储 board 的所有具有特定值的索引(在我的例子中,0)。
我编写的这段代码功能正常并且运行良好:

std::vector<int> zeroes;
zeroes.reserve(16);
//board has 16 elements, so zeroes.size() will never be larger than 16.
//I used this reserve for speedup - the compiler doesn't require it.
for (int i = 0; i < 16; ++i)
{
    if (board[i] == 0)
    {
        zeroes.push_back(i);
    }
}

但是,根据过去的经验,只要存在可以替换我的部分代码的 std 函数,它就会更简洁,因此在风格上更受欢迎,而且速度更快。我的函数似乎是一个相当基本的操作——我知道有一个标准函数*可以访问一个数组的索引,该数组包含一个值,当该值在数组中只出现一次**时。那么,假设存在多个这样的索引,是否有一个标准函数可以创建包含一个值的索引数组?


* 从技术上讲,两个嵌套函数调用:int x = std::distance(board, std::find(board, board + 16, 0));。参见 the accepted answer here .
** 好吧,如果存在多个具有所需值的索引,它仍然有效,但它只返回第一个这样的索引,这在我的上下文中不是很有用。


编辑: 由于其中一个答案误解了这个问题,我会澄清我在寻找什么。假设我们有:

char board[16] = {0, 2, 0, 4,
                  2, 4, 8, 2,
                  0, 0, 8, 4,
                  2, 0, 0, 2};

现在,我正在寻找的索引是 {0, 2, 8, 9, 13, 14} 因为 board[0] = 0, board[2] = 0board[8] = 0 等,这些是唯一满足该属性的数字。

最佳答案

这是一个使用 std::iotastd::remove_if 的解决方案:

#include <algorithm>
#include <iostream>

int main () {
  const std::size_t board_size = 16;
  char board [board_size] = {
    0, 2, 0, 4,
    2, 4, 8, 2,
    0, 0, 8, 4,
    2, 0, 0, 2
  };

  // Initialize a zero-filled vector of the appropriate size.
  std::vector<int> zeroes(board_size);

  // Fill the vector with index values (0 through board_size - 1).
  std::iota(zeroes.begin(), zeroes.end(), 0);

  // Remove the index values that do not correspond to zero elements in the board.
  zeroes.erase(std::remove_if(zeroes.begin(), zeroes.end(), [&board] (int i) {
    return board[i] != 0;
  }), zeroes.end());

  // Output the resulting contents of the vector.
  for (int i : zeroes) {
    std::cout << i << std::endl;
  }
}

程序的输出(demo):

0
2
8
9
13
14

关于c++ - 创建索引数组的标准库函数,其对应值为给定数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37557743/

相关文章:

php - 返回键包含此字符串的值

php - 在 nusoap 和 PHP 中发送数据数组

c++ - 默认构造函数中的歧义

c++ - 防止覆盖和/或隐藏基类函数 (C++ 11)

c++ - 赋值运算符重载期间类的指针成员

c++ - 使用模板类参数的模板类特化

mysql - 遍历mysql语句中的数组字段?

c++ - 为什么下面的表达式表征缩小转换?

c++ - 现金流的财务建模中的继承与特定类型

c++ - 如何创建此标记 union 的实例?关于已删除构造函数的编译器错误