青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

我輩豈是蓬蒿人!

C++ && keyWordSpotting

  C++博客 :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
  11 Posts :: 0 Stories :: 4 Comments :: 0 Trackbacks

常用鏈接

留言簿(9)

我參與的團(tuán)隊

搜索

  •  

積分與排名

  • 積分 - 7306
  • 排名 - 1368

最新評論

閱讀排行榜

評論排行榜

2006年8月29日 #

?

?1?#ifndef?_GHH_TIMER_GHH_
?2?#define?_GHH_TIMER_GHH_????1
?3?
?4?//?File:?ghhTimer.h
?5?//?Date:?2006.08.14
?7?
?8?#include?<ctime>
?9?
10?//?類導(dǎo)出導(dǎo)入類別的符號定義
11?#ifdef?_DLL_FILE_
12?#define?PORTTYE?__declspec(dllexport)?//?導(dǎo)出
13?#else
14?#define?PORTTYE?__declspec(dllimport)?//?導(dǎo)入
15?#endif????//?end?of?_DLL_FILE_
16?
17?/****************************************************************************
18??*?類名稱????ghhTimer
19??*?
20??*?描述
21??*????本類對標(biāo)準(zhǔn)庫計時函數(shù)進(jìn)行了封裝,可以實現(xiàn)非常精確的計時,毫秒級別
22??*
23??*?使用說明
24??*????在所要計時程序段之前,調(diào)用Start函數(shù),程序段結(jié)束時,調(diào)用Pause函數(shù),
25??*????多次調(diào)用程序段,即可以比較精確的估計程序段的運行時間
26?****************************************************************************/
27?class??PORTTYE?ghhTimer
28?{
29?public:
30?????ghhTimer();
31?
32?public:
33?????bool?Start(void);?
34?????bool?Stop(void);????
35?????bool?Pause(void);
36?????size_t?GetSeconds(void)?const;
37?????size_t?GetMiliSeconds(void)?const;
38?
39?private:
40?????enum?{run?=?1,?stop,?pause}?_Status;
41?????time_t?_Clock;
42?????time_t?_TotalClocks;
43?};
44?
45?#endif?//?end?of?_GHH_TIMER_GHH_

??1?#ifndef?_DLL_FILE_
??2?#define?_DLL_FILE_
??3?#endif
??4?#include?"ghhTimer.h"
??5?
??6?/****************************************************************************
??7??*?about?the?important?function?"clock()"
??8??*?#include?<time.h>
??9??*?clock_t?clock(?void?);
?10??*?The?clock()?function?returns?the?processor?time?since?the?program?started,?
?11??*?or?-1?if?that?information?is?unavailable.?
?12??*?To?convert?the?return?value?to?seconds,?divide?it?by?CLOCKS_PER_SEC.?
?13??*?(Note:?if?your?compiler?is?POSIX?compliant,?
?14??*?then?CLOCKS_PER_SEC?is?always?defined?as?1000000.)
?15??***************************************************************************/
?16?
?17?
?18?//?構(gòu)造函數(shù),設(shè)置初始狀態(tài)
?19?ghhTimer::ghhTimer()?:?_Status(stop),?_Clock(0),?_TotalClocks(0)
?20?{
?21?}
?22?
?23?//?當(dāng)表已經(jīng)停止或者暫停時啟動停表,成功返回true,否則返回false
?24?bool?ghhTimer::Start(void)
?25?{
?26?????switch?(_Status)
?27?????{
?28?????case?stop?:
?29?????????_TotalClocks?=?0;
?30?????????_Clock?=?clock();
?31?????????break;
?32?
?33?????case?pause?:
?34?????????_Clock?=?clock();
?35?????????break;
?36?
?37?????case?run?:
?38?????????break;
?39?
?40?????default?:
?41?????????return?false;
?42?????}
?43?
?44?????_Status?=?run;
?45?
?46?????return?true;
?47?}
?48?
?49?//?表運行時暫停計時,成功返回true,否則返回false
?50?bool?ghhTimer::Pause(void)
?51?{
?52?????switch?(_Status)
?53?????{
?54?????case?stop?:
?55?????case?pause?:
?56?????????break;
?57?
?58?????case?run?:
?59?????????_TotalClocks?+=?(clock()?-?_Clock);
?60?????????_Clock?=?0;
?61?????????_Status?=?pause;
?62?????????break;
?63?
?64?????default?:
?65?????????return?false;
?66?????}
?67?
?68?????return?true;
?69?}
?70?
?71?//?表運行或暫停時停止計時
?72?bool?ghhTimer::Stop(void)
?73?{
?74?????switch?(_Status)
?75?????{
?76?????case?stop?:
?77?????case?pause?:
?78?????????break;
?79?????????
?80?????case?run?:
?81?????????_TotalClocks?+=(clock()?-?_Clock);
?82?????????_Clock?=?0;
?83?????????break;
?84?
?85?????default?:
?86?????????return?false;
?87?????}
?88?
?89?????_Status?=?stop;
?90?????
?91?????return?true;
?92?}
?93?
?94?//?得到當(dāng)前積累的秒數(shù)
?95?size_t?ghhTimer::GetSeconds(void)?const
?96?{
?97?????time_t?Clocks;
?98?
?99?????switch?(_Status)
100?????{
101?????case?stop:
102?????case?pause:
103?????????Clocks?=?_TotalClocks;
104?????????break;
105?
106?????case?run:
107?????????Clocks?=?_TotalClocks?+?clock()?-?_Clock;
108?????????break;
109?
110?????default:
111?????????return?false;
112?????}
113?????return?(Clocks?/?CLOCKS_PER_SEC);
114?}
115?
116?//?得到當(dāng)前積累的毫秒數(shù)
117?size_t?ghhTimer::GetMiliSeconds(void)?const
118?{
119?????time_t?Clocks;
120?
121?????switch(_Status)
122?????{
123?????case?stop:
124?????case?pause:
125?????????Clocks?=?_TotalClocks;
126?????????break;
127?????case?run:
128?????????Clocks?=?_TotalClocks?+?clock()?-?_Clock;
129?????????break;
130?
131?????default:
132?????????return?false;
133?????}
134?????return?(Clocks?*?1000?/?CLOCKS_PER_SEC);
135?}
136?
posted @ 2006-08-29 09:51 keyws 閱讀(964) | 評論 (0)編輯 收藏

2006年8月22日 #

Shifting from C to C++

1. To C++ programmer, for example, a pointer to a pointer looks a little funny. Why, we wonder, wasn’t a reference to a pointer used ?instead?

?????? const char chr[] = "chenzhenshi&guohonghua";

?????? const char*? pchr = chr;

?????? const char** ppchr = &pchr;

?????? const char*&? rpchr? = pchr; // a reference to a pointer

?????? std::cout << pchr << ' ' << *ppchr << ' ' << rpchr << std::endl;

?

2. C is a fairly simple language. All it really offers is macros, pointers, structs, arrays, and functions. No matter what the problem is, the solution will always boil down to macros, pointers, structs, arrays, and functions. Not so in C++. The macros, pointers, structs, arrays and functions are still there, of course, but so are private and protected members, function overloading, default parameters, constructors and destructors, user-defined operators, inline functions, references, friends, templates, exceptions, namespaces, and more. The design space is much richer in C++ than it is in C: there are just a lot more options to consider.

Item 1: Prefer const and inline to #define

3. The Item might better be called “prefer the compiler to the preprocessor”.

4. ?? const char* pc;

?????? pc = a1;

?????? std::cout << pc << std::endl;

?????? pc = a2;

?????? std::cout << pc << std::endl;

?

?????? const char* const pcc = "a const pointer to a const char array";

?????? std::cout << pcc << std::endl;

?????? // error C2166: l-value specifies const object

?????? // pcc = a1;? // error!

?????? std::cout << pcc << std::endl;

5. You can define a const variable in a class, but it must be static const, and have a definition in an implementation file.

// .h file

class try_const

{

public:

?????? static const int num;

};

// .cxx file

const int try_const::num = 250;

6. You can get all the efficiency of a macro plus all the predictable behavior and type safety of a regular function by using an inline function.

Template <class type>

Inline const type& max (const type& a, const type& b)

{

Return a > b ? a : b ;

}

7. Given the availability of consts and inlines, your need for the preprocessor is reduced, but it's not completely eliminated. The day is far from near when you can abandon #include, and #ifdef/#ifndef continue to play important roles in controlling compilation. It's not yet time to retire the preprocessor, but you should definitely plan to start giving it longer and more frequent vacations.

Item 2: Prefer <iostream> to <stdio.h>

8. ?scanf and printf are not type-safe and extensible.

9.? In particular, if you #include <iostream>, you get the elements of the iostream library ensconced within the namespace std (see Item 28), but if you #include <iostream.h>, you get those same elements at global scope. Getting them at global scope can lead to name conflicts, precisely the kinds of name conflicts the use of namespaces is designed to prevent.

Item 3: Prefer new and delete to malloc and free

10. The problem with malloc and free(and their variants) is simple : they don’t know about constructors and destructors.

11. free 操作不會調(diào)用析構(gòu)函數(shù),如果指針?biāo)笇ο蟊旧碛址峙淞藘?nèi)存,則會造成內(nèi)存丟失。

Item 4: Prefer C++ style comments

Memory Management

12. Memory management concerns in C++ fall into two general camps: getting it right and making it perform efficiently.

Item 5: Use the same form in corresponding uses of new and delete

13. When you use new, two things happen. First, memory is allocated. Second, one or more constructors are called for that memory. When you use delete, two other things happen: one or more destructors are called for the memory, then the memory is deallocated.

14. The standard C++ library includes string and vector templates that reduce the need for built-in arrays to nearly zero.

Item 6: Use delete on pointer members in destructors

15. Speaking of smart pointers, one way to avoid the need to delete pointer members is to replace those members with smart pointer objects like the standard C++ Library’s auto_ptr.

Item 7: Be prepared for out-of-memory conditions

Item 8: Adhere to convention when writing operator new and operator delete

Item 9: Avoid hiding the “normal” form of new

Item 10: Write operator delete if you write operator new

讓我們回過頭去看看這樣一個基本問題:為什么有必要寫自己的 operator new operator delete ?答案通常是:為了效率。缺省的 operator new operator delete 具有非常好的通用性,它的這種靈活性也使得在某些特定的場合下,可以進(jìn)一步改善它的性能。尤其在那些需要動態(tài)分配大量的但很小的對象的應(yīng)用程序里,情況更是如此。

?

posted @ 2006-08-22 17:24 keyws 閱讀(527) | 評論 (0)編輯 收藏

??
???動態(tài)分配的對象:程序員完全控制分配與釋放,分配在程序的空閑存儲區(qū)(free store)的可用內(nèi)存池中。
?
?1
)單個對象的動態(tài)分配與釋放;
?new
表達(dá)式?jīng)]有返回實際分配的對象,而是返回指向該對象的指針。對該對象的全部操作都要通過這個指針間接完成。
?
隨機分配的內(nèi)存具有隨機的位模式,建議初始化,例如:

?int* pi = new int(0);


?

空閑存儲區(qū)是有限的資源,若被耗盡,new表達(dá)式會失敗,拋出bad_alloc異常。
?
這樣做沒有必要:

???? if ?(?pi?!=?0?)
????????delete?pi;

? 說明:如果指針操作數(shù)被設(shè)置為0,則C++保證delete表達(dá)式不會調(diào)用操作符delete()。所以沒有必要測試其是否為0
?
delete表達(dá)式之后,pi被稱作空懸指針,即指向無效內(nèi)存的指針。空懸指針是程序錯誤的根源,建議對象釋放后,將該指針設(shè)置為0
?
?2
auto_ptr
?auto_ptr
C++標(biāo)準(zhǔn)庫提供的類模板,它可以幫助程序員自動管理用new表達(dá)式動態(tài)分配的單個對象,但是,它沒有對數(shù)組管理提供類似支持。它的頭文件為:
?

????#include? < memory > ?

? 當(dāng)auto_ptr對象的生命期結(jié)束時,動態(tài)分配的對象被自動釋放。
?auto_ptr
類模板背后的主要動機是支持與普通指針類型相同的語法,但是為auto_ptr對象所指對象的釋放提供自動管理。例:

???? //? 第一種初始化形式
????std::auto_ptr< int >?pi(? new ? int (1024)?);???? //?

?auto_ptr 類模板支持所有權(quán)概念,當(dāng)一個auto_ptr對象被用另一個auto_ptr對象初始化賦值時,左邊被賦值或初始化的對象就擁有了空閑存儲區(qū)內(nèi)底層對象的所有權(quán),而右邊的auto_ptr對象則撤消所有責(zé)任。例:

????std::auto_ptr<std:: string >?pstr_auto(? new ?std:: string (?"Brontonsaurus"?)?);
????
//?
第二種初始化形式
????std::auto_ptr<std:: string >?pstr_auto2(?pstr_auto?);

? 判斷是否指向一個對象,例:

???? // ?第三種初始化形式
????auto_ptr < int > ?p_auto_int;????
????
if ?(?p_auto_int. get ()? == ? 0 ?)
????????
????
else
????????
// ?重置底層指針,必須使用此函數(shù)????????
????????p_auto_int.reset(? new ? int (? 1024 ?)?);


?3)數(shù)組的動態(tài)分配與釋放
?
建議使用C++標(biāo)準(zhǔn)庫string,避免使用C風(fēng)格字符串?dāng)?shù)組。
?
為避免動態(tài)分配數(shù)組的內(nèi)存管理帶來的問題,一般建議使用標(biāo)準(zhǔn)庫vectorliststring容器類型。
?
?4
)常量對象的動態(tài)分配與釋放
?
可以使用new表達(dá)式在空閑存儲區(qū)內(nèi)創(chuàng)建一個const對象,例:

???? //? 此時必須初始化,否則編譯錯誤
???? const ? int *?pci?=? new ? const ? int (1024);????

? 我們不能在空閑存儲區(qū)創(chuàng)建內(nèi)置類型元素的const數(shù)組,原因是:我們不能初始化用new表達(dá)式創(chuàng)建的內(nèi)置類型數(shù)組的元素。例:

???? const ? int *?pci?=? new ? const ? int [100];? //? 編譯錯誤


?5
)定位new表達(dá)式
?new
表達(dá)式的第三種形式允許程序員要求將對象創(chuàng)建在已經(jīng)被分配好的內(nèi)存中。稱為:定位new表達(dá)式(placement new expression)。程序員在new表達(dá)式中指定待創(chuàng)建對象所在的內(nèi)存地址。如下所示:
?new
place_address) type-specifier
?
注意:place_address必須是個指針,必須包含頭文件<new>。這項設(shè)施允許程序員預(yù)分配大量的內(nèi)存,供以后通過這種形式的new表達(dá)式創(chuàng)建對象。例如:

????#include? < iostream >
????#include?
< new > ???? // ?必須包含這個頭文件
????
????
const ? int ?chunk? = ? 16 ;
????
class ?Foo
????
{
????????
????}
;
????
????
char * ?buf? = ? new ? char [? sizeof (Foo)? * ?chunk?];
????
????
int ?main( int ?argc,? char * ?argv[])
????
{
????????
// ?只有這種形式的創(chuàng)建,沒有配對形式的delete?
????????Foo * ?pb? = ? new ?(buf)?Foo;
????????????????
????????delete[]?buff;
????????
????????
return ? 0 ;
????}

posted @ 2006-08-22 14:54 keyws 閱讀(623) | 評論 (3)編輯 收藏

2006年8月20日 #

Introduction

1. A declaration tells compilers about the name and type of an object, function, class, or template, but it omits certain details.

2. A definition, on the other hand, provides compilers with the details. For an object, the definition is where compilers allocate memory for the object. For a function or a function template, the definition provides the code body. For a class or a class template, the definition lists the members of the class or template.

3. When you define a class, you generally need a default constructor if you want to define arrays of objects.Incidentally, if you want to create an array of objects for which there is no default constructor, the usual ploy is to define an array of pointers instead. Then you can initialize each pointer separately by using new.

4. Probably the most important use of the copy constructor is to define what it means to pass and return objects by value.

5. From a purely operational point of view, the difference between initialization and assignment is that the former is performed by a constructor while the latter is performed by operator=. In other words, the two processes correspond to different function calls. The reason for the distinction is that the two kinds of functions must worry about different things. Constructors usually have to check their arguments for validity, whereas most assignment operators can take it for granted that their argument is legitimate (because it has already been constructed). On the other hand, the target of an assignment, unlike an object undergoing construction, may already have resources allocated to it. These resources typically must be released before the new resources can be assigned. Frequently, one of these resources is memory. Before an assignment operator can allocate memory for a new value, it must first deallocate the memory that was allocated for the old value.

// ?a?possible?String?constructor
String::String( const ? char ? * value)
{
????
if ?(value)
????
{?
????????
// ?if?value?ptr?isn't?null
????????data? = ? new ? char [strlen(value)? + ? 1 ];
????????strcpy(data,value);
????}
????
????
else ?
????
{?
????????
// ?handle?null?value?ptr3
????????data? = ? new ? char [ 1 ];
????????
* data? = ? ' \0 ' ;? // ?add?trailing
???????? null ? char
????}

}


// ?a?possible?String?assignment?operator

String
& ?String:: operator = ( const ?String & ?rhs)
{
????
if ?( this ? == ? & rhs)
????????
return ? * this ;? // ?see?Item?17

????delete?[]?data;?
// ?delete?old?memory
????
????data?
= ? // ?allocate?new?memory
???????? new ? char [strlen(rhs.data)? + ? 1 ];

????strcpy(data,?rhs.data);
????
????
return ? * this ;? // ?see?Item?15
}


6. These different casting forms serve different purposes:

const_cast is designed to cast away the constness of objects and pointers, a topic I examine in Item 21.

dynamic_cast is used to perform "safe downcasting," a subject we'll explore in Item 39.

reinterpret_cast is engineered for casts that yield implementation-dependent results, e.g., casting between function pointer types. (You're not likely to need reinterpret_cast very often. I don't use it at all in this book.)

static_cast is sort of the catch-all cast. It's what you use when none of the other casts is appropriate. It's the closest in meaning to the conventional C-style casts.

posted @ 2006-08-20 16:10 keyws 閱讀(358) | 評論 (0)編輯 收藏

2006年8月17日 #

     摘要: Thinking in C++, 2nd ed. Volume 1 ?2000 by Bruce Eckel Unary operators The following example shows the syntax to overload all the unary operators, ...  閱讀全文
posted @ 2006-08-17 12:14 keyws 閱讀(626) | 評論 (0)編輯 收藏

2006年8月16日 #

???學(xué)習(xí)數(shù)據(jù)結(jié)構(gòu),用到了輸入輸出運算符重載,結(jié)果編譯之下,錯誤重重,,隨即停止學(xué)習(xí)進(jìn)度,追查禍源,在花費巨大腦力與時間成本之后,終于知自己錯誤之所在,定位于名字空間之困擾。為牢記教訓(xùn),寫一簡化版本記錄 debug 過程如下,警示自己。

問題
???

???有如下兩個代碼文件:
???// 20060816_operator.cxx

?1?#include?<iostream>
?2?using?namespace?std;
?3?#include?"20060816_operator.h"
?4?int?main(int?argc,?char*?argv[])
?5?{
?6?????const?std::string?GHH("GuoHonghua");
?7?????Honghua?ghh(GHH);
?8?????cout?<<?ghh?<<?endl;
?9?????return?0;
10?}
???

// 20060816_operator.h

?1?#ifndef?_20060816_OPERATOR_GHH_
?2?#define?_20060816_OPERATOR_GHH_????1
?3?
?4?//?Author?:?GuoHonghua
?5?//?Date?:?2006.08.16
?6?//?File?:?20060816_operator.h
?7?
?8?#include?<iostream>
?9?#include?<string>
10?class?Honghua
11?{
12?????friend?std::ostream&?operator<<(std::ostream&?os,?const?Honghua&?ghh);
13?public:
14?????Honghua(const?std::string&?ghh)?:?_ghh(ghh)
15?????{
16?????}
17?
18?private:
19?????std::string?_ghh;
20?};
21?
22?std::ostream&?operator<<(std::ostream&?os,?const?Honghua&?ghh)
23?{
24?????os?<<?ghh._ghh;
25?????return?os;
26?}
27?
28?#endif?//?end?of?_20060816_OPERATOR_GHH_

用上面兩個文件建立工程,vc6.0下編譯會出錯。報告如下:

--------------------Configuration: 20060816_operator - Win32 Debug--------------------

Compiling...

20060816_operator.cxx

f:\ghh_project\cxxdetail\20060816_operator.h(24) : error C2248: '_ghh' : cannot access private member declared in class 'Honghua'

??????? f:\ghh_project\cxxdetail\20060816_operator.h(19) : see declaration of '_ghh'

F:\ghh_project\CxxDetail\20060816_operator.cxx(8) : error C2593: 'operator <<' is ambiguous

Error executing cl.exe.

?

20060816_operator.exe - 2 error(s), 0 warning(s)
解決:
???

1.? 調(diào)整名字空間聲明和自定義頭文件的次序,如下:

1?#include?<iostream>
2?#include?"20060816_operator.h"
3?using?namespace?std;
4?int?main(int?argc,?char*?argv[])
5?

2.? 把類定義于標(biāo)準(zhǔn)名字之內(nèi),20060816_operator.h文件修改如下(不推薦)

?1?#ifndef?_20060816_OPERATOR_GHH_
?2?#define?_20060816_OPERATOR_GHH_????1
?3?
?4?//?Author?:?GuoHonghua
?5?//?Date?:?2006.08.16
?6?//?File?:?20060816_operator.h
?7?
?8?#include?<iostream>
?9?#include?<string>
10?namespace?std
11?{
12?????class?Honghua
13?????{
14?????????friend?std::ostream&?operator<<(std::ostream&?os,?const?Honghua&?ghh);
15?????public:
16?????????Honghua(const?std::string&?ghh)?:?_ghh(ghh)
17?????????{
18?????????}
19?????????
20?????private:
21?????????std::string?_ghh;
22?????};
23?????
24?????
25?????std::ostream&?operator<<(std::ostream&?os,?const?Honghua&?ghh)
26?????{
27?????????os?<<?ghh._ghh;
28?????????return?os;
29?????}
30?}
31?#endif?//?end?of?_20060816_OPERATOR_GHH_?

3.? 使用頭文件iostream.h代替iostream,比較落后的方式(不推薦!

?1?//?20060816_operator.cxx
?2?//?#include?<iostream>
?3?#include?<iostream.h>
?4?#include?<iostream.h>
?5?#include?"20060816_operator.h"
?6?int?main(int?argc,?char*?argv[])
?7?{
?8?????const?std::string?GHH("GuoHonghua");
?9?????Honghua?ghh(GHH);
10?????cout?<<?ghh?<<?endl;
11?????return?0;
12?}?

?1?//?20060816_operator.h
?2?#ifndef?_20060816_OPERATOR_GHH_
?3?#define?_20060816_OPERATOR_GHH_????1
?4?
?5?//?Author?:?GuoHonghua
?6?//?Date?:?2006.08.16
?7?//?File?:?20060816_operator.h
?8?//?#include?<iostream>
?9?#include?<iostream.h>
10?#include?<string>
11?
12?class?Honghua
13?{
14?????friend?ostream&?operator<<(ostream&?os,?const?Honghua&?ghh);
15?public:
16?????Honghua(const?std::string&?ghh)?:?_ghh(ghh)
17?????{
18?????}
19?????
20?private:
21?????std::string?_ghh;
22?};
23?
24?
25?ostream&?operator<<(ostream&?os,?const?Honghua&?ghh)
26?{
27?????os?<<?ghh._ghh;
28?????return?os;
29?}
30?
31?#endif?//?end?of?_20060816_OPERATOR_GHH_

4.? 任何時候都不使用using namespace 聲明!這是只需要修改20060816_operator.cxx文件如下:

?1?#include?<iostream>
?2?#include?"20060816_operator.h"
?3?int?main(int?argc,?char*?argv[])
?4?{
?5?????//?const?string?GHH("GuoHonghua");
?6?????const?std::string?GHH("GuoHonghua");
?7?????Honghua?ghh(GHH);
?8?????//?cout?<<?ghh?<<?endl;
?9?????std::cout?<<?ghh?<<?std::endl;
10?????return?0;
11?}

體會
???名字空間本來就是為了解決名字沖突問題而引入,以使標(biāo)準(zhǔn)庫不受外界影響。在這個意義上來說,
using namespace std;不是可以隨意使用的語句,應(yīng)該考慮到它的位置對程序的可能影響,而最徹底的杜絕錯誤的解決辦法是在任何時候任何情況下都不使用此語句!

posted @ 2006-08-16 21:09 keyws 閱讀(422) | 評論 (0)編輯 收藏

2006年8月13日 #

Syntax:
														
1?#include?<vector>
2?void?assign(?size_type?num,?const?TYPE&?val?);
3?void?assign(?input_iterator?start,?input_iterator?end?);

The assign() function either gives the current vector the values from start to end, or gives it num copies of val.

This function will destroy the previous contents of the vector.

For example, the following code uses assign() to put 10 copies of the integer 42 into a vector:

 vector<int> v;
 v.assign( 10, 42 );
 for( int i = 0; i < v.size(); i++ ) {
   cout << v[i] << " ";
 }
 cout << endl;            

The above code displays the following output:

 42 42 42 42 42 42 42 42 42 42          

The next example shows how assign() can be used to copy one vector to another:

 vector<int> v1;
 for( int i = 0; i < 10; i++ ) {
   v1.push_back( i );
 }              

 vector<int> v2;
 v2.assign( v1.begin(), v1.end() );             

 for( int i = 0; i < v2.size(); i++ ) {
   cout << v2[i] << " ";
 }
 cout << endl;            

When run, the above code displays the following output:

 0 1 2 3 4 5 6 7 8 9

back
Syntax:
				
1?#include?<vector>
2?TYPE&?back();
3?const?TYPE&?back()?const;

The back() function returns a reference to the last element in the vector.

For example:

 vector<int> v;
 for( int i = 0; i < 5; i++ ) {
   v.push_back(i);
 }
 cout << "The first element is " << v.front()
      << " and the last element is " << v.back() << endl;           

This code produces the following output:

 The first element is 0 and the last element is 4               


at
Syntax:
  #include <vector>
  TYPE& at( size_type loc );
  const TYPE& at( size_type loc ) const;

The at() function returns a reference to the element in the vector at index loc. The at() function is safer than the [] operator, because it won't let you reference items outside the bounds of the vector.

For example, consider the following code:

 vector<int> v( 5, 1 );
 for( int i = 0; i < 10; i++ ) {
   cout << "Element " << i << " is " << v[i] << endl;
 }              

This code overrunns the end of the vector, producing potentially dangerous results. The following code would be much safer:

 vector<int> v( 5, 1 );
 for( int i = 0; i < 10; i++ ) {
   cout << "Element " << i << " is " << v.at(i) << endl;
 }              

Instead of attempting to read garbage values from memory, the at() function will realize that it is about to overrun the vector and will throw an exception.

capacity
Syntax:
  #include <vector>
  size_type capacity() const;

The capacity() function returns the number of elements that the vector can hold before it will need to allocate more space.

For example, the following code uses two different methods to set the capacity of two vectors. One method passes an argument to the constructor that suggests an initial size, the other method calls the reserve function to achieve a similar goal:

 vector<int> v1(10);
 cout << "The capacity of v1 is " << v1.capacity() << endl;
 vector<int> v2;
 v2.reserve(20);
 cout << "The capacity of v2 is " << v2.capacity() << endl;         

When run, the above code produces the following output:

 The capacity of v1 is 10
 The capacity of v2 is 20               

C++ containers are designed to grow in size dynamically. This frees the programmer from having to worry about storing an arbitrary number of elements in a container. However, sometimes the programmer can improve the performance of her program by giving hints to the compiler about the size of the containers that the program will use. These hints come in the form of the reserve() function and the constructor used in the above example, which tell the compiler how large the container is expected to get.

The capacity() function runs in constant time.


begin
Syntax:
  #include <vector>
  iterator begin();
  const_iterator begin() const;

The function begin() returns an iterator to the first element of the vector. begin() should run in constant time.

For example, the following code uses begin() to initialize an iterator that is used to traverse a list:

   // Create a list of characters
   list<char> charList;
   for( int i=0; i < 10; i++ ) {
     charList.push_front( i + 65 );
   }
   // Display the list
   list<char>::iterator theIterator;
   for( theIterator = charList.begin(); theIterator != charList.end(); theIterator++ ) {
     cout << *theIterator;
   }            
max_size
Syntax:
  #include <vector>
  size_type max_size() const;

The max_size() function returns the maximum number of elements that the vector can hold. The max_size() function should not be confused with the size() or capacity() functions, which return the number of elements currently in the vector and the the number of elements that the vector will be able to hold before more memory will have to be allocated, respectively.

clear
Syntax:
  #include <vector>
  void clear();

The function clear() deletes all of the elements in the vector. clear() runs in linear time.

empty
Syntax:
  #include <vector>
  bool empty() const;

The empty() function returns true if the vector has no elements, false otherwise.

For example, the following code uses empty() as the stopping condition on a (C/C++ Keywords) while loop to clear a vector and display its contents in reverse order:

 vector<int> v;
 for( int i = 0; i < 5; i++ ) {
   v.push_back(i);
 }
 while( !v.empty() ) {
   cout << v.back() << endl;
   v.pop_back();
 }              
end
Syntax:
  #include <vector>
  iterator end();
  const_iterator end() const;

The end() function returns an iterator just past the end of the vector.

Note that before you can access the last element of the vector using an iterator that you get from a call to end(), you'll have to decrement the iterator first. This is because end() doesn't point to the end of the vector; it points just past the end of the vector.

For example, in the following code, the first "cout" statement will display garbage, whereas the second statement will actually display the last element of the vector:

  vector<int> v1;
  v1.push_back( 0 );
  v1.push_back( 1 );
  v1.push_back( 2 );
  v1.push_back( 3 );

  int bad_val = *(v1.end());
  cout << "bad_val is " << bad_val << endl;

  int good_val = *(v1.end() - 1);
  cout << "good_val is " << good_val << endl;

The next example shows how begin() and end() can be used to iterate through all of the members of a vector:

 vector<int> v1( 5, 789
  ); vector<int>::iterator it; for( it = v1.begin(); it !=
  v1.end(); it++ ) { cout << *it << endl; } 

The iterator is initialized with a call to begin(). After the body of the loop has been executed, the iterator is incremented and tested to see if it is equal to the result of calling end(). Since end() returns an iterator pointing to an element just after the last element of the vector, the loop will only stop once all of the elements of the vector have been displayed.

end() runs in constant time.


erase
Syntax:
  #include <vector>
  iterator erase( iterator loc );
  iterator erase( iterator start, iterator end );

The erase() function either deletes the element at location loc, or deletes the elements between start and end (including start but not including end). The return value is the element after the last element erased.

The first version of erase (the version that deletes a single element at location loc) runs in constant time for lists and linear time for vectors, dequeues, and strings. The multiple-element version of erase always takes linear time.

For example:

 // Create a vector, load it with the first ten characters of the alphabet
 vector<char> alphaVector;
 for( int i=0; i < 10; i++ ) {
   alphaVector.push_back( i + 65 );
 }
 int size = alphaVector.size();
 vector<char>::iterator startIterator;
 vector<char>::iterator tempIterator;
 for( int i=0; i < size; i++ ) {
   startIterator = alphaVector.begin();
   alphaVector.erase( startIterator );
   // Display the vector
   for( tempIterator = alphaVector.begin(); tempIterator != alphaVector.end(); tempIterator++ ) {
     cout << *tempIterator;
   }
   cout << endl;
 }              

That code would display the following output:

 BCDEFGHIJ
 CDEFGHIJ
 DEFGHIJ
 EFGHIJ
 FGHIJ
 GHIJ
 HIJ
 IJ
 J              

In the next example, erase() is called with two iterators to delete a range of elements from a vector:

 // create a vector, load it with the first ten characters of the alphabet
 vector<char> alphaVector;
 for( int i=0; i < 10; i++ ) {
   alphaVector.push_back( i + 65 );
 }
 // display the complete vector
 for( int i = 0; i < alphaVector.size(); i++ ) {
   cout << alphaVector[i];
 }
 cout << endl;            

 // use erase to remove all but the first two and last three elements
 // of the vector
 alphaVector.erase( alphaVector.begin()+2, alphaVector.end()-3 );
 // display the modified vector
 for( int i = 0; i < alphaVector.size(); i++ ) {
   cout << alphaVector[i];
 }
 cout << endl;            

When run, the above code displays:

 ABCDEFGHIJ
 ABHIJ          

front
Syntax:
  #include <vector>
  TYPE& front();
  const TYPE& front() const;

The front() function returns a reference to the first element of the vector, and runs in constant time.


insert
Syntax:
  #include <vector>
  iterator insert( iterator loc, const TYPE& val );
  void insert( iterator loc, size_type num, const TYPE& val );
  template<TYPE> void insert( iterator loc, input_iterator start, input_iterator end );

The insert() function either:

  • inserts val before loc, returning an iterator to the element inserted,
  • inserts num copies of val before loc, or
  • inserts the elements from start to end before loc.

Note that inserting elements into a vector can be relatively time-intensive, since the underlying data structure for a vector is an array. In order to insert data into an array, you might need to displace a lot of the elements of that array, and this can take linear time. If you are planning on doing a lot of insertions into your vector and you care about speed, you might be better off using a container that has a linked list as its underlying data structure (such as a List or a Deque).

For example, the following code uses the insert() function to splice four copies of the character 'C' into a vector of characters:

 // Create a vector, load it with the first 10 characters of the alphabet
 vector<char> alphaVector;
 for( int i=0; i < 10; i++ ) {
   alphaVector.push_back( i + 65 );
 }              

 // Insert four C's into the vector
 vector<char>::iterator theIterator = alphaVector.begin();
 alphaVector.insert( theIterator, 4, 'C' );             

 // Display the vector
 for( theIterator = alphaVector.begin(); theIterator != alphaVector.end(); theIterator++ )    {
   cout << *theIterator;
 }              

This code would display:

 CCCCABCDEFGHIJ         

Here is another example of the insert() function. In this code, insert() is used to append the contents of one vector onto the end of another:

  vector<int> v1;
  v1.push_back( 0 );
  v1.push_back( 1 );
  v1.push_back( 2 );
  v1.push_back( 3 );

  vector<int> v2;
  v2.push_back( 5 );
  v2.push_back( 6 );
  v2.push_back( 7 );
  v2.push_back( 8 );

  cout << "Before, v2 is: ";
  for( int i = 0; i < v2.size(); i++ ) {
    cout << v2[i] << " ";
  }
  cout << endl;

  v2.insert( v2.end(), v1.begin(), v1.end() );

  cout << "After, v2 is: ";
  for( int i = 0; i < v2.size(); i++ ) {
    cout << v2[i] << " ";
  }
  cout << endl;

When run, this code displays:

  Before, v2 is: 5 6 7 8
  After, v2 is: 5 6 7 8 0 1 2 3

Vector constructors
Syntax:
  #include <vector>
  vector();
  vector( const vector& c );
  vector( size_type num, const TYPE& val = TYPE() );
  vector( input_iterator start, input_iterator end );
  ~vector();

The default vector constructor takes no arguments, creates a new instance of that vector.

The second constructor is a default copy constructor that can be used to create a new vector that is a copy of the given vector c.

The third constructor creates a vector with space for num objects. If val is specified, each of those objects will be given that value. For example, the following code creates a vector consisting of five copies of the integer 42:

 vector<int> v1( 5, 42 );         

The last constructor creates a vector that is initialized to contain the elements between start and end. For example:

 // create a vector of random integers
 cout << "original vector: ";
 vector<int> v;
 for( int i = 0; i < 10; i++ ) {
   int num = (int) rand() % 10;
   cout << num << " ";
   v.push_back( num );
 }
 cout << endl;            

 // find the first element of v that is even
 vector<int>::iterator iter1 = v.begin();
 while( iter1 != v.end() && *iter1 % 2 != 0 ) {
   iter1++;
 }              

 // find the last element of v that is even
 vector<int>::iterator iter2 = v.end();
 do {
   iter2--;
 } while( iter2 != v.begin() && *iter2 % 2 != 0 );              

 cout << "first even number: " << *iter1 << ", last even number: " << *iter2 << endl;         

 cout << "new vector: ";
 vector<int> v2( iter1, iter2 );
 for( int i = 0; i < v2.size(); i++ ) {
   cout << v2[i] << " ";
 }
 cout << endl;            

When run, this code displays the following output:

 original vector: 1 9 7 9 2 7 2 1 9 8
 first even number: 2, last even number: 8
 new vector: 2 7 2 1 9          

All of these constructors run in linear time except the first, which runs in constant time.

The default destructor is called when the vector should be destroyed.


pop_back
Syntax:
  #include <vector>
  void pop_back();

The pop_back() function removes the last element of the vector.

pop_back() runs in constant time.


push_back
Syntax:
  #include <vector>
  void push_back( const TYPE& val );

The push_back() function appends val to the end of the vector.

For example, the following code puts 10 integers into a list:

   list<int> the_list;
   for( int i = 0; i < 10; i++ )
     the_list.push_back( i );           

When displayed, the resulting list would look like this:

 0 1 2 3 4 5 6 7 8 9            

push_back() runs in constant time.


rbegin
Syntax:
  #include <vector>
  reverse_iterator rbegin();
  const_reverse_iterator rbegin() const;

The rbegin() function returns a reverse_iterator to the end of the current vector.

rbegin() runs in constant time.


rend
Syntax:
  #include <vector>
  reverse_iterator rend();
  const_reverse_iterator rend() const;

The function rend() returns a reverse_iterator to the beginning of the current vector.

rend() runs in constant time.


reserve
Syntax:
  #include <vector>
  void reserve( size_type size );

The reserve() function sets the capacity of the vector to at least size.

reserve() runs in linear time.


resize
Syntax:
  #include <vector>
  void resize( size_type num, const TYPE& val = TYPE() );

The function resize() changes the size of the vector to size. If val is specified then any newly-created elements will be initialized to have a value of val.

This function runs in linear time.


size
Syntax:
  #include <vector>
  size_type size() const;

The size() function returns the number of elements in the current vector.


swap
Syntax:
  #include <vector>
  void swap( const container& from );

The swap() function exchanges the elements of the current vector with those of from. This function operates in constant time.

For example, the following code uses the swap() function to exchange the values of two strings:

   string first( "This comes first" );
   string second( "And this is second" );
   first.swap( second );
   cout << first << endl;
   cout << second << endl;          

The above code displays:

   And this is second
   This comes first             

Vector operators
Syntax:
  #include <vector>
  TYPE& operator[]( size_type index );
  const TYPE& operator[]( size_type index ) const;
  vector operator=(const vector& c2);
  bool operator==(const vector& c1, const vector& c2);
  bool operator!=(const vector& c1, const vector& c2);
  bool operator<(const vector& c1, const vector& c2);
  bool operator>(const vector& c1, const vector& c2);
  bool operator<=(const vector& c1, const vector& c2);
  bool operator>=(const vector& c1, const vector& c2);

All of the C++ containers can be compared and assigned with the standard comparison operators: ==, !=, <=, >=, <, >, and =. Individual elements of a vector can be examined with the [] operator.

Performing a comparison or assigning one vector to another takes linear time. The [] operator runs in constant time.

Two vectors are equal if:

  1. Their size is the same, and
  2. Each member in location i in one vector is equal to the the member in location i in the other vector.

Comparisons among vectors are done lexicographically.

For example, the following code uses the [] operator to access all of the elements of a vector:

 vector<int> v( 5, 1 );
 for( int i = 0; i < v.size(); i++ ) {
   cout << "Element " << i << " is " << v[i] << endl;
 }              

posted @ 2006-08-13 19:52 keyws 閱讀(686) | 評論 (0)編輯 收藏

(一)

One of the basic classes implemented by the Standard Template Library is the vector class. A vector is, essentially, a resizeable array; the vector class allows random access via the [] operator, but adding an element anywhere but to the end of a vector causes some overhead as all of the elements are shuffled around to fit them correctly into memory. Fortunately, the memory requirements are equivalent to those of a normal array. The header file for the STL vector library is vector. (Note that when using C++, header files drop the .h; for C header files - e.g. stdlib.h - you should still include the .h.) Moreover, the vector class is part of the std namespace, so you must either prefix all references to the vector template with std:: or include "using namespace std;" at the top of your program.

Vectors are more powerful than arrays because the number of functions that are available for accessing and modifying vectors. Unfortunately, the [] operator still does not provide bounds checking. There is an alternative way of accessing the vector, using the function at, which does provide bounds checking at an additional cost. Let's take a look at several functions provided by the vector class:

1?unsigned?int?size();?//?Returns?the?number?of?elements?in?a?vector
2?push_back(type?element);?//?Adds?an?element?to?the?end?of?a?vector
3?bool?empty();?//?Returns?true?if?the?vector?is?empty
4?void?clear();?//?Erases?all?elements?of?the?vector
5?type?at(int?n);?//Returns?the?element?at?index?n,?with?bounds?checking

also, there are several basic operators defined for the vector class:

=           Assignment replaces a vector's contents with the contents of another
==          An element by element comparison of two vectors
[]          Random access to an element of a vector (usage is similar to that
            of the operator with arrays.) Keep in mind that it does not provide
            bounds checking.

Let's take a look at an example program using the vector class:

				
?1?#include?<iostream>
?2?#include?<vector>
?3?using?namespace?std;
?4?
?5?int?main()
?6?{
?7?????vector?<int>?example;?????????//Vector?to?store?integers
?8?????example.push_back(3);?????????//Add?3? onto?the?vector
?9?????example.push_back(10);????????//Add?10?to?the?end
10?????example.push_back(33);????????//Add?33?to?the?end

11?????for(int?x=0;?x<example.size();?x++)?
12?????{
13?????????cout<<example[x]<<"?";????//Should?output:?3?10?33
14?????}

15?????if(!example.empty())??????????//Checks?if?empty
16?????????example.clear();??????????//Clears?vector
17?????vector?<int>?another_vector;??//Creates?another?vector?to?store?integers
18?????another_vector.push_back(10);?//Adds?to?end?of?vector
19?????example.push_back(10);????????//Same
20?????if(example==another_vector)???//To?show?testing?equality
21?????{
22?????????example.push_back(20);?
23?????}
24?????for(int?y=0;?y<example.size();?y++)
25?????{
26?????????cout<<example[y]<<"?";????//Should?output?10?20
27?????}
28?????return?0;
29?}
Summary of Vector Benefits

Vectors are somewhat easier to use than regular arrays. At the very least, they get around having to be resized constantly using new and delete. Furthermore, their immense flexibility - support for any datatype and support for automatic resizing when adding elements - and the other helpful included functions give them clear advantages to arrays.

Another argument for using vectors are that they help avoid memory leaks--you don't have to remember to free vectors, or worry about how to handle freeing a vector in the case of an exception. This simplifies program flow and helps you write tighter code. Finally, if you use the at() function to access the vector, you get bounds checking at the cost of a slight performance penalty.

(二)

?C++ vector is a container template available with Standard Template Library pack. This C++ vector can be used to store similar typed objects sequentially, which can be accessed at a later point of time. As this is a template, all types of data including user defined data types like struct and class can be stored in this container.

This article explains briefly about how to insert, delete, access the data with respect to the C++ vector. The type stl::string is used for the purposes of explaining the sample code. Using stl::string is only for sample purposes. Any other type can be used with the C++ vector.

The values can be added to the c++ vector at the end of the sequence. The function push_back should be used for this purpose. The <vector> header file should be included in all the header files in order to access the C++ vector and its functions.

?1?#include?<vector>
?2?#include?<string>
?3?#include?<iostream.h>
?4?
?5?void?main()
?6?{
?7?????//Declaration?for?the?string?data
?8?????std::string?strData?=?"One";
?9?????//Declaration?for?C++?vector
10?????std::?vector?<std::string>?str_Vector;
11?????str_Vector.push_back(strData);
12?????strData?=?"Two";
13?????str_Vector.push_back(strData);
14?????strData?=?"Three";
15?????str_Vector.push_back(strData);
16?????strData?=?"Four";
17?????str_Vector.push_back(strData);
18?}

The above code adds 4 strings of std::string type to the str_Vector. This uses std:: vector.push_back function. This function takes 1 parameter of the designated type and adds it to the end of the c++ vector.

Accessing Elements of C++ Vector:

?? The elements after being added can be accessed in two ways. One way is our legacy way of accessing the data with vector.at(position_in_integer) function. The position of the data element is passed as the single parameter to access the data.

Using our normal logic for accessing elements stored in C++ Vector:

If we want to access all the data, we can use a for loop and display them as follows.

1?for(int?i=0;i?<?str_Vector.size();?i++)
2?{
3?????std::string?strd?=?str_Vector.at(i);
4?????cout<<strd.c_str()<<endl;
5?}

The std:: vector .size() function returns the number of elements stored in the vector.

Using C++ vector iterator provided by STL:

The next way is to use the iterator object provided by the STL. These iterators are general purpose pointers allowing c++ programmers to forget the intricacies of object types and access the data of any type.

1?std::vector<std::string>::iterator?itVectorData;
2?for(itVectorData?=?str_Vector.begin();??itVectorData?!=?str_Vector.end();?itVectorData++)
3?{
4?????std::string?strD?=?*(itVectorData);
5?}

Removing elements from C++ vector:

Removing elements one by one from specified positions in c++ vector is achieved with the erase function.

The following code demonstrates how to use the erase function to remove an element from position 2 in the str_Vector from our sample.

1 str_Vector.erase(str_Vector.begin() + 1 ,str_Vector.begin() + 2 );

?The following sample demonstrates how to use the erase() function for removing elements 2,3 in the str_Vector used in the above sample.?

1 str_Vector.erase(str_Vector.begin() + 1 ,str_Vector.begin() + 3 );

If one wants to remove all the elements at once from the c++ vector, the vector.clear() function can be used.

posted @ 2006-08-13 19:36 keyws 閱讀(816) | 評論 (0)編輯 收藏

http://www.cprogramming.com/tutorial/stl/iterators.html

The concept of an iterator is fundamental to understanding the C++ Standard Template Library (STL) because iterators provide a means for accessing data stored in container classes such a vector, map, list, etc.

You can think of an iterator as pointing to an item that is part of a larger container of items. For intance, all containers support a function called begin, which will return an iterator pointing to the beginning of the container (the first element) and function, end, that returns an iterator corresponding to having reached the end of the container. In fact, you can access the element by "dereferencing" the iterator with a *, just as you would dereference a pointer.

To request an iterator appropriate for a particular STL templated class, you use the syntax

1?std::class_name<template_parameters>::iterator?name

where name is the name of the iterator variable you wish to create and the class_name is the name of the STL container you are using, and the template_paramters are the parameters to the template used to declare objects that will work with this iterator. Note that because the STL classes are part of the std namespace, you will need to either prefix every container class type with "std::", as in the example, or include "using namespace std;" at the top of your program.

For instance, if you had an STL vector storing integers, you could create an iterator for it as follows:
1std::vector<int>?myIntVector;
2std::vector<int>::iterator?myIntVectorIterator;
Different operations and containers support different types of iterator behavior. In fact, there are several different classes of iterators, each with slightly different properties. First, iterators are distinguished by whether you can use them for reading or writing data in the container. Some types of iterators allow for both reading and writing behavior, though not necessarily at the same time.

Some of the most important are the forward, backward and the bidirectional iterators. Both of these iterators can be used as either input or output iterators, meaning you can use them for either writing or reading. The forward iterator only allows movement one way -- from the front of the container to the back. To move from one element to the next, the increment operator, ++, can be used.

For instance, if you want to access the elements of an STL vector, it's best to use an iterator instead of the traditional C-style code. The strategy is fairly straightforward: call the container's begin function to get an iterator, use ++ to step through the objects in the container, access each object with the * operator ("*iterator") similar to the way you would access an object by dereferencing a pointer, and stop iterating when the iterator equals the container's end iterator. You can compare iterators using != to check for inequality, == to check for equality. (This only works for one twhen the iterators are operating on the same container!)

The old approach (avoid)
?1?using?namespace?std;
?2?
?3?vector<int>?myIntVector;
?4?
?5?//?Add?some?elements?to?myIntVector
?6?myIntVector.push_back(1);
?7?myIntVector.push_back(4);
?8?myIntVector.push_back(8);
?9?
10?for(int?y=0;?y<myIntVector.size();?y++)
11?{
12?????cout<<myIntVector[y]<<"?";??//Should?output?1?4?8
13?}
The STL approach (use this)
?1?using?namespace?std;
?2?
?3?vector<int>?myIntVector;
?4?vector<int>::iterator?myIntVectorIterator;
?5?
?6?//?Add?some?elements?to?myIntVector
?7?myIntVector.push_back(1);
?8?myIntVector.push_back(4);
?9?myIntVector.push_back(8);
10?
11?for(myIntVectorIterator?=?myIntVector.begin();?
12?????????myIntVectorIterator?!=?myIntVector.end();
13?????????myIntVectorIterator++)
14?{
15?????cout<<*myIntVectorIterator<<"?";????//Should?output?1?4?8
16?}
17?
As you might imagine, you can use the decrement operator, --, when working with a bidirectional iterator or a backward operator.

Iterators are often handy for specifying a particular range of things to operate on. For instance, the range item.begin(), item.end() is the entire container, but smaller slices can be used. This is particularly easy with one other, extremely general class of iterator, the random access iterator, which is functionally equivalent to a pointer in C or C++ in the sense that you can not only increment or decrement but also move an arbitrary distance in constant time (for instance, jump multiple elements down a vector).

For instance, the iterators associated with vectors are random access iterators so you could use arithmetic of the form
iterator + n
where n is an integer. The result will be the element corresponding to the nth item after the item pointed to be the current iterator. This can be a problem if you happen to exceed the bounds of your iterator by stepping forward (or backward) by too many elements.

The following code demonstrates both the use of random access iterators and exceeding the bounds of the array (don't run it!):
1?vector<int>?myIntVector;
2?vector<int>::iterator?myIntVectorIterator;
3?myIntVectorIterator?=?myIntVector.begin()?+?2;
You can also use the standard arithmetic shortcuts for addition and subtraction, += and -=, with random access iterators. Moreover, with random access iterators you can use <, >, <=, and >= to compare iterator positions within the container.

Iterators are also useful for some functions that belong to container classes that require operating on a range of values. A simple but useful example is the erase function. The vector template supports this function, which takes a range as specified by two iterators -- every element in the range is erased. For instance, to erase an entire vector:
1?vector<int>::iterator?myIntVectorIterator;
2?myIntVector.erase(myIntVectorIterator.begin(),?myIntVectorIterator.end());
which would delete all elements in the vector. If you only wanted to delete the first two elements, you could use
1myIntVector.erase(myIntVectorIterator.begin(),?myIntVectorIterator.begin()+2);
Note that various container class support different types of iterators -- the vector class, which has served as our model for iterators, supports a random access iterator, the most general kind. Another container, the list container (to be discussed later), only supports bidirectional iterators.

So why use iterators? First, they're a flexible way to access the data in containers that don't have obvious means of accessing all of the data (for instance, maps [to be discussed later]). They're also quite flexible -- if you change the underlying container, it's easy to change the associated iterator so long as you only use features associated with the iterator supported by both classes. Finally, the STL algorithms defined in <algorithm> (to be discussed later) use iterators.

Summary

The Good
  • The STL provides iterators as a convenient abstraction for accessing many different types of containers.
  • Iterators for templated classes are generated inside the class scope with the syntax
    class_name<parameters>::iterator
    
  • Iterators can be thought of as limited pointers (or, in the case of random access iterators, as nearly equivalent to pointers)
The Gotchas
  • Iterators do not provide bounds checking; it is possible to overstep the bounds of a container, resulting in segmentation faults
  • Different containers support different iterators, so it is not always possible to change the underlying container type without making changes to your code
  • Iterators can be invalidated if the underlying container (the container being iterated over) is changed significantly
posted @ 2006-08-13 19:04 keyws 閱讀(556) | 評論 (0)編輯 收藏

2006年8月12日 #

?????? 初學(xué)函數(shù)模版,一個“ bug” 讓俺郁悶了一番,想了一個小時都沒有想出所以然。雖求助于網(wǎng)絡(luò),發(fā)貼于 vckbase

?????? 問題已經(jīng)解決,帖子摘要如下:

??????: 函數(shù)模版問題,為什么不能采用多文件方式。?
??????: 郭晨 ( 書童)
所屬論壇: C++ 論壇
本帖分?jǐn)?shù): 0
回復(fù)次數(shù): 4
發(fā)表時間: 2006-8-12 19:09:50
正文內(nèi)容:
1

#include <fstream>
#include <iostream>
using namespace std;
template<class Type >
Type min(Type a, Type b)
{
????return a < b ? a : b;
}

int main(int argc, char* argv[])
{
????cout << min(10, 20) << endl;
????return 0;
}

可以編譯成功,運行得到正確結(jié)果。

2
)多文件方式

///////////////////////main.cxx
#include "min.h"
#include <fstream>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
????cout << min(10, 20) << endl;
????return 0;
}
///////////////////////min.h
#ifndef _MIN_GHH_
#define _MIN_GHH_??1

template<class Type >
Type min(Type a, Type b);

#endif

//////////////////////min.cxx
#include "min.h"

template<class Type >
Type min(Type a, Type b)
{
????return a < b ? a : b;
}

編譯報告錯誤:
Linking...
20060812_function_template.obj : error LNK2001: unresolved external symbol "int __cdecl min(int,int)" (?min@@YAHHH@Z)
Debug/20060812_function_template.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

20060812_function_template.exe - 2 error(s), 0 warning(s)

////////////////////////////////////
問題:為什么會出現(xiàn)這種問題,如何解決?盼賜教。
最新修改:2006-8-12 19:41:10

?

回復(fù)人: newgun ( 書童)?

2006-8-12 19:34:29 ( 得分: 10 )?

Re: 函數(shù)模版問題,為什么不能采用多文件方式。
c++標(biāo)準(zhǔn)中規(guī)定可以采用分離的編譯模式,只需要通過在模板定義中的關(guān)鍵字template 之前加上關(guān)鍵字export 來聲明一個可導(dǎo)出的函數(shù)模板當(dāng)函數(shù)模板,被導(dǎo)出時我們就可以在任意程序文本文件中使用模板的實例如:
??// model2.h
//
分離模式: 只提供模板聲明
template <typename Type> Type min( Type t1, Type t2 );
// model2.C
// the template definition
export template <typename Type>????//export
關(guān)鍵字
Type min( Type t1, Type t2 ) { /* ...*/ }

????
但是好像現(xiàn)在的好多編譯器如vc7都還不支持這種結(jié)構(gòu),所以最好把模板的聲明
和定義都放在頭文件里

?????

?????? 感受:得到網(wǎng)友提示,真有一種“柳暗花明又一村”之感,這個所謂 bug 是這么簡單又是這么不簡單!

?????? 一直認(rèn)為應(yīng)該是自己的錯誤,但沒想到是編譯器和標(biāo)準(zhǔn)兼容性的問題。看來自己的思維習(xí)慣應(yīng)該有所改變了。應(yīng)該多角度分析問題,而不能一味的想當(dāng)然。

?????? Ps :自己也應(yīng)該考慮嘗試一個新的 C++ 編譯器了,老用 vc ,感覺學(xué)的標(biāo)準(zhǔn) C++ 都不純,就象這次事件一樣。

posted @ 2006-08-12 20:32 keyws 閱讀(655) | 評論 (1)編輯 收藏

2006年8月11日 #

?????? 屈指一數(shù),兩年小碩,已過算半。回憶研一,展望研二,心中頗多感慨,喜憂四七開。

?????? 喜,相比入學(xué)之初,知識層雖無脫胎換骨之感,但也小有所學(xué),比已比人,進(jìn)步不少!

?????? 憂,兩年小碩,時間緊湊,于學(xué)術(shù)研究難有大成;再現(xiàn)實一點,雖學(xué)通信,但找一滿意工作,仍壓力不輕,任重道遠(yuǎn)!

?????? 讀研不似本科,成熟多了,也開始考慮現(xiàn)實,不敢虛浮度日,但真努力奮斗起來,仍有迷茫不知所為之惑。

?????? 不過,還好,還有一顆雖笨拙但還善于反思的腦袋,雖彎路不少,但也算一步步的向前行了。

?????? 兩年,真的太短!何況已浪費一年光陰哉 ?!

?????? 無論為了工作,為了畢業(yè),還是為了自己做點成果來,都是該努力的時候了!

?????? 不談人生意義等等大道理,就是為了追求一種生存的基礎(chǔ),在一只腳已經(jīng)踏入社會的時候,也該明白自己應(yīng)該做些什么了!

?????? 管理自己,做該做的事,做一個積極的自己!完善自我!努力!?

? ?

?????? Ps :謹(jǐn)以此文,做開博紀(jì)念,也為我意義上的研二開始的紀(jì)念。

?

posted @ 2006-08-11 22:28 keyws 閱讀(699) | 評論 (0)編輯 收藏

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            亚洲欧美久久久久一区二区三区| 亚洲视频一区在线| 久久精品一区二区三区四区| 国精品一区二区| 麻豆成人在线播放| 欧美成人精品在线播放| 亚洲三级影院| 亚洲午夜三级在线| 国产一区美女| 亚洲黄页一区| 国产精品久久久久影院色老大| 欧美亚洲一区二区在线| 久久久欧美一区二区| 亚洲国产综合在线看不卡| 亚洲黄页一区| 国产日韩综合| 国产精品日日摸夜夜摸av| 国产亚洲观看| 欧美超级免费视 在线| 欧美精品在线极品| 欧美亚洲一区二区在线观看| 老巨人导航500精品| 亚洲午夜久久久久久久久电影网| 亚洲免费影视| 亚洲激情视频网站| 亚洲尤物精选| 亚洲免费av电影| 久久www免费人成看片高清| 日韩视频在线观看一区二区| 亚洲欧美成人网| 亚洲伦理在线| 午夜精彩国产免费不卡不顿大片| 亚洲美女诱惑| 久久乐国产精品| 亚洲免费伊人电影在线观看av| 久久综合九色综合网站| 香蕉久久精品日日躁夜夜躁| 欧美成人亚洲| 久久野战av| 国产老女人精品毛片久久| 亚洲日本欧美| 亚洲黄色成人久久久| 性色av一区二区三区| 亚洲午夜视频在线观看| 麻豆精品视频在线观看视频| 久久精品三级| 国产情侣久久| 亚洲欧美精品一区| 亚洲免费在线视频| 欧美区国产区| 亚洲片国产一区一级在线观看| 好吊视频一区二区三区四区| 亚洲天堂av在线免费| 一区二区电影免费在线观看| 免费国产自线拍一欧美视频| 久久久久久亚洲精品中文字幕| 国产麻豆精品久久一二三| av成人免费在线| 一本色道久久精品| 欧美日韩国产在线| 亚洲最新合集| 亚洲欧美日韩另类| 国产精品九色蝌蚪自拍| 夜久久久久久| 亚洲午夜一区二区三区| 欧美日韩国产精品一区二区亚洲| 亚洲国产精品va在看黑人| 亚洲欧洲在线免费| 欧美日韩第一区日日骚| 亚洲免费观看| 亚洲欧美成人网| 国产毛片久久| 久久超碰97中文字幕| 久久久精品国产99久久精品芒果| 国产亚洲欧美一区| 久久人人爽人人爽| 亚洲国产一区二区在线| 一区二区三区 在线观看视频| 欧美日韩免费网站| 性8sex亚洲区入口| 免费在线看一区| 亚洲免费观看在线观看| 国产精品久久久久av| av成人免费| 国产视频一区二区三区在线观看| 亚洲美女中出| 欧美一区二区啪啪| 亚洲成色999久久网站| 欧美国产日韩一区二区三区| 一本色道久久88精品综合| 欧美在线视频全部完| 在线免费不卡视频| 欧美日韩在线大尺度| 小嫩嫩精品导航| 最新热久久免费视频| 亚洲欧美一区二区原创| 红桃视频亚洲| 欧美三级日本三级少妇99| 欧美一区二视频| 亚洲日本va午夜在线电影| 午夜免费久久久久| 亚洲国产日韩欧美一区二区三区| 欧美日韩在线电影| 久久精品视频在线播放| 99re66热这里只有精品3直播| 久久精品夜色噜噜亚洲a∨ | 亚洲一区二区三区在线| 久久综合图片| 性8sex亚洲区入口| 亚洲欧洲在线免费| 国产一区二区三区四区五区美女 | 国产精品日韩二区| 欧美顶级艳妇交换群宴| 亚洲图片欧洲图片av| 亚洲第一天堂av| 久久精品在线视频| 亚洲特级片在线| 亚洲人成网站色ww在线| 国产日韩一区二区三区在线播放| 欧美激情一区二区三级高清视频| 午夜亚洲精品| 亚洲色在线视频| 亚洲丰满少妇videoshd| 久久综合中文色婷婷| 香蕉久久夜色| 午夜免费电影一区在线观看| 亚洲久久成人| 亚洲人成艺术| 亚洲欧洲精品一区二区| 在线观看亚洲| 伊人久久久大香线蕉综合直播| 国产女人18毛片水18精品| 欧美另类69精品久久久久9999| 免费观看成人网| 美女在线一区二区| 久久久无码精品亚洲日韩按摩| 香蕉av777xxx色综合一区| 一区二区欧美精品| 亚洲精品偷拍| 亚洲精品一二三| 亚洲精品视频啊美女在线直播| 亚洲黄色毛片| 亚洲精品偷拍| 亚洲无线一线二线三线区别av| 亚洲精品日产精品乱码不卡| 亚洲乱码国产乱码精品精可以看| 91久久精品美女| 日韩视频在线观看免费| 99在线精品视频| 亚洲综合色网站| 久久国产一区二区三区| 久久九九热免费视频| 久久亚洲国产精品一区二区| 麻豆91精品91久久久的内涵| 久久狠狠婷婷| 亚洲一区二区欧美日韩| 亚洲影视在线| 欧美在线中文字幕| 久久婷婷久久| 欧美激情欧美激情在线五月| 亚洲国产裸拍裸体视频在线观看乱了中文 | 国产欧美日韩精品专区| 国产日韩欧美麻豆| 伊人激情综合| 亚洲日本无吗高清不卡| 亚洲一区二区在线播放| 欧美一级理论性理论a| 久久蜜桃香蕉精品一区二区三区| 免费观看成人鲁鲁鲁鲁鲁视频 | 久久久亚洲一区| 亚洲第一在线视频| 一本一道久久综合狠狠老精东影业 | 欧美激情1区2区3区| 亚洲精品欧美极品| 午夜精品久久久久久久久久久久久 | 久久手机精品视频| 欧美精品粉嫩高潮一区二区| 国产精品久久久久久av福利软件| 国产一区在线免费观看| 亚洲精品日韩综合观看成人91| 亚洲综合三区| 欧美高清在线视频| 亚洲天堂网在线观看| 米奇777在线欧美播放| 国产精品福利在线| 亚洲国产电影| 性欧美8khd高清极品| 亚洲电影免费在线| 亚洲自拍偷拍视频| 欧美成人免费在线视频| 国产日本欧洲亚洲| 一本色道久久88综合日韩精品| 久久先锋影音av| 国产精品99久久久久久人| 鲁大师成人一区二区三区| 国产精品一区二区在线观看不卡 | 亚洲国产日韩一区| 久久久久国产精品午夜一区| 一本久久综合亚洲鲁鲁五月天| 蜜桃久久精品一区二区|