【C/C++学院】0815-函数包装器/CPP类型转换/函数模块/动态数组

简介: <p></p> <h2><span style="font-family:宋体; font-size:16pt">函数包装器管理内嵌函数</span><span style="font-family:宋体; font-size:16pt"></span></h2> <pre name="code" class="cpp">#include<iostream>#incl

函数包装器管理内嵌函数

#include
#include

//函数包装器
//第一,设计执行接口,接口设计关卡(),计数
//第二,函数包装器依赖于函数模板,实现通用泛型
//第三,函数代码可以内嵌在另外一个函数,实现函数**
//函数包装器,用于管理内嵌函数,外部函数调用


//函数包装器, T数据类型, F是函数
template
T run(T v, F f)
{
	static int count = 0;
	count++;//计数器
	std::cout << "run  一个参数的包装器 执行" << count << "次" << std::endl;
	if (count > 1)
	{
		T  vx(0);
		return  vx;
	}
	return f(v);//函数传入参数
}

template
T run(T v1, T v2, F f) 
{
	return f(v1,v2);//函数传入参数
}


void main()
{
	using  std::cout;  //C++11. namespace,专门引用一个关键字,不需要std
	using  std::endl;
	using  std::function;
	//using namespace std;
	using  std::cin;

	double db = 12.9;//double *2
	int  num1 = 19;
	int  num2 = 29;

	function fun1 = [](double u)
	{
		return u * 2; 
	};
	function fun2 = [](double u)
	{
		return u*u;
	};
	function fun3 = [](int u1,int u2)
	{
		return u1 + u2;
	};
	
	cout << run(db, fun1) << endl;//调用
	cout << run(db, fun2) << endl;//调用
	cout << run(num1,num2, fun3) << endl;//调用
	cin.get();//等价于你输入一字符getchar;

}

函数包装器管理外部函数

#include
#include

template
T run(T v1, T v2, F f)
{
	return f(v1, v2);//函数传入参数
}

int  cheng(int a, int b)
{
	return a*b;
}

void main()
{
	using  std::cout;  //C++11. namespace,专门引用一个关键字,不需要std
	using  std::endl;
	using  std::function;
	//using namespace std;
	using  std::cin;

	int  num1 = 19;
	int  num2 = 29;
	function fun4 = cheng; //fun4函数指针

	cout << run(num1, num2, fun4) << endl;//调用
	cin.get();//等价于你输入一字符getchar;

}

函数模板根据类型覆盖

函数模板覆盖

#include

//函数模板实现通用,可以根据自有数据类型,进行优化

//结构体没有私有变量可以直接赋值初始化
//所有成员都是公有的类型可以赋值初始化
struct info
{
	char name[40];
	double db;
	int data;	
};

template
void swap(T &a, T &b)
{
	std::cout << "通用函数模板" << std::endl;
	T temp = a;
	a = b;
	b = temp;//交换两个变量
}
//模板为空,明确参数类型,覆盖函数模板的类型,
void  swap(info &info1, info &info2)
{
	std::cout << "特有函数模板" << std::endl;
	//通用模板可以实现通用,针对自己的数据类型做出优化
	info temp = info1;
	info1 = info2;
	info2 = temp;
}


void main()
{
	info info1 = { "yincheng", 20.9, 10 };
	info info2 = { "chengyin",9.2, 1 };
	swap(info1, info2);
	std::cout << info1.name << info1.db << info1.data << std::endl;
	std::cout << info2.name << info2.db << info2.data << std::endl;
	std::cin.get();
}


void main1()
{
	int num1 = 100;
	int num2 = 10;
	swap(num1, num2);//实现交换
	std::cout << num1 << "  " << num2 << std::endl;
	char  ch1 = 'Z';
	char  ch2 = 'A';
	swap(ch1, ch2);
	std::cout << ch1 << "  " << ch2 << std::endl;

	std::cin.get();//getchar
}

处理类的私有

#define _CRT_SECURE_NO_WARNINGS
#include
#include

//函数模板实现通用,可以根据自有数据类型,进行优化

//结构体可以直接赋值(没有私有变量)

//所有成员都是公有的类型可以赋值(一开始初始化)
//如果类有私有成员变量,不可以用{}初始化

//类的对象之间默认是可以直接赋值

//类,结构体都有一个默认赋值操作= 浅拷贝 ,交换数据

//深拷贝用的最多,函数模板的覆盖
class info
{
public:
	char name[40];
	char *p;
	int data;
private:
	int num;
public:
	void set(int X)
	{
		this->num = X;//通过接口设置私有变量
	}
	int get()
	{
		return this->num;//返回值,副本机制
	}
};

template
void swap(T &a, T &b)
{
	std::cout << "通用函数模板" << std::endl;
	T temp = a;
	a = b;
	b = temp;//交换两个变量
}


//模板为空,明确类型,
template<>
void  swap(info &info1, info &info2)
{
	std::cout << "特有函数模板" << std::endl;
	//通用模板可以实现通用,针对自己的数据类型做出优化
	//计数器,对象交换计数器
	info temp = info1;
	info1 = info2;
	info2 = temp;//
}

void main()
{
	info info1;
	info info2;
	std::strcpy(info1.name, "yincheng");
	std::strcpy(info2.name, "chengyin ");
	info1.data = 102;
	info2.data = 201;//初始化
	info1.p = new char[10];
	std::strcpy(info1.p, "**");

	//info2.p = nullptr;//C++的空指针
	info2.p = new char[100];
	std::strcpy(info2.p, "da**");
	info1.set(89);
	info2.set(98);

	swap(info1, info2);

	std::cout << info1.name << "   "<< info1.data  <<"   "<CPP类型转换 四种cast

#include
#include

void main1()
{
	double db = 10.9;
	float fl= db;//默认数据类型转换
	std::cin.get();
}

void main2()
{
	void *p = new int[10];
	int *pint =(int*) p;//C语言风格
}

//static_cast<需要转换的数据类型>(要转换的数据)80% static_cast
void main3()
{
	//int n = static_cast(78.98);
	printf("\n%d", 98.87);
	printf("\n%d",static_cast( 98.87));
	printf("\n%f", 98);
	printf("\n%f", static_cast(98));

	int *p = static_cast (malloc(100));
	std::cin.get();
}
//const int num = 10,可以修改无法生效,编译的时候不读内存
//const int *p指向变量限定权限,只读不可写,
//const_cast去掉常量指针属性 %5
void main4()
{
	int num[3] = { 1, 2, 3 };
	const int *p = num;
	std::cout << *p << *(p + 1) << *(p + 2) << std::endl;
	//*p = 10;
	//*(p + 1) = 20;
	int *pnew = const_cast(p);
	*pnew = 10;

	std::cin.get();
}
//reinterpret_cast %1  专业转换指针,最安全
void  main()
{
	//指针。强类型,类型决定了数据的解析方式,内存占多大
	int num = 3;
	char *p = reinterpret_cast(&num);
	for (int i = 0; i < 4; i++)
	{
		printf("%c,%d,%p\n", *(p+i), *(p+i), p+i);
	}
	std::cin.get();
}
//dynamic_cast 类的指针之间的转换

函数模板重载调用规则

函数模板与普通函数的选择问题

#include 

//函数模板可以对类型进行优化重载,根据类型会覆盖
//如果仍然要使用模板函数,需要实例化
template
T add(T a,T b)
{
	std::cout << " T add" << std::endl;
	return a + b;
}

int add(int a, int b)
{
	std::cout << " int add" << std::endl;
	return a + b;
}

void main()
{
	int a = 10, b = 20;
	double db1 = 10.9, db2 = 10.8;
	add(db1, db2);
	add(a,b);
	add(a, b);//进行实例化,

	std::cin.get();
}

函数可变参数通用类型模板函数 

可变参数高级模板

#include
#include

void showall(){};//预留一个

template 
void show(T t, ...)
{
	std::cout << t << std::endl;
}

template 
void showall(T t, Args...args)
{
	std::cout << t << std::endl;
	showall( args...);
}

void main()
{
	int num1 = 10, num2 = 9, num3 = 11;
	double db1 = 10.8, db2 = 10.9;
	char str[40] = "yincheng";
	char ch = 'A';
	show(num1);
	showall(num2,num3);
	showall(num2, num3,num1,str,ch);

	std::cin.get();
}

通用函数可变参数模板

#include 


//通用可变参数模板    处理不限定个数的参数,处理不同类型
void showall()//空函数,接口,最后结束递归  新版本编译
{

}

template
void showall(const T &value, const Args &...args)
{
	std::cout << value << std::endl;
	showall(args...);//继续传递
}

//设计可以修改原来的数据的  T &value,  Args &...args
//设计可以修改副本    T value,  Args ...args
//设计不可以可以改原来的数据不可以修改副本 const T value,  const Args ...args
//设计引用原来的数据不可以修改 const  T &value,  const Args &...args

void main()
{
	int num1 = 10, num2 = 9, num3 = 11;
	double db1 = 10.8, db2 = 10.9;
	char str[40] = "yincheng";
	char ch = 'A';
	showall(num1);
	std::cout << "\n\n\n";
	showall(num1,num2,num3);
	std::cout << "\n\n\n";
	showall(db1, db2, num1, ch);
	std::cout << "\n\n\n";
	showall(db1, db2, num1, ch,str);

	std::cin.get();
}

cpp新数组

#include 
#include
#include
#include

void main1()
{
	double db[4] = { 1.1, 2.2, 3.3, 4.4 };
	//std::array数据类型,double元素类型,4个数
	std::array  dbnew1 = { 10.1, 10.2, 10.3, 10.4 };
	std::array  dbnew2 = dbnew1;//可以实现数组之间整体操作
	for (int i = 0; i < 4; i++)
	{
		std::cout << db[i] << "   " << dbnew1[i]<<"    "< string1 = { "calc", "notepad", "tasklist", "mspaint", "write" };

	for (int i = 0; i < 5; i++)
	{
		std::cout << string1[i] << std::endl;
		system(string1[i].c_str());
	}
	std::cin.get();
}

void main()
{
	std::string str1 = "task";
	std::string str2 = "list";
	std::string str3 = str1 + str2;
	system(str3.c_str());
	std::cin.get();
}

高级数组array_vector 

#include
#include
#include//C++的标准库
#include//C++字符串

using  std::array;//静态数组,栈上,
using std::vector;//动态数组,堆上,
using std::string;

//使用C++风格数组不需要管理内存。
//array注意不要栈溢出
//array适用于任何类型

void main1()
{
	//
	array myint = { 1, 2, 3, 4, 5 };
	array myint1;
	vector myvector; //动态数组
	for (int i = 0; i < 1024 * 1024; i++)
	{
		myvector.push_back(i);//
	}

	std::cin.get();
}

void main2()
{
	array myint1 = { 1, 2, 3, 4, 5 };
	array myint2 = { 11, 12, 13, 14, 15 };
	array myint3 = { 21, 22, 23, 24, 25 };
//	array, 3> myint = {myint1,myint2,myint3};
	array, 3> myint = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
	for (int i = 0; i < myint.size();i++)//数组大小
	{
		for (int j = 0; j < myint1.size(); j++)
		{
			std::cout << "  "<  string1;//动态字符串数组
	//可以反复利用
	string1.push_back("notepad");
	string1.push_back("calc");
	string1.push_back("mspaint");
	string1.pop_back();//删除一个
	//string1.clear();//清空
	for (int i = 0; i < string1.size(); i++)//遍历动态数组
	{
		//system(string1[i].c_str());
	}
}

void main5()
{
	vector   string1;//动态字符串数组
	string1.push_back("notepad");
	string1.push_back("calc");
	string1.push_back("mspaint");

	vector::iterator ibegin, iend;//迭代器
	ibegin = string1.begin();//数据起始点
	iend = string1.end();//结束

	for (;ibegin!=iend;ibegin++)
	{
		string tempstr = *ibegin;//获取指针指向的数据
		system(tempstr.c_str());//执行指令
	}
}

void  main6()
{
	array myint = { 1, 2, 3, 4, 5 };
	array::iterator ibegin, iend;//正向迭代器
	ibegin = myint.begin();
	iend = myint.end();
	while (ibegin!=iend)
	{
		std::cout << *ibegin << std::endl;
		ibegin++;
	}
	array::reverse_iterator rbegin, rend;
	rbegin = myint.rbegin();
	rend = myint.rend();
	while (rbegin!=rend)
	{
		std::cout << *rbegin << std::endl;
		rbegin++;

	}

	std::cin.get();
}

void main7()
{
	vector   string1;//动态字符串数组
	string1.push_back("notepad");
	string1.push_back("calc");
	string1.push_back("mspaint");
	//反向迭代器
	vector::reverse_iterator rbegin = string1.rbegin();
	vector::reverse_iterator rend = string1.rend();
	//rend--;rend最后不指向数据,指向数据的结尾的下一个节点
A:	if (rbegin!=rend)
	{
		system((*rend).c_str());//执行指令
		//rbegin++;
		rend--;
		goto A;
	}
}

Lambda  [ret](int x){xxx;}

高级表达式以及增删查改

#include
#include
#include//算法  	lambda表达式,不仅仅适用与array ,也适用于vector

void main1()
{
	std::vector myvector;
	myvector.push_back(11);
	myvector.push_back(22);
	myvector.push_back(33);
	myvector.push_back(3);
	myvector.push_back(4);
	myvector.push_back(5);
	int res=0;//结果
	//&res直接操作一个变量,res等价于返回值,x代表参数,每次充当迭代器指向的元素,大括号就是代码
	std::for_each(myvector.begin(), myvector.end(), [&res](int x){res += x; });
	std::cout << res;
	std::cin.get();
}

void main()
{
	std::vector myvector(5);//分配5个空间,默认初始化为0

	myvector.push_back(1);//增
	myvector.push_back(11);
	myvector.push_back(111);
	myvector.push_back(1111);
	myvector.push_back(2);
	myvector.pop_back();//弹出一个元素,删除最后一个
	myvector.insert(myvector.begin() +1, 999);//插入,
	myvector.erase(myvector.begin()+5);//根据迭代器的位置
	//myvector.clear();//删除所有元素
	for (int i = 0; i < myvector.size(); i++)
	{
		if (1)
		{
			//查询,修改
		}
		std::cout << myvector.at(i) << std::endl;
	}

	system("pause");
}

void main123123()
{
	//可以实现动态无规则数组管理
	std::vector myvetor1;
	myvetor1.push_back(12);
	myvetor1.push_back(13);
	myvetor1.push_back(14);

	std::vector myvetor2;
	myvetor2.push_back(22);

	std::vector myvetor3;
	myvetor3.push_back(32);
	myvetor3.push_back(37);

	std::vector> allvecor;
	allvecor.push_back(myvetor1);
	allvecor.push_back(myvetor2);
	allvecor.push_back(myvetor3);
	for (int i = 0; i < allvecor.size(); i++)
	{
		for (int j = 0; j < allvecor[i].size(); j++)
		{
			std::cout <<"  "<< allvecor[i][j];
		}
		std::cout << "\n";
	}
	
	std::cin.get();
}

动态不规则数组以及增删查改 

#include
#include
#include//C++的标准库
#include//C++字符串

using  std::array;//静态数组,栈上,
using std::vector;//动态数组,堆上,
using std::string;

//使用C++风格数组不需要管理内存。
//array注意不要栈溢出
//array适用于任何类型

void main1()
{	
	array myint = { 1, 2, 3, 4, 5 };
	array myint1;
	vector myvector; //动态数组
	for (int i = 0; i < 1024 * 1024; i++)
	{
		myvector.push_back(i);//
	}

	std::cin.get();
}

void main2()
{
	array myint1 = { 1, 2, 3, 4, 5 };
	array myint2 = { 11, 12, 13, 14, 15 };
	array myint3 = { 21, 22, 23, 24, 25 };
//	array, 3> myint = {myint1,myint2,myint3};
	array, 3> myint = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
	for (int i = 0; i < myint.size();i++)//数组大小
	{
		for (int j = 0; j < myint1.size(); j++)
		{
			std::cout << "  "<  string1;//动态字符串数组
	//可以反复利用
	string1.push_back("notepad");
	string1.push_back("calc");
	string1.push_back("mspaint");
	string1.pop_back();//删除一个
	//string1.clear();//清空
	for (int i = 0; i < string1.size(); i++)//遍历动态数组
	{
		//system(string1[i].c_str());
	}
}

void main5()
{
	vector   string1;//动态字符串数组
	string1.push_back("notepad");
	string1.push_back("calc");
	string1.push_back("mspaint");

	vector::iterator ibegin, iend;//迭代器
	ibegin = string1.begin();//数据起始点
	iend = string1.end();//结束

	for (;ibegin!=iend;ibegin++)
	{
		string tempstr = *ibegin;//获取指针指向的数据
		system(tempstr.c_str());//执行指令
	}
}

void  main6()
{
	array myint = { 1, 2, 3, 4, 5 };
	array::iterator ibegin, iend;//正向迭代器
	ibegin = myint.begin();
	iend = myint.end();
	while (ibegin!=iend)
	{
		std::cout << *ibegin << std::endl;
		ibegin++;
	}
	array::reverse_iterator rbegin, rend;
	rbegin = myint.rbegin();
	rend = myint.rend();
	while (rbegin!=rend)
	{
		std::cout << *rbegin << std::endl;
		rbegin++;

	}

	std::cin.get();
}

void main7()
{
	vector   string1;//动态字符串数组
	string1.push_back("notepad");
	string1.push_back("calc");
	string1.push_back("mspaint");
	//反向迭代器
	vector::reverse_iterator rbegin = string1.rbegin();
	vector::reverse_iterator rend = string1.rend();
	//rend--;rend最后不指向数据,指向数据的结尾的下一个节点
A:	if (rbegin!=rend)
	{
		system((*rend).c_str());//执行指令
		//rbegin++;
		rend--;
		goto A;
	}
}

动态数组任意位置插入 

#include
#include
#include//算法  	lambda表达式,不仅仅适用与array ,也适用于vector

void main1()
{
	std::vector myvector;
	myvector.push_back(11);
	myvector.push_back(22);
	myvector.push_back(33);
	myvector.push_back(3);
	myvector.push_back(4);
	myvector.push_back(5);
	int res=0;//结果
	//&res直接操作一个变量,res等价于返回值,x代表参数,每次充当迭代器指向的元素,大括号就是代码
	std::for_each(myvector.begin(), myvector.end(), [&res](int x){res += x; });
	std::cout << res;
	std::cin.get();
}

void main()
{
	std::vector myvector(5);//分配5个空间,默认初始化为0

	myvector.push_back(1);//增
	myvector.push_back(11);
	myvector.push_back(111);
	myvector.push_back(1111);
	myvector.push_back(2);
	myvector.pop_back();//弹出一个元素,删除最后一个
	myvector.insert(myvector.begin() +1, 999);//插入,
	myvector.erase(myvector.begin()+5);//根据迭代器的位置
	//myvector.clear();//删除所有元素
	for (int i = 0; i < myvector.size(); i++)
	{
		if (1)
		{
			//查询,修改
		}
		std::cout << myvector.at(i) << std::endl;
	}

	system("pause");
}

多元数组 tuple

#include
#include
//多元数组
//tuple必须是一个静态数组,
//配合vector, array

void main(void)//void在参数内部意味着参数为空,不写也意味着为空
{
	int int1 = 10;
	double double1 = 99.8;
	char ch = 'A';
	char *str = "hellochina";
	std::tuple mytuple(int1, double1, ch, str);
	const int num = 3;
	auto data0 = std::get<0>(mytuple);
	auto data1 = std::get<1>(mytuple);
	auto data2 = std::get<2>(mytuple);
	auto data3 = std::get(mytuple);//下标只能是常量
	std::cout <限定区域分配内存的语法
#include
#include

const int buf(512);//限定一个常量整数512
int N(5);//数组的长度
char buffer[buf] = {0};//静态区

//p1,p3,p5作为指针变量在栈区,存储的地址指向堆区
//手动释放内存

//p2,p4,p6作为指针变量在栈区,存储的地址在静态区。缓冲区。
//自动释放内存,用于分配用完了就不会再用的数据
//避免内存泄漏,自动释放内存。牺牲了内存访问独立性,

using namespace std;
void main()
{
	double *p1, *p2;
	std::cout << "\n\n\n";
	p1 = new double[N];//分配内存,N个元素的大小
	p2 = new (buffer)double[N];//指定区域分配内存
	for (int i = 0; i < N; i++)
	{
			p1[i] = p2[i] = i + 10.8;//对于数组初始化
			std::cout << "p1===   " << &p1[i] << "  " << p1[i];
			std::cout << "   p2===   " << &p2[i] << "  " << p2[i] << std::endl;
	}

	double *p3, *p4;
	std::cout << "\n\n\n";
	p3 = new double[N];//分配内存,N个元素的大小
	p4 = new (buffer)double[N];//指定区域分配内存	
	for (int i = 0; i < N; i++)
	{
		p3[i] = p4[i] = i + 10.8 ;//对于数组初始化
		std::cout << "p3===   " << &p3[i] << "  " << p3[i];
		std::cout << "   p4===   " << &p4[i] << "  " << p4[i] << std::endl;
	}

	double *p5, *p6;
	std::cout << "\n\n\n";
	p5 = new double[N];//分配内存,N个元素的大小
	p6 = new (buffer)double[N];//指定区域分配内存
	for (int i = 0; i < N; i++)
	{
		p6[i] = p5[i] = i + 10.8;//对于数组初始化
		std::cout << "p5===   " << &p5[i] << "  " << p5[i];
		std::cout << "   p6===   " << &p6[i] << "  " << p6[i] << std::endl;
	}

	std::cin.get();
}

函数模板重载

#include
#include
using  std::array;

template
void showarray(array myarray,int n)
{
	using namespace std;
	cout << "TTTTT" << endl;
	for (int i = 0; i < n;i++)
	{
		cout << myarray[i] <<" ";
	}
	cout << endl;
}

template
void showarray(array  myarray, int n)
{
	using namespace std;
	cout << "T*T*T*T*T*" << endl;
	for (int i = 0; i < n; i++)
	{
		cout << *myarray[i] << " ";
	}
	cout << endl;
}

void main()
{
	array intarray = { 1, 2, 3, 4, 5,6,7,8,9,10 };
	array pintarray ;
	for (int i = 0; i < 10; i++)
	{
		pintarray[i] = &intarray[i];
	}
	array ppintarray;
	for (int i = 0; i < 10; i++)
	{
		ppintarray[i] = &pintarray[i];
	}
	showarray(intarray, 10);
	showarray(pintarray, 10);
	showarray(ppintarray, 10);

	std::cin.get();
}













目录
相关文章
|
4天前
|
存储 C++
【C++】string类的使用③(非成员函数重载Non-member function overloads)
这篇文章探讨了C++中`std::string`的`replace`和`swap`函数以及非成员函数重载。`replace`提供了多种方式替换字符串中的部分内容,包括使用字符串、子串、字符、字符数组和填充字符。`swap`函数用于交换两个`string`对象的内容,成员函数版本效率更高。非成员函数重载包括`operator+`实现字符串连接,关系运算符(如`==`, `&lt;`等)用于比较字符串,以及`swap`非成员函数。此外,还介绍了`getline`函数,用于按指定分隔符从输入流中读取字符串。文章强调了非成员函数在特定情况下的作用,并给出了多个示例代码。
|
5天前
|
算法 C++ 容器
|
5天前
|
存储 编译器 程序员
|
5天前
|
存储 编译器 文件存储
|
9天前
|
存储 编译器 C++
【C++】类和对象④(再谈构造函数:初始化列表,隐式类型转换,缺省值
C++中的隐式类型转换在变量赋值和函数调用中常见,如`double`转`int`。取引用时,须用`const`以防修改临时变量,如`const int& b = a;`。类可以有隐式单参构造,使`A aa2 = 1;`合法,但`explicit`关键字可阻止这种转换。C++11起,成员变量可设默认值,如`int _b1 = 1;`。博客探讨构造函数、初始化列表及编译器优化,关注更多C++特性。
|
9天前
|
编译器 C++
【C++】类和对象③(类的默认成员函数:赋值运算符重载)
在C++中,运算符重载允许为用户定义的类型扩展运算符功能,但不能创建新运算符如`operator@`。重载的运算符必须至少有一个类类型参数,且不能改变内置类型运算符的含义。`.*::sizeof?`不可重载。赋值运算符`=`通常作为成员函数重载,确保封装性,如`Date`类的`operator==`。赋值运算符应返回引用并检查自我赋值。当未显式重载时,编译器提供默认实现,但这可能不足以处理资源管理。拷贝构造和赋值运算符在对象复制中有不同用途,需根据类需求定制实现。正确实现它们对避免数据错误和内存问题至关重要。接下来将探讨更多操作符重载和默认成员函数。
|
4天前
|
编译器 C++
【C++】string类的使用④(字符串操作String operations )
这篇博客探讨了C++ STL中`std::string`的几个关键操作,如`c_str()`和`data()`,它们分别返回指向字符串的const char*指针,前者保证以&#39;\0&#39;结尾,后者不保证。`get_allocator()`返回内存分配器,通常不直接使用。`copy()`函数用于将字符串部分复制到字符数组,不添加&#39;\0&#39;。`find()`和`rfind()`用于向前和向后搜索子串或字符。`npos`是string类中的一个常量,表示找不到匹配项时的返回值。博客通过实例展示了这些函数的用法。
|
4天前
|
C++
【C++】string类的使用④(常量成员Member constants)
C++ `std::string` 的 `find_first_of`, `find_last_of`, `find_first_not_of`, `find_last_not_of` 函数分别用于从不同方向查找目标字符或子串。它们都返回匹配位置,未找到则返回 `npos`。`substr` 用于提取子字符串,`compare` 则提供更灵活的字符串比较。`npos` 是一个表示最大值的常量,用于标记未找到匹配的情况。示例代码展示了这些函数的实际应用,如替换元音、分割路径、查找非字母字符等。