C++中如何象printf一樣設定顯示格式

使用前需引用標頭檔#include <iomanip> cout.precision(x),setprecision(x)設定精度float f = 33.456; cout.precision(3);cout<<f<<endl;cout<<setprecision(3)<<f<<endl;// out:33.5 一些其它函數dec //置基數為10 相當於"%d" hex //置基數為16 相當於"%X" oct

C++類的成員函數指標

//*********************************************************//條款一 類的成員函數指標變數之間可相互強轉//*********************************************************class A{};class B{};class C{};typedef void (A::*AFun)(void);typedef int (B::*BFun)(int, int);typedef void (C:

C#讀取Xml檔案

C#中可以用XmlDocument類操作Xml檔案例如要讀取如下Xml檔案1 <root> 2 <person name="WangYao"> 3 <age>25</age> 4 </person> 5 <person name="Jobs"> 6 <age>56</age> 7 </person> 8 </root> 程式如下

C++ cout 輸出 16, 8 , 2進位

C++ cout 輸出 16, 8 , 2進位#include <iostream>#include <iomanip>#include <bitset>using std::bitset;using std::hex;using std::oct;using std::cout;using std::cin;using std::endl;int main(){ int

union+int within assembly and C language

只談一個問題:head_common.S中__switch_data:.long   init_thread_union + THREAD_START_SPinit_thread_union 使用ctags無法跳轉,且連結指令碼中沒有init_thread_union,使用grep搜尋下發現這是在arch/unicore/kernel/init_task.c3 

effective C++ 條款 40:明智而審慎地使用多重繼承

一旦涉及多重繼承(multiple inheritance;MI):程式有可能從一個以上的base class繼承相同名稱(如函數、typedef等)。那會導致較多的歧義機會。例如:class BorrowableItem { public:     void checkOut(); };class ElectronicGadet { private:     bool checkOut() const; };class

effective C++ 條款 28:避免返回handles指向對象內部成分

假設程式涉及矩形。為了讓Rectangle對象儘可能小,可能把定義矩形的點放在一個輔助的struct內再讓Rectangle去指它:class Point{ public:     Point(int x, int y);     ...     void setX(int newVal);     void setY(int newVal);     ... }; struct RectData{    

effective C++ 條款 38:通過複合塑模出has-a或“根據某物實現出”

複合(composition)是類型之間的一種關係,當某種類型的對象內含它種類型的對象,便是這種關係:class Address {...}; class PhoneNumber {...}; class Person { public:     ... private:     std::string name;//合成成分物     Address address;//合成成分物     PhoneNumber

effective C++ 條款 20:寧以pass-by-reference-to-const替換pass-by-value

預設情況下c++以by value 的方式傳遞對象(或來自)函數。函數參數是以實參的副本為初值,用函數獲得的也是函數傳回值的一個副本這些副本由對象的copy建構函式產出,這可能使得pass-by-value成為昂貴的操作:class Person { public:     Person();     virtual ~Person(); protected: private:     std::string name;

effective C++ 條款 18:讓介面容易被正確使用,不易被誤用

如果客戶企圖使用某個介面而卻沒有獲得他所預期的行為,這個代碼就不該通過編譯,如果代碼通過了編譯,它的作為就該是客戶所想要的。 class Date { public:      Date(int month, int day, int year);      ... };第一,以錯誤的次序傳遞參數:Date(30, 3, 1998);//應該是“3,30”而不是“30,3”第二,傳遞一個無效的月份或天數:(打岔一個鍵) Date(2,

effective C++ 條款 27:盡量少做轉型動作

轉型(casts)破壞了類型系統(type system)。可能導致任何類型的麻煩。c++提供四種新式轉型const_cast<T>(expression) //cast away the constnessdynamic_cast<T>(expression) //safe

effective C++ 條款 48:認識template元編程

template metaprogramming(模板元編程)是編寫template-based

effective C++ 條款 37:絕不重新定義繼承而來的預設參數值

重新定義一個繼承而來的non-virtual函數永遠都是錯誤的,本條款的討論限制在“帶有預設參數的virtual函數”。virtual函數是動態綁定的,而預設參數卻是靜態繫結。對象的所謂靜態類型,是它在程式中被聲明時所採用的類型。class Shape { public:     enum ShapeColor {Red, Green, Blue};     virtual void draw(ShapeColor color = Red) const = 0;

effective C++ 條款 29:為“異常安全”而努力是值得的

有個class用來表現夾帶背景圖案的GUI菜單單,這個class用於多線程環境:class PrettyMenu{ public:     ...     void changeBackground(std::istream& imgSrc);     ... private:     Mutex mutex;     Image* bgImage;     int imageChanges; };

effective C++ 條款 17:以獨立語句將newed對象置入智能指標

假設一個函數用來揭示處理常式的優先權int priority();另一個函數用來在動態分配的Widget上進行某些帶有優先權的處理:void processWidget(std::tr1::shared_ptr<Widget> pw, int priority);考慮調用processWidget:processWidget(new Widget,

effective C++ 條款 39:明智而審慎地使用private繼承

c++中public繼承視為is-a關係。現在看private繼承:class Person{...}; class Student: private Person {...}; void eat(const Person& p); void study(const Student& s);Person p; Student s; eat(p); eat(s); //錯誤! 難道學生不是人?!顯然private繼承不是is-

effective C++ 條款 47:使用traits classes表現類型資訊

stl主要由“用以表現容器、迭代器和演算法”的template構成,但也覆蓋若干工具性的templates,其中一個名為advance,將某個迭代器移動某個給定距離:template<typename IterT, typename DistT> void advance(IterT& iter, DistT d);   

effective C++ 條款 19:設計class猶如設計type

設計優秀的classes和是一項艱巨的工作,因為設計好的types是一項艱巨的工作。設計出至少像c++內建類型一樣好的classes。幾乎每一個class都要求面對以下提問,回答往往導致你的設計規範:1,新type的對象應該如何建立和銷毀?包括構造和解構函式,記憶體配置和釋放函數(operator new, operator delete,operator new[], operator

effective C++ 條款 49:瞭解new-handler的行為

當operator new無法滿足某一記憶體配置需求時,會拋出異常。再拋出異常以反映一個未獲滿足的記憶體需求之前,它會先調用客戶指定的錯誤處理函數,new-handler。為了指定這個“用以處理記憶體不足”的函數,客戶必須調用set-new-handler,那是聲明於<new>的一個標準函數庫函數:namespace std{     typedef void (*new_handler)();     new_handler

effective C++ 條款 30:透徹瞭解inlining的裡裡外外

inline函數,可以調用它們而又不需蒙受函數調用所招致的額外開銷當你inline某個函數,或許編譯器就因此又能力對它(函數本體)執行語境相關最佳化。然而,inline函數背後的整體觀念是,將“對此函數的每一個調用”都已函數本體替換之,這樣做可能增加你的目標碼(object

總頁數: 4314 1 .... 843 844 845 846 847 .... 4314 Go to: 前往

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.