C/C++基础 (typename)

本文深入探讨了C++模板的使用方法,包括泛型函数和类的设计,并详细解析了标准模板库(STL)中的关键数据结构如vector、list和string的实现原理与操作方法,展示了如何通过模板和STL提升代码效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<stdio.h>
#include<string.h>
#include<vector>
#include<list>
#include<string>
using namespace std;

template <typename T, typename S>
S findmax(T arr[], int len) {
	T val = arr[0];
	for (int i = 1; i < len; i++) {
		if (arr[i] > val) val = arr[i];
	}
	return (int)val;
}


template<typename T>
class Array {
public:
	Array(int capacity=4 ) {
		m_buffer = new T[capacity];
		m_capacity = capacity;
		m_size = 0;
	}
	void PushBack(T val) {
		if (m_size >= m_capacity) {
			Resize();
		}
		print();
		m_buffer[m_size] = val;
		m_size++;
	}
	T* getall() {
		return m_buffer;
	}
private:
	void Resize() {
		int n = m_capacity + 4;
		T* buf = new T[n];
		memcpy(buf, m_buffer,m_capacity);
		delete[] m_buffer;
		m_capacity = n;
		m_buffer = buf;
	}
private:
	T* m_buffer;
	int m_capacity;
	int m_size;
};
void print() {
	printf("han shu bu yong qian zhi shenming\n");
}
int main() {
	double arr[4] = { 1.0,2.0,3.0,100.0 };
	int res = findmax<double, int>(arr, 4);
	Array<double> arr1;
	arr1.PushBack(1.1);
	arr1.PushBack(1.1);
	arr1.PushBack(1.1);
	arr1.PushBack(1.1);
	arr1.PushBack(1.1);
	
	vector<int> arr2(16);
	int capacity = arr2.capacity(); //16
	int size = arr2.size(); // 16
	arr2[0] = 21;
	arr2[1] = 22; // other is 0
	arr2.push_back(123456);
	
	capacity = arr2.capacity(); //24
	size = arr2.size(); // 17

	vector<int>::iterator iter;
	for (iter = arr2.begin(); iter != arr2.end(); iter++) {
		int& value = *iter;
		printf("%d\n", value);
	}
	
	arr2.clear();
	capacity = arr2.capacity(); //24
	size = arr2.size(); // 0

	list<int> lst;
	lst.push_back(1);
	lst.push_back(1);
	lst.push_back(2);
	lst.push_back(3);
	lst.push_back(4);
	lst.pop_front();
	for (list<int>::iterator iter = lst.begin(); iter != lst.end(); iter++) {
		int& v = *iter;
		printf("%d\n", v);
	}
	for (list<int>::iterator iter = lst.begin(); iter != lst.end(); iter++) {
		int& v = *iter;
		if (v == 3) {
			lst.erase(iter);
			break;
		}
	}
	for (list<int>::iterator iter = lst.begin(); iter != lst.end(); iter++) {
		int& v = *iter;
		printf("%d\n", v);
	}

	list<Array<double>> arr3;
	arr3.push_back(Array<double>());
	arr3.push_back(Array<double>());
	for (list<Array<double>>::iterator iter = arr3.begin(); iter != arr3.end(); iter++) {
		(*iter).PushBack(10.0);
	}
	for (list<Array<double>>::iterator iter = arr3.begin(); iter != arr3.end(); iter++) {
		printf("%f\n",(*iter).getall()[0]);
	}

	string str1("LiMing");
	string str2 = "WangHua";
	string str4("abcdfgh", 2); //"ab "
	string str5("ab ", 5); //"ab "
	string str6; // string str4="";
	//string str7 = NULL; !!!!!!EORR!!!!!

	string text("hello,world!");
	const char* p = text.c_str();
	printf("str: %s\n",p);
	text.append("aaaaaaaaa", 3);
	text.append("aabbbbbbbbbbb", 2, 3);
	text.append(3,'c');
	printf("str: %s\n", p);
	const char* p1 = text.c_str();
	printf("str: %s\n", p1);

	text[0] = 'H';
	const char* p2 = text.c_str();
	printf("str: %s\n", p2);
	bool b = (text == "Hello,world!aaabbbccc"); // true
	int pos = text.find('!');  // 11, if not -1
	int pos1 = text.find("!aaabbb");  // 11, if not -1
	string sll =text.substr(3);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值