我們?cè)趯W(xué)習(xí)了 string 類對(duì)象后,就不禁頭腦中冒出了一個(gè)問題:string 類對(duì)象還具備 C 方式字符串的靈活性嗎?還能直接訪問單個(gè)字符嗎?那么 string 類最大限度的考慮了 C 字符串的兼容性,可以按照使用 C 字符串的方式使用 string 對(duì)象。
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡(jiǎn)單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:主機(jī)域名、虛擬空間、營(yíng)銷軟件、網(wǎng)站建設(shè)、巴州網(wǎng)站維護(hù)、網(wǎng)站推廣。
下來我們用 C 方式使用 string 類,看看示例代碼
#include <iostream> #include <string> using namespace std; int main() { string s = "a1b2c3d4e"; int n = 0; for(int i=0; i<s.length(); i++) { if( isdigit(s[i]) ) { n++; } } cout << n << endl; return 0; }
我們這個(gè)程序是用來統(tǒng)計(jì)字符串中的數(shù)字的個(gè)數(shù)的,看看編譯結(jié)果
再用 BCC 編譯試試
同樣也是 4,再來用 VS 編譯器試試
VS 同樣也是 4,證明這就不是偶然了。那么問題來了哈,類的對(duì)象怎么支持?jǐn)?shù)組的下標(biāo)訪問呢?這時(shí)我們就得需要重載數(shù)組訪問操作符了。數(shù)組訪問符是 C/C++ 中的內(nèi)置操作符,數(shù)組訪問符的原生意義是數(shù)組訪問和指針運(yùn)算。
下來我們就復(fù)習(xí)下之前在 C 語言中學(xué)習(xí)的指針和數(shù)組
#include <iostream> #include <string> using namespace std; int main() { int a[5] = {0}; for(int i=0; i<5; i++) { a[i] = i; } for(int i=0; i<5; i++) { cout << *(a + i) << endl; // cout << a[i] << endl; } cout << endl; for(int i=0; i<5; i++) { i[a] = i + 10; } for(int i=0; i<5; i++) { cout << *(i + a) << endl; // cout << a[i] << endl; } return 0; }
我們看看編譯結(jié)果
已經(jīng)實(shí)現(xiàn)了數(shù)組的訪問。那么下來說說關(guān)于數(shù)組訪問操作符([])的幾個(gè)知識(shí)點(diǎn):a> 只能通過類的成員函數(shù)重載;b> 重載函數(shù)能且僅能使用一個(gè)參數(shù);c> 可以定義不同參數(shù)的多個(gè)重載函數(shù)。
下來我們通過實(shí)例代碼來看看重載數(shù)組訪問操作符是怎樣實(shí)現(xiàn)的
#include <iostream> #include <string> using namespace std; class Test { int a[5]; public: int& operator [] (int i) { return a[i]; } int& operator [] (const string& s) { if( s == "1st" ) { return a[0]; } else if( s == "2nd" ) { return a[1]; } else if( s == "3rd" ) { return a[2]; } else if( s == "4th" ) { return a[3]; } else if( s == "5th" ) { return a[4]; } return a[0]; } int length() { return 5; } }; int main() { Test t; for(int i=0; i<t.length(); i++) { t[i] = i; } for(int i=0; i<t.length(); i++) { cout << t[i] << endl; } cout << endl; cout << t["5th"] << endl; cout << t["4th"] << endl; cout << t["3rd"] << endl; cout << t["2nd"] << endl; cout << t["1st"] << endl; return 0; }
我們通過重載數(shù)組操作符,可以在 C++ 中對(duì)類對(duì)象也可以使用數(shù)組那樣的方式進(jìn)行訪問。我們還可以直接通過 t["1st"] 這種方式對(duì)數(shù)組進(jìn)行訪問??纯淳幾g結(jié)果
下來我們利用數(shù)組操作符的重載來完善下我們之前實(shí)現(xiàn)的數(shù)組類
IntArry.h 源碼
#ifndef _INTARRAY_H_ #define _INTARRAY_H_ class IntArray { private: int m_length; int* m_pointer; IntArray(int len); bool construct(); public: static IntArray* NewInstance(int length); int length(); bool get(int index, int& value); bool set(int index, int value); int& operator [] (int index); IntArray& self(); ~IntArray(); }; #endif
IntArray.cpp 源碼
#include "IntArray.h" IntArray::IntArray(int len) { m_length = len; } bool IntArray::construct() { bool ret = true; m_pointer = new int[m_length]; if( m_pointer ) { for(int i=0; i<m_length; i++) { m_pointer[i] = 0; } } else { ret = false; } return ret; } IntArray* IntArray::NewInstance(int length) { IntArray* ret = new IntArray(length); if( !(ret && ret->construct()) ) { delete ret; ret = 0; } return ret; } int IntArray::length() { return m_length; } bool IntArray::get(int index, int& value) { bool ret = (0 <= index) && (index <= length()); if( ret ) { value = m_pointer[index]; } return ret; } bool IntArray::set(int index, int value) { bool ret = (0 <= index) && (index <= length()); if( ret ) { m_pointer[index] = value; } return ret; } int& IntArray::operator [] (int index) { return m_pointer[index]; } IntArray& IntArray::self() { return *this; } IntArray::~IntArray() { delete[] m_pointer; }
test.cpp 源碼
#include <iostream> #include <string> #include "IntArray.h" using namespace std; int main() { IntArray* a = IntArray::NewInstance(5); if( a != NULL ) { IntArray& array = a->self(); cout << "array.length() = " << array.length() << endl; array[0] = 1; for(int i=0; i<array.length(); i++) { cout << array[i] << endl; } } delete a; return 0; }
我們?cè)?IntArray.h 頭文件中添加了兩個(gè)函數(shù),一個(gè)是數(shù)組操作符的重載,另一個(gè)則是返回類的引用。返回類的引用則是方便我們?cè)?test.cpp 中可以直接以數(shù)組的方式來進(jìn)行訪問,看看編譯結(jié)果
通過對(duì)數(shù)組操作符重載的學(xué)習(xí),總結(jié)如下:1、string 類最大程度的兼容了 C 字符串的用法;2、數(shù)組訪問符的重載能夠使得對(duì)象模擬數(shù)組的行為;3、只能通過類的成員函數(shù)重載數(shù)組訪問符;4、重載函數(shù)能且僅能使用一個(gè)參數(shù)。
歡迎大家一起來學(xué)習(xí) C++ 語言,可以加我QQ:243343083。
本文標(biāo)題:數(shù)組操作符的重載(二十八)
網(wǎng)址分享:http://chinadenli.net/article44/ppigee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營(yíng)銷推廣、定制開發(fā)、搜索引擎優(yōu)化、企業(yè)網(wǎng)站制作、App開發(fā)、營(yíng)銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)