锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
1. To C++ programmer, for example, a pointer to a pointer looks a little funny. Why, we wonder, wasn鈥檛 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.
3. The Item might better be called 鈥減refer 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.
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.
10. The problem with malloc and free(and their variants) is simple : they don鈥檛 know about constructors and destructors.
11. free 鎿嶄綔涓嶄細璋冪敤鏋愭瀯鍑芥暟錛屽鏋滄寚閽堟墍鎸囧璞℃湰韜張鍒嗛厤浜嗗唴瀛橈紝鍒欎細閫犳垚鍐呭瓨涓㈠け銆?/span>
12. Memory management concerns in C++ fall into two general camps: getting it right and making it perform efficiently.
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.
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鈥檚 auto_ptr.
璁╂垜浠洖榪囧ご鍘葷湅鐪嬭繖鏍蜂竴涓熀鏈棶棰橈細涓轟粈涔堟湁蹇呰鍐欒嚜宸辯殑 operator new 鍜?/span> operator delete 錛熺瓟妗堥氬父鏄細涓轟簡鏁堢巼銆傜己鐪佺殑 operator new 鍜?/span> operator delete 鍏鋒湁闈炲父濂界殑閫氱敤鎬э紝瀹冪殑榪欑鐏墊椿鎬т篃浣垮緱鍦ㄦ煇浜涚壒瀹氱殑鍦哄悎涓嬶紝鍙互榪涗竴姝ユ敼鍠勫畠鐨勬ц兘銆傚挨鍏跺湪閭d簺闇瑕佸姩鎬佸垎閰嶅ぇ閲忕殑浣嗗緢灝忕殑瀵硅薄鐨勫簲鐢ㄧ▼搴忛噷錛屾儏鍐墊洿鏄姝ゃ?/span>
聽int* pi = new int(0);
聽
聽聽聽聽
if
聽(聽pi聽!=聽0聽)
聽聽聽聽聽聽聽聽delete聽pi;
聽
璇存槑錛氬鏋滄寚閽堟搷浣滄暟琚緗負0錛屽垯C++淇濊瘉delete琛ㄨ揪寮忎笉浼氳皟鐢ㄦ搷浣滅delete()銆傛墍浠ユ病鏈夊繀瑕佹祴璇曞叾鏄惁涓?span lang="EN-US">0銆?span lang="EN-US">
聽鍦?span lang="EN-US">delete琛ㄨ揪寮忎箣鍚庯紝pi琚О浣滅┖鎮寚閽堬紝鍗蟲寚鍚戞棤鏁堝唴瀛樼殑鎸囬拡銆傜┖鎮寚閽堟槸紼嬪簭閿欒鐨勬牴婧愶紝寤鴻瀵硅薄閲婃斁鍚庯紝灝嗚鎸囬拡璁劇疆涓?span lang="EN-US">0銆?span lang="EN-US">
聽
聽2錛?span lang="EN-US">auto_ptr
聽auto_ptr鏄?span lang="EN-US">C++鏍囧噯搴撴彁渚涚殑綾繪ā鏉匡紝瀹冨彲浠ュ府鍔╃▼搴忓憳鑷姩綆$悊鐢?span lang="EN-US">new琛ㄨ揪寮忓姩鎬佸垎閰嶇殑鍗曚釜瀵硅薄錛屼絾鏄紝瀹冩病鏈夊鏁扮粍綆$悊鎻愪緵綾諱技鏀寔銆傚畠鐨勫ご鏂囦歡涓猴細
聽
褰?span lang="EN-US">auto_ptr瀵硅薄鐨勭敓鍛芥湡緇撴潫鏃訛紝鍔ㄦ佸垎閰嶇殑瀵硅薄琚嚜鍔ㄩ噴鏀俱?span lang="EN-US">
聽auto_ptr綾繪ā鏉胯儗鍚庣殑涓昏鍔ㄦ満鏄敮鎸佷笌鏅氭寚閽堢被鍨嬬浉鍚岀殑璇硶錛屼絾鏄負auto_ptr瀵硅薄鎵鎸囧璞$殑閲婃斁鎻愪緵鑷姩綆$悊銆備緥錛?span lang="EN-US">
聽auto_ptr
綾繪ā鏉挎敮鎸佹墍鏈夋潈姒傚康錛屽綋涓涓?span lang="EN-US">auto_ptr瀵硅薄琚敤鍙︿竴涓?span lang="EN-US">auto_ptr瀵硅薄鍒濆鍖栬祴鍊兼椂錛屽乏杈硅璧嬪兼垨鍒濆鍖栫殑瀵硅薄灝辨嫢鏈変簡絀洪棽瀛樺偍鍖哄唴搴曞眰瀵硅薄鐨勬墍鏈夋潈錛岃屽彸杈圭殑auto_ptr瀵硅薄鍒?font color="#ff0000">鎾ゆ秷鎵鏈夎矗浠?/font>銆備緥錛?span lang="EN-US">
聽
鍒ゆ柇鏄惁鎸囧悜涓涓璞★紝渚嬶細
聽3錛夋暟緇勭殑鍔ㄦ佸垎閰嶄笌閲婃斁
聽寤鴻浣跨敤C++鏍囧噯搴?span lang="EN-US">string,閬垮厤浣跨敤C椋庢牸瀛楃涓叉暟緇勩?span lang="EN-US">
聽涓洪伩鍏嶅姩鎬佸垎閰嶆暟緇勭殑鍐呭瓨綆$悊甯︽潵鐨勯棶棰橈紝涓鑸緩璁嬌鐢ㄦ爣鍑嗗簱vector銆?span lang="EN-US">list鎴?span lang="EN-US">string瀹瑰櫒綾誨瀷銆?span lang="EN-US">
聽
聽4錛夊父閲忓璞$殑鍔ㄦ佸垎閰嶄笌閲婃斁
聽鍙互浣跨敤new琛ㄨ揪寮忓湪絀洪棽瀛樺偍鍖哄唴鍒涘緩涓涓?span lang="EN-US">const瀵硅薄錛屼緥錛?span lang="EN-US">
聽
鎴戜滑涓嶈兘鍦ㄧ┖闂插瓨鍌ㄥ尯鍒涘緩鍐呯疆綾誨瀷鍏冪礌鐨?span lang="EN-US">const鏁扮粍錛屽師鍥犳槸錛氭垜浠笉鑳藉垵濮嬪寲鐢?span lang="EN-US">new琛ㄨ揪寮忓垱寤虹殑鍐呯疆綾誨瀷鏁扮粍鐨勫厓绱犮備緥錛?span lang="EN-US">
聽5
錛夊畾浣?span lang="EN-US">new琛ㄨ揪寮?span lang="EN-US">
聽new琛ㄨ揪寮忕殑絎笁縐嶅艦寮忓厑璁哥▼搴忓憳瑕佹眰灝嗗璞″垱寤哄湪宸茬粡琚垎閰嶅ソ鐨勫唴瀛樹腑銆傜О涓猴細瀹氫綅new琛ㄨ揪寮忥紙placement new expression)銆傜▼搴忓憳鍦?span lang="EN-US">new琛ㄨ揪寮忎腑鎸囧畾寰呭垱寤哄璞℃墍鍦ㄧ殑鍐呭瓨鍦板潃銆傚涓嬫墍紺猴細
聽new 錛?span lang="EN-US">place_address) type-specifier
聽娉ㄦ剰錛?span lang="EN-US">place_address蹇呴』鏄釜鎸囬拡錛屽繀欏誨寘鍚ご鏂囦歡<new>銆傝繖欏硅鏂藉厑璁哥▼搴忓憳棰勫垎閰嶅ぇ閲忕殑鍐呭瓨錛屼緵浠ュ悗閫氳繃榪欑褰㈠紡鐨?span lang="EN-US">new琛ㄨ揪寮忓垱寤哄璞°備緥濡傦細
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.
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.
闂錛?br />聽聽聽
聽聽聽鏈夊涓嬩袱涓唬鐮佹枃浠訛細
聽聽聽// 20060816_operator.cxx
// 20060816_operator.h
鐢ㄤ笂闈袱涓枃浠跺緩绔嬪伐紼嬶紝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)
瑙e喅:
聽聽聽
1錛?span style="FONT: 7pt 'Times New Roman'">聽
璋冩暣鍚嶅瓧絀洪棿澹版槑鍜岃嚜瀹氫箟澶存枃浠剁殑嬈″簭錛屽涓嬶細2錛?span style="FONT: 7pt 'Times New Roman'">聽 鎶婄被瀹氫箟浜庢爣鍑嗗悕瀛椾箣鍐咃紝20060816_operator.h鏂囦歡淇敼濡備笅錛堜笉鎺ㄨ崘錛?br />
3錛?span style="FONT: 7pt 'Times New Roman'">聽 浣跨敤澶存枃浠?/span>iostream.h浠f浛iostream,姣旇緝钀藉悗鐨勬柟寮忥紙涓嶆帹鑽?/span>!錛?br />
4錛?span style="FONT: 7pt 'Times New Roman'">聽 浠諱綍鏃跺欓兘涓嶄嬌鐢?/span>using namespace 澹版槑錛佽繖鏄彧闇瑕佷慨鏀?/span>20060816_operator.cxx鏂囦歡濡備笅錛?br />
浣撲細錛?br />聽聽聽鍚嶅瓧絀洪棿鏈潵灝辨槸涓轟簡瑙e喅鍚嶅瓧鍐茬獊闂鑰屽紩鍏ワ紝浠ヤ嬌鏍囧噯搴撲笉鍙楀鐣屽獎鍝嶃傚湪榪欎釜鎰忎箟涓婃潵璇達紝using namespace std;涓嶆槸鍙互闅忔剰浣跨敤鐨勮鍙ワ紝搴旇鑰冭檻鍒板畠鐨勪綅緗紼嬪簭鐨勫彲鑳藉獎鍝嶏紝鑰屾渶褰誨簳鐨勬潨緇濋敊璇殑瑙e喅鍔炴硶鏄湪浠諱綍鏃跺欎換浣曟儏鍐典笅閮戒笉浣跨敤姝よ鍙ワ紒
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
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
#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.
#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.
#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; }
#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.
#include <vector> void clear();
The function clear() deletes all of the elements in the vector. clear() runs in linear time.
#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(); }
#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.
#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
#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.
#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:
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
#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.
#include <vector> void pop_back();
The pop_back() function removes the last element of the vector.
pop_back() runs in constant time.
#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.
#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.
#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.
#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.
#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.
#include <vector> size_type size() const;
The size() function returns the number of elements in the current vector.
#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
#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:
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; }
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:
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:
Summary of Vector Benefits聽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聽}
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.
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.
聽聽 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.
If we want to access all the data, we can use a for loop and display them as follows.
The std:: vector .size() function returns the number of elements stored in the vector.
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.
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.
聽The following sample demonstrates how to use the erase() function for removing elements 2,3 in the str_Vector used in the above sample.聽
If one wants to remove all the elements at once from the c++ vector, the vector.clear() function can be used.
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.1std::vector<int>聽myIntVector;
2std::vector<int>::iterator聽myIntVectorIterator;
The STL approach (use this)聽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聽}
As you might imagine, you can use the decrement operator, --, when working with a bidirectional iterator or a backward operator.聽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聽
iterator + nwhere 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.
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.1聽vector<int>聽myIntVector;
2聽vector<int>::iterator聽myIntVectorIterator;
3聽myIntVectorIterator聽=聽myIntVector.begin()聽+聽2;
which would delete all elements in the vector. If you only wanted to delete the first two elements, you could use1聽vector<int>::iterator聽myIntVectorIterator;
2聽myIntVector.erase(myIntVectorIterator.begin(),聽myIntVectorIterator.end());
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.1myIntVector.erase(myIntVectorIterator.begin(),聽myIntVectorIterator.begin()+2);
class_name<parameters>::iterator
聽聽聽聽聽聽
闂宸茬粡瑙e喅錛屽笘瀛愭憳瑕佸涓嬶細
涓?span lang="EN-US">聽聽聽聽聽聽棰?span lang="EN-US">: 鍑芥暟妯$増闂錛屼負浠涔堜笉鑳介噰鐢ㄥ鏂囦歡鏂瑰紡銆?span lang="EN-US">聽 |
|
|
鍥炲浜?span lang="EN-US">: newgun (
涔︾)聽 |
2006-8-12 19:34:29 (
寰楀垎:
10
)聽 |
Re:
鍑芥暟妯$増闂錛屼負浠涔堜笉鑳介噰鐢ㄥ鏂囦歡鏂瑰紡銆?span lang="EN-US">
鍦?span lang="EN-US">c++
聽聽聽聽聽聽 鎰熷彈錛氬緱鍒扮綉鍙嬫彁紺猴紝鐪熸湁涓縐嶁滄煶鏆楄姳鏄庡張涓鏉戔濅箣鎰燂紝榪欎釜鎵璋?/span> bug 鏄繖涔堢畝鍗曞張鏄繖涔堜笉綆鍗曪紒
聽聽聽聽聽聽 涓鐩磋涓哄簲璇ユ槸鑷繁鐨勯敊璇紝浣嗘病鎯沖埌鏄紪璇戝櫒鍜屾爣鍑嗗吋瀹規х殑闂銆傜湅鏉ヨ嚜宸辯殑鎬濈淮涔犳儻搴旇鏈夋墍鏀瑰彉浜嗐傚簲璇ュ瑙掑害鍒嗘瀽闂錛岃屼笉鑳戒竴鍛崇殑鎯沖綋鐒躲?/span>
聽聽聽聽聽聽 Ps 錛氳嚜宸變篃搴旇鑰冭檻灝濊瘯涓涓柊鐨?/span> C++ 緙栬瘧鍣ㄤ簡錛岃佺敤 vc 錛屾劅瑙夊鐨勬爣鍑?/span> C++ 閮戒笉綰紝灝辮薄榪欐浜嬩歡涓鏍楓?/span>
聽聽聽聽聽聽 鍠滐紝鐩告瘮鍏ュ涔嬪垵錛岀煡璇嗗眰铏芥棤鑴辮儙鎹㈤涔嬫劅錛屼絾涔熷皬鏈夋墍瀛︼紝姣斿凡姣斾漢錛岃繘姝ヤ笉灝戯紒
聽聽聽聽聽聽 蹇э紝涓ゅ勾灝忕錛屾椂闂寸揣鍑戯紝浜庡鏈爺絀墮毦鏈夊ぇ鎴愶紱鍐嶇幇瀹炰竴鐐癸紝铏藉閫氫俊錛屼絾鎵句竴婊℃剰宸ヤ綔錛屼粛鍘嬪姏涓嶈交錛屼換閲嶉亾榪滐紒
聽聽聽聽聽聽 璇葷爺涓嶄技鏈錛屾垚鐔熷浜嗭紝涔熷紑濮嬭冭檻鐜板疄錛屼笉鏁㈣櫄嫻害鏃ワ紝浣嗙湡鍔姏濂嬫枟璧鋒潵錛屼粛鏈夎糠鑼笉鐭ユ墍涓轟箣鎯戙?/span>
聽聽聽聽聽聽 涓嶈繃錛岃繕濂斤紝榪樻湁涓棰楄櫧絎ㄦ嫏浣嗚繕鍠勪簬鍙嶆濈殑鑴戣錛岃櫧寮礬涓嶅皯錛屼絾涔熺畻涓姝ユ鐨勫悜鍓嶈浜嗐?/span>
聽聽聽聽聽聽 涓ゅ勾錛岀湡鐨勫お鐭紒浣曞喌宸叉氮璐逛竴騫村厜闃村搲 ?!
聽聽聽聽聽聽 鏃犺涓轟簡宸ヤ綔錛屼負浜嗘瘯涓氾紝榪樻槸涓轟簡鑷繁鍋氱偣鎴愭灉鏉ワ紝閮芥槸璇ュ姫鍔涚殑鏃跺欎簡錛?/span>
聽聽聽聽聽聽 涓嶈皥浜虹敓鎰忎箟絳夌瓑澶ч亾鐞嗭紝灝辨槸涓轟簡榪芥眰涓縐嶇敓瀛樼殑鍩虹錛屽湪涓鍙剼宸茬粡韙忓叆紺句細鐨勬椂鍊欙紝涔熻鏄庣櫧鑷繁搴旇鍋氫簺浠涔堜簡錛?/span>
聽聽聽聽聽聽 綆$悊鑷繁錛屽仛璇ュ仛鐨勪簨錛屽仛涓涓Н鏋佺殑鑷繁錛佸畬鍠勮嚜鎴戯紒鍔姏錛?img height="20" src="http://www.shnenglu.com/Emoticons/QQ/45.gif" width="20" border="0" />聽
聽聽聽聽聽聽 Ps 錛氳皚浠ユ鏂囷紝鍋氬紑鍗氱邯蹇碉紝涔熶負鎴戞剰涔変笂鐨勭爺浜屽紑濮嬬殑綰康銆?/span>
聽