可以通过以下步骤实现:
以下是一个示例程序:
#include <iostream>
using namespace std;
int* findIndexes(int* arr, int size, int target, int& count) {
int* indexes = new int[size]; // 创建索引数组
count = 0; // 初始化索引数量
for (int i = 0; i < size; i++) {
if (*arr == target) { // 判断当前元素是否与目标元素相等
indexes[count] = i; // 存储当前索引到索引数组中
count++; // 增加索引数量
}
arr++; // 指针移动到下一个元素
}
return indexes;
}
int main() {
int arr[] = {1, 2, 3, 2, 4, 2, 5};
int size = sizeof(arr) / sizeof(arr[0]);
int target = 2;
int count;
int* indexes = findIndexes(arr, size, target, count);
cout << "Indexes of target element " << target << ": ";
for (int i = 0; i < count; i++) {
cout << indexes[i] << " ";
}
cout << endl;
delete[] indexes; // 释放索引数组的内存
return 0;
}
这个程序使用指针遍历数组,查找与目标元素相等的元素,并将其索引存储到一个动态分配的索引数组中。最后,打印出所有找到的索引。
这个程序适用于需要查找数组中所有与目标元素相等的元素的场景。腾讯云提供了多种云计算产品,例如云服务器、云数据库、云存储等,可以根据具体需求选择适合的产品。
小程序·云开发官方直播课(数据库方向)
Elastic 实战工作坊
Elastic 实战工作坊
云+社区技术沙龙[第17期]
云+社区技术沙龙[第8期]
T-Day
Elastic 中国开发者大会
腾讯云GAME-TECH游戏开发者技术沙龙
腾讯云GAME-TECH游戏开发者技术沙龙
云+社区技术沙龙[第6期]
云+社区沙龙online [国产数据库]
Techo Day 第三期
领取专属 10元无门槛券
手把手带您无忧上云