找到 34423 篇文章 关于编程

C++ 中的 strstr()

George John
更新于 2020-06-25 09:04:00

1K+ 次浏览

strstr() 函数是 string.h 中的预定义函数。它用于查找字符串中子字符串的出现位置。此匹配过程在 ‘\0’ 处停止,并不包含它。strstr() 的语法如下:char *strstr( const char *str1, const char *str2)在上述语法中,strstr() 查找字符串 str2 在字符串 str1 中的第一次出现位置。一个实现 strstr() 的程序如下:示例 在线演示#include <string.h>#include <iostream>using namespace std; int main() {    char str1[] = "Apples are red";    char str2[] = "are";    char *ptr;    ptr = strstr(str1, str2);    if(ptr)    cout<<ptr<<endl;    return 0; }

C++ 中的子字符串

Ankith Reddy
更新于 2023-10-07 01:44:35

24K+ 次浏览

子字符串是字符串的一部分。在 C++ 中获取子字符串的函数是 substr()。此函数包含两个参数:pos 和 len。pos 参数指定子字符串的起始位置,len 表示子字符串中的字符数。获取 C++ 中子字符串的程序如下:示例 在线演示#include <iostream>#include <string>using namespace std; int main() {    string str1 = "Apples are red";    string str2 = str1.substr(11, 3);    string str3 = str1.substr(0, 6);    cout<<str2<<endl;    cout<<str3<<endl;    return 0; }

使用位运算符执行加法运算的 C++ 程序

Arjun Thakur
更新于 2020-06-25 09:05:32

2K+ 次浏览

位运算符用于执行位运算。这意味着对位进行操作。一些位运算符包括按位与、按位或、按位异或等。使用位运算符执行加法运算的程序如下:示例 在线演示#include <iostream>using namespace std; int main() {    int num1, num2, carry;    cout<<"输入两个数字:";    cin>>num1>>num2; ...

使用 rand 和 srand 函数的 C++ 程序

Chandu yadav
更新于 2020-06-25 09:06:34

481 次浏览

可以使用 rand() 函数在 C++ 中生成随机数。srand() 函数为 rand() 使用的随机数生成器播种。使用 rand() 和 srand() 的程序如下:示例 在线演示#include <iostream>#include <stdlib.h>#include <time.h>using namespace std; int main() {    srand(1);    for(int i=0; i<10; i++)

按字典序 (词典顺序) 排序元素的 C++ 程序

Arjun Thakur
更新于 2020-06-25 09:08:19

1K+ 次浏览

字典序表示单词在列表中按字母顺序排列的方式。例如:单词列表:Harry Adam Sam单词的字典序:Adam Harry Sam按字典序排序元素的程序如下:示例 在线演示#include <iostream>using namespace std; int main() {    int i,j;    string s[5], temp;    cout<<"输入 5 个字符串:\n";

通过将结构传递给函数来添加复数的 C++ 程序

Chandu yadav
更新于 2020-06-25 09:09:47

667 次浏览

复数是可以表示为 a+bi 的数,其中 i 是虚数,a 和 b 是实数。复数的一些示例:2+5i 3-9i 8+2i通过将结构传递给函数来添加复数的程序如下:示例 在线演示#include <iostream>using namespace std; typedef struct complexNumber {    float real;    float imag; }; complexNumber addCN(complexNumber num1, complexNumber num2) {    complexNumber temp;    temp.real = num1.real + num2.real;    temp.imag = num1.imag + num2.imag;    return(temp); } int main() {    complexNumber num1, num2, sum;    cout<<"输入第一个复数的实部和虚部:";    cin>>num1.real; ... 阅读更多

使用结构体添加两个距离(英寸-英尺)系统的 C++ 程序

George John
更新于 2020-06-25 09:10:59

652 次浏览

结构体是不同数据类型项目的集合。它在创建具有不同数据类型记录的复杂数据结构时非常有用。结构体使用 struct 关键字定义。结构体示例如下:struct DistanceFI {    int feet;    int inch; };上述结构体以英尺和英寸的形式定义距离。使用 C++ 中的结构体添加两个英寸-英尺距离的程序如下:示例 在线演示#include <iostream>using namespace std; struct DistanceFI {    int feet;    int inch; }; int main() {    struct DistanceFI distance1, distance2, distance3;    cout<<"输入第一个距离的英尺和英寸:";

实现向量的 C++ 程序

Ankith Reddy
更新于 2020-06-25 09:14:02

869 次浏览

向量是一个动态数组,如果插入或删除元素,它可以调整自身大小。向量元素包含在连续存储区中,容器会自动处理存储。实现向量的程序如下:示例#include <iostream>#include <vector>#include <algorithm>#include <numeric>using namespace std; int main() {    int ch, val;    vector<int> vec;    cout<<"菜单:\n";

实现变长数组的 C++ 程序

Arjun Thakur
更新于 2020-06-25 08:33:13

6K+ 次浏览

变长数组可以具有用户所需的长度,即它们可以具有可变长度。在 C++ 中实现变长数组的程序如下:示例 在线演示#include <iostream>#include <cstdlib>using namespace std; int main() {    int *array, size;    cout<<"输入数组的大小:";

实现并行数组的 C++ 程序

Chandu yadav
更新于 2020-06-25 08:34:31

4K+ 次浏览

并行数组是一个包含多个数组的结构。这些数组的大小相同,并且数组元素彼此相关。并行数组中的所有元素都代表一个公共实体。并行数组示例如下:employee_name = { Harry, Sally, Mark, Frank, Judy } employee_salary = {10000, 5000, 20000, 12000, 5000}在上述示例中,5 个不同员工的姓名和工资存储在 2 个数组中。演示并行数组的程序如下:示例#include <iostream>#include <string>using namespace std; int main() {    int ... 阅读更多

广告
© . All rights reserved.