文章目录
- 目录
- 1.模板
- 2.预处理器
- 3.信号处理
目录
1.模板
模板是泛型编程的基础,泛型编程:以一种独立于任何特定类型的方式
#include <iostream>
#include <string>using namespace std;template <typename T>
inline T const& Max(T const &a , T const &b){return a<b?b:a;
}int main(){int i = 10 , j = 11;cout<<"int\t"<<Max(i,j)<<endl;double m = 13.5 , n = 11.3;cout<<"double\t"<<Max(m,n)<<endl;string s1 = "wfy", s2 = "hxm";cout<<"string\t"<<Max(s1,s2)<<endl;return 0;
}
int 11
double 13.5
string wfy
#include <iostream>
#include <vector>
#include <cstdlib>
#include <string>
#include <stdexcept>using namespace std;template <class T>
class Stack { private: vector<T> elems; // 元素 public: void push(T const&); // 入栈void pop(); // 出栈T top() const; // 返回栈顶元素bool empty() const{ // 如果为空则返回真。return elems.empty(); }
}; template <class T>
void Stack<T>::push (T const& elem)
{ // 追加传入元素的副本elems.push_back(elem);
} template <class T>
void Stack<T>::pop ()
{ if (elems.empty()) { throw out_of_range("Stack<>::pop(): empty stack"); }// 删除最后一个元素elems.pop_back();
} template <class T>
T Stack<T>::top () const
{ if (elems.empty()) { throw out_of_range("Stack<>::top(): empty stack"); }// 返回最后一个元素的副本 return elems.back();
} int main()
{ try { Stack<int> intStack; // int 类型的栈 Stack<string> stringStack; // string 类型的栈 // 操作 int 类型的栈 intStack.push(7); cout << intStack.top() <<endl; // 操作 string 类型的栈 stringStack.push("hello"); cout << stringStack.top() << std::endl; stringStack.pop(); stringStack.pop(); } catch (exception const& ex) { cerr << "Exception: " << ex.what() <<endl; return -1;}
}
当上面的代码被编译和执行时,它会产生下列结果:
7
hello
Exception: Stack<>::pop(): empty stack
第一个例子中typename改为class也是可以的:
#include <iostream>
#include <string>using namespace std;template <class T>
inline T const& Max (T const& a, T const& b)
{ return a < b ? b:a;
}
int main ()
{int i = 39;int j = 20;cout << "Max(i, j): " << Max(i, j) << endl; double f1 = 13.5; double f2 = 20.7; cout << "Max(f1, f2): " << Max(f1, f2) << endl; string s1 = "Hello"; string s2 = "World"; cout << "Max(s1, s2): " << Max(s1, s2) << endl; return 0;
}
Max(i, j): 39
Max(f1, f2): 20.7
Max(s1, s2): World
2.预处理器
3.信号处理
#include <iostream>
#include <csignal>
#include <unistd.h>using namespace std;void signalHandler( int signum )
{cout << "Interrupt signal (" << signum << ") received.\n";// 清理并关闭// 终止程序 exit(signum); }int main ()
{// 注册信号 SIGINT 和信号处理程序signal(SIGINT, signalHandler); while(1){cout << "Going to sleep...." << endl;sleep(1);}return 0;
}
当上面的代码被编译和执行时,它会产生下列结果
Going to sleep…
Going to sleep…
Going to sleep…
现在,按 Ctrl+C 来中断程序,您会看到程序捕获信号,程序打印如下内容并退出:
Going to sleep…
Going to sleep…
Going to sleep…
Interrupt signal (2) received.
#include <iostream>
#include <csignal>
#include <unistd.h>using namespace std;void signalHandler( int signum )
{cout << "Interrupt signal (" << signum << ") received.\n";// 清理并关闭// 终止程序 exit(signum); }int main ()
{int i = 0;// 注册信号 SIGINT 和信号处理程序signal(SIGINT, signalHandler); while(++i){cout << "Going to sleep...." << endl;if( i == 3 ){raise( SIGINT);}sleep(1);}return 0;
}
当上面的代码被编译和执行时,它会产生下列结果,并会自动退出:
Going to sleep…
Going to sleep…
Going to sleep…
Interrupt signal (2) received.