找到 34423 篇文章 关于编程

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

Ayush Gupta
更新于 2020年3月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年3月16日 10:04:44

89 次浏览

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

使用 C++ STL 中的集合统计反转次数

Ayush Gupta
更新于 2020年3月16日 10:02:31

178 次浏览

在本教程中,我们将讨论一个使用 C++ STL 中的集合统计反转次数的程序。反转计数是衡量数组接近完全排序程度的指标。如果数组已经排序,则反转计数将为 0。示例:`#include ` `#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 中的 STL 统计二进制数组中 1 和 0 的数量

Ayush Gupta
更新于 2020年3月16日 10:00:27

332 次浏览

在本教程中,我们将讨论一个使用 C++ STL 中的 STL 统计二进制数组中 1 和 0 的数量的程序。我们将提供一个数组,我们的任务是计算数组中 0 和 1 的数量。示例:`#include ` `#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 中的集合统计右侧较小元素的数量

Ayush Gupta
更新于 2020年3月16日 09:58:45

219 次浏览

在本教程中,我们将讨论一个使用 C++ STL 中的集合统计右侧较小元素数量的程序。我们将提供一个数组,我们的任务是构造一个新数组,并在其位置添加当前元素右侧较小元素的数量。示例:`#include ` `#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年3月16日 09:55:50

210 次浏览

在本教程中,我们将讨论一个在 C++ 中统计排序数组中较小元素数量的程序。我们将得到一个数字,我们的任务是计算排序数组中所有小于该数字的元素的数量。示例:`#include ` `#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年3月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年3月16日 09:52:05

200 次浏览

在本教程中,我们将讨论一个理解如何在 C/C++ 中将字符串转换为数字的程序。C/C++ 提供两种将字符串转换为数字的方法。示例:`#include ` `int main(){ const char *str = "12345"; int x; sscanf(str, "%d", &x); printf("The value of x : %d", x); return 0; }`输出:`The value of 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年3月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年3月16日 09:47:51

4K+ 次浏览

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

广告
© . All rights reserved.