C++基础-string截取、替换、查找子串函数
1. 截取子串
s.substr(pos, n) 截取s中从pos开始(包括0)的n个字符的子串,并返回
s.substr(pos) 截取s中从从pos开始(包括0)到末尾的所有字符的子串,并返回
2. 替换子串
s.replace(pos, n, s1) 用s1替换s中从pos开始(包括0)的n个字符的子串
4. 查找子串
s.find(s1) 查找s中第一次出现s1的位置,并返回(包括0)
s.rfind(s1) 查找s中最后次出现s1的位置,并返回(包括0)
s.find_first_of(s1) 查找在s1中任意一个字符在s中第一次出现的位置,并返回(包括0)
s.find_last_of(s1) 查找在s1中任意一个字符在s中最后一次出现的位置,并返回(包括0)
s.fin_first_not_of(s1) 查找s中第一个不属于s1中的字符的位置,并返回(包括0)
s.fin_last_not_of(s1) 查找s中最后一个不属于s1中的字符的位置,并返回(包括0)
模板函数
//1.定义一个可以求整数或者实数的绝对值的模板,在主函数中调用模板函数,输出绝对值。 (50分)
//定义一个可以求整数或者实数的绝对值的模板 30分
//在主函数中调用模板函数,输出绝对值。 20分
template <typename T>//先声明模板参数 T
typename T add(const T &num1, const T &num2)//定义模板函数,注意参数的类型
{
return num1 + num2;
}
int main()
{
cout << "Hello World!\n";
cout << add(1, 3) << endl;//模板的实例化:int add(const int &num1, const int &num2)
cout << add(3.0, 9.9) << endl; //实例化:double add(const double &num1, const double &num2)
return 0;
}
角度,弧度
2.从键盘输入一个角度值,求出该角度的正弦值、余弦值和正切值。 (50分)
提示:系统函数中提供了求正弦值、余弦值和正切值的函数:sin( )、cos( )、tan( ),函数的说明在头文件math.h中。
(1)输入的数转成角度值 20分
(2)求正弦值 10分
(3)求余弦值 10分
(4)求正切值 10分
角度 = (180° / π) * 弧度
弧度 = (π / 180°) * 角度
int main()
{
cout << "从键盘输入一个角度值:";
double PI = 3.14159;
double d_a1,d_a2;
cin >> d_a1;
cout << "求正弦值:"<<sin(d_a1*(180/PI));
cout << "求余弦值:"<<cos(d_a1*(180/PI));
cout << "求正切值:"<<tan(d_a1*(180/PI));
}
}
宏定义函数
1.用#define实现求两个数的最大值和最小值的宏,比较大小部分可以通过三目运算符?:实现,具体要求如下:
(1)定义头文件(10分)
(2)定义最大值最小值的宏(10分)
(3)在main函数中通过条件编译分别对最大值和最小值宏定义测试(20分)
//
#define da MAX //宏定义函数名
#define MIN(a,b) ((a>b)?b:a) //宏定义实现最大值最小值
double MAX(double a, double b)
{
return (a > b) ? a : b;
}
int main()
{
cout << "da:" << da(1, 2) << endl;
cout << "xiao:" << MIN(1, 2);
}
}
}
进制转化
#include "pch.h"
#include <iostream>
#include <iomanip> //不要忘了头文件
#include <math.h>
using namespace std;
//2.将输入的十六进制字符串转换为对应的整数(知识点:将16进制字符转换为十进制,再转换为整数。eg: “12AB3d”先变为整数的值:0x12AB3d ,即十进制的1223485,相当于1 * 16 ^ 5 + 2 * 16 ^ 4 + …..13 * 16 ^ 0)(字符长度可用strlen(str)函数)
//(1)定义测试字符数组(10分)
//(2)完成十六进制的单个字符转换成整数值函数书写(20分)
//(3)完成十六进制字符串转对应整数值函数(对应位加权,可通过字符指针传递参数)(20分)
//(4)完成测试并答应结果(10分)
//方法一
//int dan(char a)
//{
// if (a>='a' && a<='z')
// {
// return int(a) - 97+10;
// }
// else if(a>='0'&&a<='9')
// {
// return int(a)-48;
// }
// else if(a>='A'&&a<='Z')
// {
// return int(a)-65+10;
// }
//}
//
//int main()
//{
// char str[20]="7B";
// int sum = 0,i=0;
// if ((str[0] == 'o' || str[0] == 'O') && (str[1] == 'x' || str[1] == 'X'))
// {
// i = 2;
// }
//
//
// for (int g= strlen(str)-1; i < strlen(str); i++,g--)
// {
// int z = dan(str[i]);
// sum += ((z) * pow(16,g));//z代表的是单个字符的值,g字符在字符串中的位置,0——strlen(),从右至左
//
// }
// cout << sum;
//
//}
//方法二
/*将大写字母转换成小写字母*/
//int tolower(int c)
//{
// if (c >= 'A' && c <= 'Z')
// {
// return c + 'a' - 'A';
// }
// else
// {
// return c;
// }
//}
//将十六进制的字符串转换成整数
//int htoi(char s[])
//{
// int i;
// int n = 0;
// if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
// {
// i = 2;
// }
// else
// {
// i = 0;
// }
// for (; (s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z'); ++i)
// {
// if (tolower(s[i]) > '9')
// {
// n = 16 * n + (10 + tolower(s[i]) - 'a');
// }
// else
// {
// n = 16 * n + (tolower(s[i]) - '0');
// }
// }
// return n;
//}
//int main()
//{
// char aa[20] = "0x12AB3d ";
// //cout << htoi(aa);
// cout << tolower('a')-'a'<<endl;
//
//}
}
文件读写
#include "pch.h"
#include <iostream>
#include <iomanip> //不要忘了头文件
#include <math.h>
using namespace std;
//1.利用C++编程实现在D盘根目录下新建test1.txt文档、copytest1.txt文档,向test1.txt文档中写入20个随机数,然后通过文件读写的方式将test1.txt的内容拷贝到copytest1.txt文档。
//(1)新建test1.txt文档(15分)
//(2)新建copytest1.txt文档(15分)
//(3)将20个随机数写入test1.txt文档(20分)
//(4)将test1.txt的内容拷贝到copytest1中(20分)
#include <fstream>
#include <cstdlib>
//字符串读写
//int main()
//{
// 随机数
// //for (size_t i = 0; i < 5; i++)
// //{
// // cout << rand()%5 << endl;
// //}
//
//
// /*模式标志 描述
// ios::app 追加模式。所有写入都追加到文件末尾。
// ios::ate 文件打开后定位到文件末尾。
// ios::in 打开文件用于读取。
// ios::out 打开文件用于写入。
// ios::trunc 如果该文件已经存在,其内容将在打开文件之前被截断,即把文件长度设为 0
// */
// //字符串读写
// ofstream outfile;//存入文件
// char data[500];
// outfile.open("b.txt", ios::app);
//
// outfile << "你好吗?" << endl;
// outfile << "I am fine." << endl;
// outfile << "but,I am not OK" << endl;
// outfile << "What is up?" << endl << endl;
//
// outfile.close();
//
// ifstream infile;
// infile.open("a.txt");
// while (true)
// {
// infile >> data;
// cout << data;
// if (data == "")
// {
// break;
// }
//
// }
//
// infile.close();
//
//
//}
二进制读写
#include <stdlib.h>
//#include <cstdlib>
//#include <fstream>
//
//int main()
//{
// //cout << size(list1) << endl;
// /*int list1[5];
// int list2[5];
// for (size_t i = 0; i < 5; i++)
// {
// list1[i] = rand() % 10;
// }
// ofstream outfile;
// outfile.open("binary.dat", ios::binary);
// outfile.write((char *)list1,sizeof(list1));
// outfile.close();
//
//
//
// ifstream infile;
// infile.open("binary.dat", ios::binary);
// infile.read((char *)list2, sizeof(list2));
// infile.close();*/
//
//
// char str1[20] = "你长得好丑!";
// char str2[20];
//
// ofstream ouflile;
// ouflile.open("binary.dat", ios::binary);
// ouflile.write((char *)str1, sizeof(str1));
// ouflile.close();
//
// ifstream infile;
// infile.open("binary.dat", ios::binary);
// infile.read((char *)str2, sizeof(str2));
// infile.close();
//
//
// //cout << strlen(str1) << endl;
// for (size_t i = 0; i < strlen(str1); i++)
// {
// cout << str2 << endl;
// }
//
// return 0;
//}
}
list用法
#include "pch.h"
#include <iostream>
#include <iomanip> //不要忘了头文件
#include <math.h>
using namespace std;
//Ref: c++中的list用法
//
// assign() 给list赋值
// back() 返回最后一个元素
// begin() 返回指向第一个元素的迭代器
// clear() 删除所有元素
// empty() 如果list是空的则返回true
// end() 返回末尾的迭代器
// erase() 删除一个元素
// front() 返回第一个元素
// get_allocator() 返回list的配置器
// insert() 插入一个元素到list中
// max_size() 返回list能容纳的最大元素数量
// merge() 合并两个list
// pop_back() 删除最后一个元素
// pop_front() 删除第一个元素
// push_back() 在list的末尾添加一个元素
// push_front() 在list的头部添加一个元素
// rbegin() 返回指向第一个元素的逆向迭代器
// remove() 从list删除元素
// remove_if() 按指定条件删除元素
// rend() 指向list末尾的逆向迭代器
// resize() 改变list的大小
// reverse() 把list的元素倒转
// size() 返回list中的元素个数
// sort() 给list排序
// splice() 合并两个list
// swap() 交换两个list
// unique() 删除list中重复的元素
#include <string>
#include <list>
int main()
{
list<string> Milkshakes;
Milkshakes.push_back("123");
Milkshakes.push_front("456");
cout << *Milkshakes.begin();
return 0;
}
}
getchar()
#include "pch.h"
#include<stdio.h>
#include<iostream>
#include<string>
using namespace std;
//
int main()
{
/*char test2 = getchar(); //从缓冲区读取单个字符,使用完毕后要清理缓冲区,不然下次读到的就是'\n'
rewind(stdin);* ///所以这个函数可以计算有多少个字母,有多少个空格。。。。。
int i, num = 0, word = 0;
char c;
//cin >> str;
//cout << str.length() << "\n";
while ((c = getchar()) != '\n')
{
if (c == ' ')
num++;
}
printf("there are %d words in this line.\n", num);
return 0;
}
}
strstr()
#include "pch.h"
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
// 从str1中查找str2的个数,并返回
int findChildCnt(char* str1, char* str2)
{
int len = strlen(str2);
cout << "str2长度" << len << endl;
/*str1 = strstr(str1, str2);
cout << "str1:" << str1 << endl;
return 0;*/
int cnt = 0;
while (str1 = strstr(str1, str2)) // 如果查找到,则执行循环,否则为空退出循环
{
cnt++; // 统计次数
str1 += len; // 加上偏移量,即移除str2
}
return cnt;
}
int main()
{
char str1[100], str2[100];
printf("intput str1 :");
cin >> str1;
printf("intput str2 :");
cin >> str2;
printf("Child Cnt: %d\n", findChildCnt(str1, str2));
return 0;
}
构造函数
#include <iostream>
using namespace std;
class Line
{
public:
void setLength( double len );
double getLength( void );
Line(double len); // 这是构造函数
private:
double length;
};
// 成员函数定义,包括构造函数
Line::Line( double len)
{
cout << "Object is being created, length = " << len << endl;
length = len;
}
void Line::setLength( double len )
{
length = len;
}
double Line::getLength( void )
{
return length;
}
// 程序的主函数
int main( )
{
Line line(10.0);
// 获取默认设置的长度
cout << "Length of line : " << line.getLength() <<endl;
// 再次设置长度
line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<endl;
return 0;
}
当上面的代码被编译和执行时,它会产生下列结果:
Object is being created, length = 10
Length of line : 10
Length of line : 6
使用初始化列表来初始化字段:
Line::Line( double len): length(len)
{
cout << "Object is being created, length = " << len << endl;
}
上面的语法等同于如下语法:
Line::Line( double len)
{
length = len;
cout << "Object is being created, length = " << len << endl;
}
析构函数
#include <iostream>
using namespace std;
class Line
{
public:
void setLength( double len );
double getLength( void );
Line(); // 这是构造函数声明
~Line(); // 这是析构函数声明
private:
double length;
};
// 成员函数定义,包括构造函数
Line::Line(void)
{
cout << "Object is being created" << endl;
}
Line::~Line(void)
{
cout << "Object is being deleted" << endl;
}
void Line::setLength( double len )
{
length = len;
}
double Line::getLength( void )
{
return length;
}
// 程序的主函数
int main( )
{
Line line;
// 设置长度
line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<endl;
return 0;
}
当上面的代码被编译和执行时,它会产生下列结果:
Object is being created
Length of line : 6
Object is being deleted
友元函数
#include <iostream>
using namespace std;
class Box
{
double width;
public:
friend void printWidth( Box box );
void setWidth( double wid );
};
// 成员函数定义
void Box::setWidth( double wid )
{
width = wid;
}
// 请注意:printWidth() 不是任何类的成员函数
void printWidth( Box box )
{
/* 因为 printWidth() 是 Box 的友元,它可以直接访问该类的任何成员 */
cout << "Width of box : " << box.width <<endl;
}
// 程序的主函数
int main( )
{
Box box;
// 使用成员函数设置宽度
box.setWidth(10.0);
// 使用友元函数输出宽度
printWidth( box );
return 0;
}
当上面的代码被编译和执行时,它会产生下列结果:
Width of box : 10
拷贝构造函数
#include <iostream>
using namespace std;
class Line
{
public:
int getLength( void );
Line( int len ); // 简单的构造函数
Line( const Line &obj); // 拷贝构造函数
~Line(); // 析构函数
private:
int *ptr;
};
// 成员函数定义,包括构造函数
Line::Line(int len)
{
cout << "调用构造函数" << endl;
// 为指针分配内存
ptr = new int;
*ptr = len;
}
Line::Line(const Line &obj)
{
cout << "调用拷贝构造函数并为指针 ptr 分配内存" << endl;
ptr = new int;
*ptr = *obj.ptr; // 拷贝值
}
Line::~Line(void)
{
cout << "释放内存" << endl;
delete ptr;
}
int Line::getLength( void )
{
return *ptr;
}
void display(Line obj)
{
cout << "line 大小 : " << obj.getLength() <<endl;
}
// 程序的主函数
int main( )
{
Line line1(10);
Line line2 = line1; // 这里也调用了拷贝构造函数
display(line1);
display(line2);
return 0;
}
当上面的代码被编译和执行时,它会产生下列结果:
调用构造函数
调用拷贝构造函数并为指针 ptr 分配内存
调用拷贝构造函数并为指针 ptr 分配内存
line 大小 : 10
释放内存
调用拷贝构造函数并为指针 ptr 分配内存
line 大小 : 10
释放内存
释放内存
释放内存
this指针
#include <iostream>
using namespace std;
class Box
{
public:
// 构造函数定义
Box(double l=2.0, double b=2.0, double h=2.0)
{
cout <<"Constructor called." << endl;
length = l;
breadth = b;
height = h;
}
double Volume()
{
return length * breadth * height;
}
int compare(Box box)
{
return this->Volume() > box.Volume();
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
int main(void)
{
Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2
if(Box1.compare(Box2))
{
cout << "Box2 is smaller than Box1" <<endl;
}
else
{
cout << "Box2 is equal to or larger than Box1" <<endl;
}
return 0;
}
当上面的代码被编译和执行时,它会产生下列结果:
Constructor called.
Constructor called.
Box2 is equal to or larger than Box1
指向类的指针
#include <iostream>
using namespace std;
class Box
{
public:
// 构造函数定义
Box(double l=2.0, double b=2.0, double h=2.0)
{
cout <<"Constructor called." << endl;
length = l;
breadth = b;
height = h;
}
double Volume()
{
return length * breadth * height;
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
int main(void)
{
Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2
Box *ptrBox; // Declare pointer to a class.
// 保存第一个对象的地址
ptrBox = &Box1;
// 现在尝试使用成员访问运算符来访问成员
cout << "Volume of Box1: " << ptrBox->Volume() << endl;
// 保存第二个对象的地址
ptrBox = &Box2;
// 现在尝试使用成员访问运算符来访问成员
cout << "Volume of Box2: " << ptrBox->Volume() << endl;
return 0;
}
当上面的代码被编译和执行时,它会产生下列结果:
Constructor called.
Constructor called.
Volume of Box1: 5.94
Volume of Box2: 102
模板函数
}
模板函数
}
模板函数
}
模板函数
}
模板函数
}
模板函数
}
模板函数
}