C++超詳細(xì)講解數(shù)組操作符的重載
一、字符串類(lèi)的兼容性
問(wèn)題:string 類(lèi)對(duì)象還具備 C 方式字符串的靈活性嗎?還能直接訪(fǎng)問(wèn)單個(gè)字符嗎?
- string 類(lèi)最大限度的考慮了 C 字符串的兼容性
- 可以按照使用 C 字符串的方式使用 string 對(duì)象
下面看一個(gè)用 C 方式使用 string 類(lèi)的示例:
#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; }
輸出結(jié)果如下:
二、重載數(shù)組訪(fǎng)問(wèn)操作符
問(wèn)題:類(lèi)的對(duì)象怎么支持?jǐn)?shù)組的下標(biāo)訪(fǎng)問(wèn)?
被忽略的事實(shí)
- 數(shù)組訪(fǎng)問(wèn)符是 C/C++ 中的內(nèi)置操作符
- 數(shù)組訪(fǎng)問(wèn)符的原生意義是數(shù)組訪(fǎng)問(wèn)和指針運(yùn)算
下面進(jìn)行指針與數(shù)組的復(fù)習(xí):
#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; //cout << a[i] <<endl; } for (int i = 0; i < 5; i++) { cout << *(i + a) << endl; } return 0; }
輸出結(jié)果如下:
數(shù)組訪(fǎng)問(wèn)操作符([ ])
- 只能通過(guò)類(lèi)的成員函數(shù)重載重載
- 函數(shù)能且僅能使用一個(gè)參數(shù)
- 可以定義不同參數(shù)的多個(gè)重載函數(shù)
#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]; } } 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; }
輸出結(jié)果如下:
這個(gè)示例說(shuō)明可以將字符串作為下標(biāo)訪(fǎng)問(wèn)數(shù)組。
所以之前寫(xiě)的數(shù)組類(lèi)的代碼可以進(jìn)一步完善啦:
IntArray.h:
#ifndef _INTARRAY_H_ #define _INTARRAY_H_ class IntArray { private: int m_length; int* m_pointer; IntArray(int len); IntArray(const IntArray& obj); 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; }
main.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; }
輸出結(jié)果如下:
三、小結(jié)
- string 類(lèi)最大程度的兼容了 C 字符串的用法
- 數(shù)組訪(fǎng)問(wèn)符的重載能夠使得對(duì)象模擬數(shù)組的行為
- 只能通過(guò)類(lèi)的成員函數(shù)重載數(shù)組訪(fǎng)問(wèn)符
- 重載函數(shù)能且僅能使用一個(gè)參數(shù)
到此這篇關(guān)于C++超詳細(xì)講解數(shù)組操作符的重載的文章就介紹到這了,更多相關(guān)C++數(shù)組操作符重載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++標(biāo)準(zhǔn)模板庫(kù)STL的介紹
今天小編就為大家分享一篇關(guān)于C++標(biāo)準(zhǔn)模板庫(kù)STL的介紹,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12隊(duì)列的動(dòng)態(tài)鏈?zhǔn)酱鎯?chǔ)實(shí)現(xiàn)代碼分享
DynaLnkQueue.cpp - 動(dòng)態(tài)鏈?zhǔn)疥?duì)列,即隊(duì)列的動(dòng)態(tài)鏈?zhǔn)酱鎯?chǔ)實(shí)現(xiàn)2014-02-02C語(yǔ)言實(shí)現(xiàn)猜數(shù)字游戲
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)猜數(shù)字游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)不掛科指南之隊(duì)列詳解
這篇博客主要介紹一下隊(duì)列的概念,并且采用 C 語(yǔ)言,編寫(xiě)兩種存儲(chǔ)實(shí)現(xiàn)方式:順序存儲(chǔ)和鏈?zhǔn)酱鎯?chǔ),當(dāng)然還有常規(guī)的隊(duì)列基本操作的實(shí)現(xiàn)算法2022-09-09C/C++通過(guò)SQLite SDK實(shí)現(xiàn)數(shù)據(jù)庫(kù)增刪改查操作
SQLite,作為一款嵌入式關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng),一直以其輕量級(jí)、零配置以及跨平臺(tái)等特性而備受青睞,本文主要介紹了C++如何通過(guò)SQLite SDK實(shí)現(xiàn)數(shù)據(jù)庫(kù)增刪改查操作,感興趣的可以了解下2023-11-11