C++標準擴展應(yīng)用技巧分享
作者:佚名
我們今天在這篇文章中主要為大家介紹了C++標準擴展的一些基本應(yīng)用。對C++編程語言有興趣的朋友們可以從中加深對這一語言的認知程度。
對于經(jīng)驗豐富的編程人員來說,C++編程語言他們應(yīng)該再熟悉不過了。這樣一款功能強大的語言,給他們的程序開發(fā)帶來了非常大的好處,我們今天就可以從C++標準擴展的方法來體驗一下這語言的功能特點。
今天實驗一下C++標準擴展中的shared_ptr的使用,結(jié)果在gcc4.1上怎么也編譯不過去,上網(wǎng)查了一下,還下載了TR1的手冊。終于明白了,要在#include中加入
- #include < tr1/memory>
- #include < iostream>
- #include < string>
- #include < tr1/array>
- #include < tr1/memory>
- using namespace std;
- using std::tr1::shared_ptr;
- class Widget
- {
- public:
- Widget()
- {
- pstr = new string("Hello world!");
- cout < < "Widget's construction is called" < < endl;
- }
- Widget(const Widget& rhs) { cout < < "Widget's copy
construction is called" < < endl; }- Widget& operator=(const Widget& rhs) { return *this; }
- ~Widget()
- {
- delete pstr;
- cout < < "Destruction is called" < < endl;
- }
- private:
- string* pstr;
- };
- int main()
- {
- auto_ptr< Widget> pInv(new Widget);
- auto_ptr< Widget> pInv2(pInv);
- shared_ptr< Widget> pInvN(new Widget);
- array< int, 5> a = {{1,2,3,4,5}};
- cout < < a[3] < < endl;
- return 0;
- }
這個文件。呵呵,可能是自己太不小心了!這次C++標準擴展的部分,根據(jù)TR1的說明主要有:
- Reference Wrappers
- Shared_ptr
- result_of
- mem_fn
- Function Object Binders
- Polymorphic Function Wrappers
- Type Traits
- Random Numbers
- Tuples
- Array
- Hash Functions
- Regular Expressions
- Complex Number Algorithms
這些C++標準擴展部分,我們看到了期待以久的正則表達式也在這里面哦!
【編輯推薦】
責任編輯:曹凱
來源:
博客園