C++纯一维数组的综合运用
作者:野牛程序员:2023-02-24 14:05:01C++程序设计阅读 2714
以下是一些使用纯一维数组的C++综合应用示例:
数组排序
可以使用标准库函数std::sort
对数组进行排序,也可以实现自己的排序算法。
示例代码:
#include <algorithm> #include <iostream> const int ARRAY_SIZE = 10; int main() { int arr[ARRAY_SIZE] = {10, 8, 6, 4, 2, 1, 3, 5, 7, 9}; // 使用std::sort对数组进行排序 std::sort(arr, arr + ARRAY_SIZE); // 输出排序后的数组 for (int i = 0; i < ARRAY_SIZE; i++) { std::cout << arr[i] << " "; } std::cout << std::endl; return 0; }
数组查找
可以使用线性搜索或二分搜索对数组进行查找。
示例代码:
#include <iostream> const int ARRAY_SIZE = 10; int linear_search(int arr[], int size, int target) { for (int i = 0; i < size; i++) { if (arr[i] == target) { return i; } } return -1; } int binary_search(int arr[], int size, int target) { int left = 0; int right = size - 1; while (left <= right) { int mid = (left + right) / 2; if (arr[mid] == target) { return mid; } else if (arr[mid] < target) { left = mid + 1; } else { right = mid - 1; } } return -1; } int main() { int arr[ARRAY_SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int target = 6; int index1 = linear_search(arr, ARRAY_SIZE, target); std::cout << "Linear search result: " << index1 << std::endl; int index2 = binary_search(arr, ARRAY_SIZE, target); std::cout << "Binary search result: " << index2 << std::endl; return 0; }
数组逆序
可以使用双指针对数组进行逆序操作。
示例代码:
#include <iostream> const int ARRAY_SIZE = 10; void reverse(int arr[], int size) { int left = 0; int right = size - 1; while (left < right) { int temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; left++; right--; } } int main() { int arr[ARRAY_SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // 输出原数组 std::cout << "Original array: "; for (int i = 0; i < ARRAY_SIZE; i++) { std::cout << arr[i] << " "; } std::cout << std::endl; // 对数组进行逆序 reverse(arr, ARRAY_SIZE); // 输出逆序后的数组 std::cout << "Reversed array: "; for (int i = 0; i < ARRAY_SIZE; i++) { std::cout << arr[i] << " "; } std::cout << std::endl; return 0; }
4. 数组去重 可以使用双指针或哈希表对数组进行去重操作。 示例代码: ```cpp #include <iostream> #include <unordered_set> const int ARRAY_SIZE = 10; int remove_duplicates(int arr[], int size) { std::unordered_set<int> hash_set; int j = 0; for (int i = 0; i < size; i++) { if (hash_set.count(arr[i]) == 0) { arr[j] = arr[i]; j++; hash_set.insert(arr[i]); } } return j; } int main() { int arr[ARRAY_SIZE] = {1, 2, 2, 3, 3, 3, 4, 5, 5, 6}; // 输出原数组 std::cout << "Original array: "; for (int i = 0; i < ARRAY_SIZE; i++) { std::cout << arr[i] << " "; } std::cout << std::endl; // 对数组进行去重 int new_size = remove_duplicates(arr, ARRAY_SIZE); // 输出去重后的数组 std::cout << "New array: "; for (int i = 0; i < new_size; i++) { std::cout << arr[i] << " "; } std::cout << std::endl; return 0; }
数组求和
可以使用循环对数组进行求和操作。
示例代码:
#include <iostream> const int ARRAY_SIZE = 10; int sum(int arr[], int size) { int result = 0; for (int i = 0; i < size; i++) { result += arr[i]; } return result; } int main() { int arr[ARRAY_SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int result = sum(arr, ARRAY_SIZE); std::cout << "The sum is: " << result << std::endl; return 0; }
这些示例代码展示了如何使用纯一维数组进行常见的操作,这些操作包括排序、查找、逆序、去重和求和。这些示例可以帮助你更好地理解C++中数组的使用方法。
数组拷贝
可以使用循环将一个数组的元素拷贝到另一个数组中。
示例代码:
#include <iostream> const int ARRAY_SIZE = 10; void copy(int source[], int target[], int size) { for (int i = 0; i < size; i++) { target[i] = source[i]; } } int main() { int arr1[ARRAY_SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int arr2[ARRAY_SIZE]; copy(arr1, arr2, ARRAY_SIZE); // 输出拷贝后的数组 std::cout << "Copied array: "; for (int i = 0; i < ARRAY_SIZE; i++) { std::cout << arr2[i] << " "; } std::cout << std::endl; return 0; }
数组切片
在C++中,数组切片需要使用指针或迭代器实现。
示例代码:
#include <iostream> const int ARRAY_SIZE = 10; void slice(int arr[], int start, int end) { int* p = arr + start; int* q = arr + end; std::cout << "Sliced array: "; for (; p < q; p++) { std::cout << *p << " "; } std::cout << std::endl; } int main() { int arr[ARRAY_SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; slice(arr, 2, 6); return 0; }
这里使用指针将数组切片,并使用循环输出切片后的结果。
数组翻转
可以使用双指针或递归的方式对数组进行翻转操作。
示例代码:
#include <iostream> const int ARRAY_SIZE = 10; void reverse(int arr[], int start, int end) { while (start < end) { int temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; } } int main() { int arr[ARRAY_SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // 输出原数组 std::cout << "Original array: "; for (int i = 0; i < ARRAY_SIZE; i++) { std::cout << arr[i] << " "; } std::cout << std::endl; // 翻转数组 reverse(arr, 0, ARRAY_SIZE - 1); // 输出翻转后的数组 std::cout << "Reversed array: "; for (int i = 0; i < ARRAY_SIZE; i++) { std::cout << arr[i] << " "; } std::cout << std::endl; return 0; }
这些示例代码展示了如何使用纯一维数组进行常见的操作,这些操作包括拷贝、切片和翻转。
野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892

- 上一篇:C++中数组的读入与输出
- 下一篇:C++中纯二维数组与多维数组的综合应用