找到 34423 篇文章 关于编程

使用 C/C++ 程序创建目录或文件夹

Ayush Gupta
更新于 2020-03-16 10:07:19

3K+ 浏览量

在本教程中,我们将讨论一个使用 C/C++ 程序创建目录或文件夹的程序。要创建一个新目录,我们将使用 mkdir() 命令。请注意,给定的代码仅适用于 Windows 编译器。示例#include #include #include #include void main(){    int check;    char* dirname = "tutorialspoint";    clrscr();    check = mkdir(dirname);    //检查目录是否已创建    if (!check)       printf("目录已创建");    else {       printf("无法创建目录");       exit(1);    }    getch();    system("dir/p");    getch(); }输出目录已创建

使用 C++ STL 计算长度为二的连续子字符串的不同数量

Ayush Gupta
更新于 2020-03-16 10:04:44

89 浏览量

在本教程中,我们将讨论一个使用 C++ STL 计算长度为二的连续子字符串的不同数量的程序。为此,我们将提供一个字符串。我们的任务是从给定字符串中计算并打印所有唯一的长度为二的子字符串。示例 在线演示#include using namespace std; void calc_distinct(string str){    map dPairs;    for (int i=0; i

使用 C++ STL 中的 Set 计算逆序对

Ayush Gupta
更新于 2020-03-16 10:02:31

178 浏览量

在本教程中,我们将讨论一个使用 C++ STL 中的 set 计算逆序对的程序。逆序对计数是衡量数组距离完全排序程度的指标。如果数组已经排序,则逆序对计数将为 0。示例 在线演示#include using namespace std; //返回逆序对计数 int get_Icount(int arr[],int n){    multiset set1;    set1.insert(arr[0]);    int invcount = 0; //初始化结果    multiset::iterator itset1;    for (int i=1; i

使用 C++ 中的 STL 在二进制数组中计算 1 和 0 的数量

Ayush Gupta
更新于 2020-03-16 10:00:27

332 浏览量

在本教程中,我们将讨论一个使用 C++ 中的 STL 在二进制数组中计算 1 和 0 的数量的程序。为此,我们将提供一个数组。我们的任务是计算数组中 0 和 1 的数量。示例 在线演示#include using namespace std; //检查元素是否为 1 bool isOne(int i){    if (i == 1)       return true;    else       return false; } int main(){    int a[] = { 1, 0, 0, 1, 0, 0, 1 };    int n = sizeof(a) / sizeof(a[0]);    int count_of_one = count_if(a, a + n, isOne);    cout

使用 C++ STL 中的 Set 计算右侧较小的元素数量

Ayush Gupta
更新于 2020-03-16 09:58:45

219 浏览量

在本教程中,我们将讨论一个使用 C++ STL 中的 set 计算右侧较小的元素数量的程序。为此,我们将提供一个数组。我们的任务是构造一个新数组,并在其位置添加当前元素右侧较小元素的数量。示例 在线演示#include using namespace std; void count_Rsmall(int A[], int len){    set s;    int countSmaller[len];    for (int i = len - 1; i >= 0; i--) {       s.insert(A[i]);       auto it = s.lower_bound(A[i]);       countSmaller[i] = ... 阅读更多

在 C++ 中计算排序数组中较小的元素数量

Ayush Gupta
更新于 2020-03-16 09:55:50

210 浏览量

在本教程中,我们将讨论一个在 C++ 中计算排序数组中较小的元素数量的程序。在这里,我们将给出一个数字,我们的任务是计算排序数组中所有小于给定数字的元素。示例 在线演示#include using namespace std; int countSmaller(int arr[], int n, int x){    return upper_bound(arr, arr+n, x) - arr; } int main(){    int arr[] = { 10, 20, 30, 40, 50 };    int n = sizeof(arr)/sizeof(arr[0]);    cout

C/C++ 中的核心转储(段错误)

Ayush Gupta
更新于 2020-03-16 09:54:10

5K+ 浏览量

在本教程中,我们将讨论一个了解 C/C++ 中核心转储(段错误)的程序。它发生的原因包括代码尝试写入只读内存或尝试访问已损坏的内存位置。示例修改字符串字面量int main(){    char *str;    str = "GfG";    *(str+1) = 'n';    return 0; }访问数组索引范围之外#include using namespace std; int main(){    int arr[2];    arr[3] = 10;    return 0; }访问已释放的地址#include #include int main(void){    int* p = malloc(8);    *p = 100;    free(p);    *p = 110;    return 0; }输出程序异常终止

在 C/C++ 中将字符串转换为数字

Ayush Gupta
更新于 2020-03-16 09:52:05

200 浏览量

在本教程中,我们将讨论一个了解如何在 C/C++ 中将字符串转换为数字的程序。C/C++ 提供两种方法将字符串转换为数字。示例 在线演示使用 sscanf()#include int main(){    const char *str = "12345";    int x;    sscanf(str, "%d", &x);    printf("x 的值为:%d", x);    return 0; }输出x 的值为:12345使用 stoi() 在线演示#include #include using namespace std; int main(){    string str1 = "45";    string str2 = "3.14159";    string str3 = "31337 geek";    int myint1 = stoi(str1);    int myint2 = stoi(str2);    int myint3 = stoi(str3);    cout

在 C/C++ 中将字符串转换为整数数组

Ayush Gupta
更新于 2020-03-16 09:50:25

3K+ 浏览量

在本教程中,我们将讨论一个了解如何在 C/C++ 中将字符串转换为整数数组的程序。为此,我们将创建一个新数组。遍历给定的字符串,如果字符是逗号“,”,则继续下一个字符,否则将其添加到新数组中。示例 在线演示#include using namespace std; //将字符串转换为整数数组 void convert_array(string str){    int str_length = str.length();    int arr[str_length] = { 0 };    int j = 0, i, sum = 0;    //遍历字符串    for (i = 0; str[i] != ... 阅读更多

使用 C++ 中的 STL 将整个字符串转换为大写或小写

Ayush Gupta
更新于 2020-03-16 09:47:51

4K+ 浏览量

在本教程中,我们将讨论一个了解如何使用 C++ 中的 STL 将整个字符串转换为大写或小写的程序。要执行此转换,C++ STL 提供了 toupper() 和 tolower() 函数,分别用于转换为大写和小写。示例 在线演示#include using namespace std; int main(){    string su = "Tutorials point";    transform(su.begin(), su.end(), su.begin(), ::toupper);    cout

广告

© . All rights reserved.