#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;
}
C/C++基础 (typename)
最新推荐文章于 2025-05-14 21:52:40 发布