找到 34423 篇文章 关于编程

C++ 中的 Makefile 及其应用

Ayush Gupta
更新于 2020-04-01 06:46:17

180 次浏览

在本教程中,我们将讨论一个程序来理解 C++ 中的 Makefile 及其应用。任务是使用 Makefile 拆分整个程序。通常通过创建包含所有类/功能的 .cpp 文件和 .h 文件并将它们链接在一起来完成。示例main.cpp#include #include "function.h" using namespace std; //主执行程序 int main(){    int num1 = 1;    int num2 = 2;    cout

使用 C++ STL 的克鲁斯卡尔最小生成树

Ayush Gupta
更新于 2020-04-01 06:43:15

417 次浏览

在本教程中,我们将讨论一个程序来理解使用 C++ STL 的克鲁斯卡尔最小生成树。为此,我们将提供一个连通的、无向的和加权的图。我们的任务是计算给定图的最小生成树。示例 在线演示#include using namespace std; typedef pair iPair; //图的结构 struct Graph{    int V, E;    vector< pair > edges;    Graph(int V, int E){       this->V = V;       this->E = E;    }    void addEdge(int u, int v, int w){       edges.push_back({w, {u, v}});    } ... 阅读更多

C++ 中的 lower_bound

Ayush Gupta
更新于 2020-04-01 06:41:03

236 次浏览

在本教程中,我们将讨论一个程序来理解 C++ 中的 lower_bound。C++ 中的 lower_bound() 方法用于返回容器对象中第一个不小于给定值的数字。示例 在线演示#include int main(){    std::vector v{ 10, 20, 30, 40, 50 };    std::cout

C/C++ 中的 lseek() 用于读取交替的第 n 个字节并将其写入另一个文件

Ayush Gupta
更新于 2020-04-01 06:38:41

588 次浏览

在本教程中,我们将讨论一个程序来理解如何读取交替的第 n 个字节并将其写入另一个文件。为此,我们将提供两个 .txt 文件。我们的任务是使用 lseek() 将一个文件的内容写入另一个文件,lseek() 用于更改文件描述符的指针。示例#include #include #include #include void func(char arr[], int n){    int f_write = open("start.txt", O_RDONLY);    int f_read = open("end.txt", O_WRONLY);    int count = 0;    while (read(f_write, arr, 1)){       if (count < n) ... 阅读更多

C++ 程序中的迭代器失效

Ayush Gupta
更新于 2020-04-01 06:47:34

110 次浏览

在本教程中,我们将讨论一个程序来理解 C++ 中的迭代器失效。在迭代容器对象元素时,如果我们不应用边界检查,有时可能会导致迭代器失效。这主要是由于容器对象形状和大小的变化造成的。示例 在线演示#include using namespace std; int main() {    //声明一个向量    vector v{1, 5, 10, 15, 20};    //在执行期间更改向量    //这将导致边界失效    for (auto it=v.begin();it!=v.end();it++)       if ((*it) == 5)          v.push_back(-1);    for (auto ... 阅读更多

C++ 中 std::sort() 的内部细节

Ayush Gupta
更新于 2020-04-01 06:31:34

198 次浏览

在本教程中,我们将讨论一个程序来理解 C++ 中 std::sort() 的内部细节。std::sort() 函数用于使用元素比较对数组进行排序。如果我们查看 std::sort() 的深入功能,它使用 IntroSort 算法对容器对象的元素进行排序。示例 在线演示#include using namespace std; int main(){    int arr[] = {1, 5, 8, 9, 6, 7, 3, 4, 2, 0};    int n = sizeof(arr)/sizeof(arr[0]);    sort(arr, arr+n);    cout

C/C++ 中的整数文字(前缀和后缀)

Ayush Gupta
更新于 2020-04-01 06:29:25

2K+ 次浏览

在本教程中,我们将讨论一个程序来理解 C/C++ 中的整数文字(前缀和后缀)。整数文字是直接在源代码中表示的整数值的文字。此外,它们有两种类型:前缀 - 前缀表示值的基数。例如,0x10 表示十六进制值,其中 0x。后缀 - 后缀表示值的类型。例如,8465484156155LL 表示长长整数。示例 在线演示#include using namespace std; int main(){    //前缀    cout

C/C++ 中的 INT_MAX 和 INT_MIN 及其应用

Ayush Gupta
更新于 2020-04-01 06:30:53

3K+ 次浏览

在本教程中,我们将讨论一个程序来理解 C/C++ 中的 INT_MAX 和 INT_MIN。INT_MIN 和 INT_MAX 是定义为设置变量/元素的最小值和最大值的宏。示例 在线演示#include int main(){    printf("%d", INT_MAX);    printf("%d", INT_MIN);    return 0; }输出2147483647 -2147483648应用计算数组中的 MIN 值示例 在线演示#include //计算数组中的最小元素 int compute_min(int arr[], int n){    int MIN = INT_MAX;    for (int i = 0; i < n; i++)    MIN = std::min(MIN, arr[i]);    std::cout

使用 C++ STL 的插入排序

Ayush Gupta
更新于 2020-04-01 06:20:53

618 次浏览

在本教程中,我们将讨论一个程序来理解使用 C++ STL 的插入排序。在这里,我们使用 std::upper_bound 查找错误位置的元素,然后旋转数组的未排序部分使其排序。示例 在线演示#include //执行插入排序的函数 void insertionSort(std::vector &vec){    for (auto it = vec.begin(); it != vec.end(); it++){       auto const insertion_point =       std::upper_bound(vec.begin(), it, *it);       std::rotate(insertion_point, it, it+1);    } } //打印数组 void print(std::vector vec){    for( int x : vec)    std::cout

C++ 程序中 STL set 的插入和删除

Ayush Gupta
更新于 2020-04-01 06:18:48

392 次浏览

在本教程中,我们将讨论一个程序来理解 C++ 中 STL set 的插入和删除。set 是一个容器元素。使其独一无二的属性是它只能包含唯一元素,并且可以以排序的方式循环遍历。示例插入 在线演示#include #include using namespace std; int main(){    set st;    //声明迭代器    set::iterator it = st.begin();    set::iterator it1, it2;    pair< set::iterator,bool> ptr;    //插入单个元素    ptr = st.insert(20);    if (ptr.second)       cout

广告

© . All rights reserved.