网上教学网站建设网络营销有哪些推广方式
- 阐述g++ 有哪些常用的选项,该选项有什么作用
选项 | 作用 |
---|---|
-o <file> | 指定输出文件名(默认生成 a.out ) |
-c | 仅编译生成目标文件(.o 文件),不链接 |
-E | 只进行预处理,输出预处理后的代码(展开宏、包含头文件) |
-S | 生成汇编代码(.s 文件) |
选项 | 作用 |
---|---|
-g | 生成调试信息(支持 GDB 调试) |
-ggdb | 生成更详细的 GDB 调试信息 |
-Wall | 开启所有常见警告(未初始化变量、未使用变量等) |
-Wextra | 启用额外警告(如函数参数未使用) |
-Werror | 将警告视为错误,强制修复所有警告 |
-pedantic | 严格遵循 ISO C++ 标准,拒绝非标准语法 |
- 试写一个函数,计算字符串 s中最大连续相同的字符个数。例如,若s 为"aaabbbb",则返回值为4;若s为"abcde",则返回 值为1。
#include <iostream>
using namesapce std;int max_same_char(const char* s) {if (s == nullptr || *s == '\0') return 0; // 处理空指针和空字符串[4,7](@ref)int max_count = 1, current = 1; // 初始值设为1(至少有一个字符)for (int i = 1; s[i] != '\0'; ++i) { // 从第二个字符开始遍历[6,9](@ref)if (s[i] == s[i-1]) {current++;max_count = (current > max_count) ? current : max_count; // 更新最大值[4,8](@ref)} else {current = 1; // 字符变化时重置计数器[9](@ref)}}return max_count;
}
- 编写多个同名函数,实现对指定字符数组中按索引添加元素,按指定字符前添加元素和批量添加元素
#include <iostream>
using namesapce std;
// 按索引插入单个字符
void array_insert(char* arr, int& size, int index, char ch) {if (index < 0 || index > size) return; // 索引越界检查[6](@ref)if (size + 1 >= sizeof(arr)/sizeof(arr[0])) return; // 容量检查[6](@ref)memmove(arr + index + 1, arr + index, size - index); // 移动元素[6,10](@ref)arr[index] = ch;size++;arr[size] = '\0'; // 维护字符串终止符
}// 在指定字符前插入元素
bool array_insert(char* arr, int& size, char target, char ch) {for (int i = 0; i < size; ++i) {if (arr[i] == target) {array_insert(arr, size, i, ch); // 复用索引插入逻辑[2,6](@ref)return true;}}return false; // 未找到目标字符
}// 批量添加元素
void array_insert(char* arr, int& size, const char* elements) {int add_len = strlen(elements);if (size + add_len >= sizeof(arr)/sizeof(arr[0])) return; // 容量检查[10](@ref)memcpy(arr + size, elements, add_len); // 批量复制[5,7](@ref)size += add_len;arr[size] = '\0'; // 维护字符串终止符
}int main() {char buffer[50] = "Hello";int length = strlen(buffer);// 测试索引插入array_insert(buffer, length, 5, '!'); // 末尾插入cout << buffer << std::endl; // 输出: Hello!// 测试字符前插入array_insert(buffer, length, 'e', '3'); // 在第一个'e'前插入cout << buffer << std::endl; // 输出: H3ello!// 测试批量添加array_insert(buffer, length, "World");cout << buffer << std::endl; // 输出: H3ello!Worldreturn 0;
}
-
利用今天学习的函数模板,设计一个排序算法函数,实现对任意类型的数据实现排序
#include <iostream> using namesapce std; // 自定义交换函数模板 template <typename T> void custom_swap(T& a, T& b) {T temp = a;a = b;b = temp; }// 分区函数 template <typename T> int partition(T arr[], int low, int high) {T pivot = arr[high]; // 基准值选择优化可参考文献[3,9](@ref)int i = low - 1;for (int j = low; j <= high - 1; ++j) {if (arr[j] < pivot) {++i;custom_swap(arr[i], arr[j]); // 手动交换元素}}custom_swap(arr[i + 1], arr[high]);return i + 1; }// 快速排序主函数 template <typename T> void quick_sort(T arr[], int low, int high) {if (low < high) {int pi = partition(arr, low, high);quick_sort(arr, low, pi - 1);quick_sort(arr, pi + 1, high);} }int main() {int nums[] = {5, 2, 9, 1, 5};quick_sort(nums, 0, 4); // 排序整型数组string strs[] = {"apple", "banana"};quick_sort(strs, 0, 1); // 排序字符串数组return 0; }
-
C++编写一个可以容纳任意数据类型的链表.。
#include <iostream>
#include <memory>template <typename T>
class LinkedList {
private:struct Node {T data;std::unique_ptr<Node> next;Node(const T& val) : data(val), next(nullptr) {}};std::unique_ptr<Node> head;Node* tail;public:LinkedList() : head(nullptr), tail(nullptr) {}// 添加元素到链表尾部void append(const T& value) {auto newNode = std::make_unique<Node>(value);if (!head) {head = std::move(newNode);tail = head.get();} else {tail->next = std::move(newNode);tail = tail->next.get();}}// 删除指定元素bool remove(const T& value) {Node* current = head.get();Node* prev = nullptr;while (current) {if (current->data == value) {if (prev) {prev->next = std::move(current->next);if (!prev->next) {tail = prev;}} else {head = std::move(current->next);if (!head) {tail = nullptr;}}return true;}prev = current;current = current->next.get();}return false;}// 遍历打印链表内容void print() const {Node* current = head.get();while (current) {cout << current->data;if (current->next) std::cout << " -> ";current = current->next.get();}cout << " -> nullptr" << std::endl;}
};int main() {// 整型链表示例LinkedList<int> intList;intList.append(1);intList.append(2);intList.append(3);intList.print(); // 输出: 1 -> 2 -> 3 -> nullptrintList.remove(2);intList.print(); // 输出: 1 -> 3 -> nullptr// 字符串链表示例LinkedList<std::string> strList;strList.append("Hello");strList.append("World");strList.print(); // 输出: Hello -> World -> nullptrreturn 0;
}