#include<stdio.h>
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
LOG // 日志
int main()
{
stack is;
}
template
class Array
{
enum { INIT = 10 };
T* data;
size_t capacity;
size_t count;
public:
typedef T value_type;
typedef T& reference;
typedef const T& const_reference;
typedef T* pointer;
typedef const T* const_pointer;
typedef pointer iterator;
typedef const_pointer const_iterator;
public:
Array() :count(0), capacity(INIT)
{
data = new T[capacity]; // 10
}
~Array()
{
delete[]data;
data = nullptr;
capacity = 0;
count = 0;
}
void push_back(const T& val)
{
if (count == capacity)
{
size_t newCap = 2 * capacity;
T* newData = new T[newCap];
for (int i = 0; i < capacity; ++i)
{
newData[i] = data[i];
}
delete[]data;
data = newData;
capacity = newCap;
}
data[count++] = val;
}
void pop_back()
{
if (count > 0)
–count;
}
iterator begin() { return data; }
iterator end() { return data + count; }
};
template<class T, class Seq = Array >
class Container
{
private:
Seq seq; // Array seq;
public:
void append(const T& val)
{
seq.push_back(val);
}
T* begin() {
return seq.begin();
}
T* end() {
return seq.end();
}
};
int main()
{
Container is;
}
// Container<int, Array > cont;
template
class Container
{
Seq seq; // Array seq;
public:
typedef typename Seq::value_type value_type;
typedef typename Seq::reference reference;
typedef typename Seq::const_reference const_reference;
typedef typename Seq::pointer pointer;
typedef typename Seq::const_pointer const_pointer;
typedef typename Seq::iterator iterator;
typedef typename Seq::const_iterator const_iterator;
public:
void append(const_reference val) { seq.push_back(val); }
iterator begin() { return seq.begin(); }
iterator end() { return seq.end(); }
};
int main()
{
Container<Array > cont;
cont.append(1.2);
cont.append(2.3);
cont.append(3.4);
Container<Array >::iterator p = cont.begin();
for (; p != cont.end(); ++p)
{
cout << *p << endl;
}
return 0;
}
int main()
{
Container<double, Array > cont;
cont.append(12);
cont.append(23);
cont.append(34);
int* p = cont.begin();
for (; p != cont.end(); ++p)
{
cout << *p << endl;
}
return 0;
}
//
template<class T, template class Seq>
class Container
{
private:
Seq seq; // Array seq;
public:
void append(const T& val)
{
seq.push_back(val);
}
T* begin() {
return seq.begin();
}
T* end() {
return seq.end();
}
};
int main()
{
Container<int, Array> cont;
cont.append(12);
cont.append(23);
cont.append(34);
int* p = cont.begin();
for (; p != cont.end(); ++p)
{
cout << *p << endl;
}
return 0;
}
struct Student
{
string _id;
string _name;
string _sex;
int _age;
public:
Student()
{
cout << "Create Student " << endl;
}
};
void fun(int a, int b, int c= 0, int d = 0)
{
}
template
class Stack
{
private:
T data[N];
size_t count;
public:
void Push(const T& val)
{
}
};
int main()
{
Stack<> is;
is.Push(12);
return 0;
}
// typedef unsigned int size_t;
// T N
template<class T,size_t N>
class Stack
{
private:
T data[N];
size_t count;
public:
void Push(const T& val)
{
N = 100;
}
};
int main()
{
Stack<int, 10> is;
Stack<int, 20> ss;
is.Push(12);
return 0;
}
template
class Stack
{
T* data;
size_t count;
public:
Stack() {
//data = new T[10]; // T Student //
data = (T*)malloc(sizeof(T) * 10);
if (nullptr == data) exit(1);
count = -1;
}
void Push(const T& val)
{
//data[++count] = val;
count += 1;
new(&data[count]) T(val);
}
};
int main()
{
Stack ss;
Stack is;
return 0;
}
// STL template class template func;
// STL
// STL
// STL alloc
//atoi; my_atoi;
//itoa my_itoa
//
class My_Mutex
{
private:
public:
My_Mutex() {}
~My_Mutex() {}
void lock() {}
void unlock() {}
void try_lock() {}
};
int main()
{
stringstream ss;
int a, b;
cin >> a >> b;
ss << "a = " << a << " b = " << b << ‘\n’;
string s = ss.str();
sprintf_s(str, 10000, "%d", a);
}
int main()
{
char str[200];
int a = 123, b = 20;
sprintf_s(str, 20, "a = %d b = %d ", a, b);
printf_s("a = %d b = %d ", a, b); // stdout;
return 0;
}
int main()
{
int ar[10];
ifstream ifile(“tulun.txt”, ios::binary);
if (!ifile.is_open())
{
cout << "file open error " << endl;
exit(1);
}
ifile.read((char*)ar, sizeof(int) * 10);
ifile.close();
return 0;
}
int main()
{
const int len = 10;
int ar[len] = { 12,23,34,45,56,67,78,89,90,100 };
// 0C,17, 64
ofstream ofile(“tulun.txt”, ios::binary);
if (!ofile.is_open())
{
cout << "file open error " << endl;
exit(1);
}
ofile.write((char*)ar, sizeof(ar));
ofile.close();
return 0;
}
int main()
{
int val = 0;
ifstream ifile(“yhp.txt”);
if (!ifile.is_open())
{
cout << "file open error " << endl;
exit(1);
}
int n = 0;
ifile >> n;
for (int i = 0; i < n; ++i)
{
ifile >> val;
cout << val << endl;
}
ifile.close();
return 0;
}
class Int : public std::enable_shared_from_this
{
private:
int value;
public:
Int(int x = 0) :value(x) { cout << "Create Int: " << this << endl; }
~Int() { cout << "Destroy Int: " << this << endl; }
void PrintInt()const { cout << value << endl; }
std::shared_ptr GetShared()
{
//return std::shared_ptr(this);
return shared_from_this();
}
};
int main()
{
Int* ip = new Int(10);
std::shared_ptr pInta = ip->GetShared();
std::shared_ptr pIntb = ip->GetShared();
cout << pInta.use_count() << endl;
cout << pIntb.use_count() << endl;
return 0;
}
int main()
{
std::shared_ptr pInta(new Int(10));
std::shared_ptr pIntb = pInta->GetShared();
pInta->PrintInt();
pIntb->PrintInt();
cout << pInta.use_count() << endl;
cout << pIntb.use_count() << endl;
return 0;
}
int main()
{
int val;
ifstream ifile(“yhp2.txt”);
if (!ifile)
{
exit(1);
}
int n;
ifile >> n;
int i = 0;
while (i < n)
{
ifile >> val;
cout << val << endl;
//cin >> val;
++i;
}
ifile.close();
return 0;
}
class String
{
char* str;
public:
String(const char* p = nullptr) :str(nullptr)
{
}
~String() {}
operator bool() const { return str != nullptr; }
};
void func()
{
String s;
if (s)
if(s.operator bool())
{
}
}
int main()
{
const int len = 10;
int ar[len] = { 12,23,34,45,56,67,78,89,90,100 };
ofstream ofile(“yhp2.txt”);
if (!ofile)
{
cout << "file open error " << endl;
exit(1);
}
ofile << 10 << endl;
for (int i = 0; i < len; ++i)
{
ofile << ar[i] << " ";
}
ofile.close();
return 0;
}
int main()
{
char str[10];
int ar[10] = { 12,23,34,45,56,67,78,89,90,100 };
FILE* fp = nullptr;
errno_t tag = fopen_s(&fp, “yhp.txt”, “w”);
// “w” create // clear; “ab”
if (tag)
{
strerror_s(str, tag);
cout << str << endl;
exit(1);
}
for (int i = 0; i < 10; ++i)
{
//printf("%d ", ar[i]); // stdout;
fprintf(fp, "%d ", ar[i]); //
//sprintf(str, "%d ", ar[i]); //
}
fclose(fp);
return 0;
}
//C11
int main()
{
char str[256];
FILE* fp = nullptr; //
errno_t tag = fopen_s(&fp, “Y:\yhp.txt”, “w”);
if (tag)
{
cout << tag << endl;// 2
cout << strerror_s(str,256,tag) << endl;
cout << str << endl;
return 0;
}
return 0;
}
int main()
{
const int len = 256;
char str[len];
//cin >> str; // yhping hello newdata print
//cin.getline(str, len);
cin.getline(str, len, ‘#’);// \n
cout << str << endl;
return 0;
}
int main()
{
char str[256];
int x;
cout << "input int value: " << endl;
cin >> x;
while (cin.fail())
{
cout << cin.rdstate() << endl;
cin.clear(0);
cin.getline(str, 256);
cout << " input int value: " << endl;
cin >> x;
}
cout << x << endl;
return 0;
}
int main()
{
int a = 10;
//int tag = scanf_s(“%d”, &a); // a
//cout << a << endl;
cin >> a;
cout << a << endl;
return 0;
}
int main()
{
int a = 10;
printf("%d %o %x ", a, a, a);
cout << a << hex << a << oct << a << endl;
return 0;
}
#include
#include
// ostream , istream
#include
#include
#include
// ifstream // ofstream
using namespace std;
int main()
{
stdin;
stdout;
stderr;
stdext;
int a = 10;
cout << a << endl;
return 0;
}
class Test
{
public:
void print() const noexcept
{
}
};
void fun() noexcept
{
}
void funa(int n) noexcept(n > 0)
{
}
void func() noexcept(false)
{
}
// c98
//c11
void fun() throw()
{
}
void funa() throw(std::bad_alloc, std::out_of_range)
{
func();
}
int main()
{
}
template
class MyVector
{
private:
_Ty* _first;
_Ty* _Last;
_Ty* _End;
public:
_Ty& operator[](const int index)
{
if (index < 0 || index > size())
{
throw std::out_of_range();
}
return _frist[index];
}
};
class Object
{
public:
virtual void fun() {}
};
class Base : public Object
{
public:
virtual void fun() {}
};
//
int main()
{
Base base;
Object obj;
Object& ob = obj;
try
{
Base& ba = dynamic_cast<Base&>(ob);
ba.fun();
}
catch (std::bad_cast& e)
{
cout << e.what() << endl;
}
return 0;
}
int main()
{
Object* op = nullptr;
Base* bp = nullptr;
Base base;
Object obj;
op = &base;
//op = &obj;
bp = dynamic_cast<Base*>(op);
return 0;
}
void funa(int n)
{
int* p = (int*)malloc(sizeof(int) * n);
if (nullptr == p)
{
exit(1);
}
///
free(p); //
}
//
int funb(int n) try
{
int* ip = new int[n];
//
throw std::bad_alloc();
delete[]ip;
return n;
}
catch (std::bad_alloc& e)
{
cout << e.what() << endl;
cout << “bad_alloc” << endl;
}
int main()
{
int x = funb(10);
cout << x << endl;
return 0;
}
void funb(int n)
{
try
{
int* p = nullptr;
p = new int[n];
// for(int i = 0;i<n;++i)
delete[]p;
}
catch (std::bad_alloc& e)
{
cout << e.what() << endl;
}
}
int main()
{
int* p = new int[10];
int* s = (int*)::operator new(sizeof(int) * 10);
// throw bad_alloc
if (nullptr == p)
{
cout << “bad_alloc” << endl;
exit(1);
}
}
#endif