我们在学习了 string 类对象后,就不禁头脑中冒出了一个问题:string 类对象还具备 C 方式字符串的灵活性吗?还能直接访问单个字符吗?那么 string 类最大限度的考虑了 C 字符串的兼容性,可以按照使用 C 字符串的方式使用 string 对象。
下来我们用 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;
}
我们这个程序是用来统计字符串中的数字的个数的,看看编译结果
再用 BCC 编译试试
同样也是 4,再来用 VS 编译器试试
VS 同样也是 4,证明这就不是偶然了。那么问题来了哈,类的对象怎么支持数组的下标访问呢?这时我们就得需要重载数组访问操作符了。数组访问符是 C/C++ 中的内置操作符,数组访问符的原生意义是数组访问和指针运算。
下来我们就复习下之前在 C 语言中学习的指针和数组
#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;
}
我们看看编译结果
已经实现了数组的访问。那么下来说说关于数组访问操作符([])的几个知识点:a> 只能通过类的成员函数重载;b> 重载函数能且仅能使用一个参数;c> 可以定义不同参数的多个重载函数。
下来我们通过实例代码来看看重载数组访问操作符是怎样实现的
#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;
}
我们通过重载数组操作符,可以在 C++ 中对类对象也可以使用数组那样的方式进行访问。我们还可以直接通过 t["1st"] 这种方式对数组进行访问。看看编译结果
下来我们利用数组操作符的重载来完善下我们之前实现的数组类
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;
}
我们在 IntArray.h 头文件中添加了两个函数,一个是数组操作符的重载,另一个则是返回类的引用。返回类的引用则是方便我们在 test.cpp 中可以直接以数组的方式来进行访问,看看编译结果
通过对数组操作符重载的学习,总结如下:1、string 类最大程度的兼容了 C 字符串的用法;2、数组访问符的重载能够使得对象模拟数组的行为;3、只能通过类的成员函数重载数组访问符;4、重载函数能且仅能使用一个参数。
欢迎大家一起来学习 C++ 语言,可以加我QQ:243343083。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。