数组操作符的重载
1、问题
string类对象还具备C方式字符串的灵活性吗?还能直接访问单个字符吗?
2、字符串类的兼容性
- string类最大限度的考虑了C字符串的兼容性
- 可以按照使用C字符串的方式使用string对象
string s = "a1b2c3d4e";
int n = 0;
for(int i = 0; i < s.length(); i++)
{
if( isdigit(s[i]) )
{
n++;
}
}
3、编程实验
#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;
}
//compile,run
$ ./a.out
4
4、问题
类的对象怎么支持数组的下标访问?
5、重载数组访问操作符
-
被忽略的事实。。。
-
数组访问符是C/C++中的内置操作符
-
数组访问符的原生意义是数组访问和指针运算
a[n]
=*(a+n)
=*(n+a)
=n[a]
-
6、示例分析
#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 << endl;
for(int i = 0; i < 5; i++)
{
i[a] = i + 10;
}
for(int i = 0; i < 5; i++)
{
cout << *(i + a) << endl;
}
return 0;
}
//compile,run
$ ./a.out
0
1
2
3
4
10
11
12
13
14
7、数组访问操作符([])
- 只能通过类的成员函数重载
- 重载函数能且仅能使用一个参数
- 可以定义不同参数的多个重载函数
//34-2.cpp
#include<iostream>
#include<string>
using namespace std;
class Test
{
private:
/* data */
int a[5];
public:
int& operator [] (int i)
{
return a[i];
}
int& operator [] (const string& s)
{
if( s == "1st")
return a[0];
if( s == "2st")
return a[1];
if( s == "3st")
return a[2];
if( s == "4st")
return a[3];
if( s == "5st")
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["5st"] << endl;
cout << t["3st"] << endl;
cout << t["3st"] << endl;
cout << t["2st"] << endl;
cout << t["1st"] << endl;
return 0;
}
//compile,run
$ ./a.out
0
1
2
3
4
4
2
2
1
0
8、完善代码
//main.cpp
#include "IntArray.h"
#include<iostream>
#include<string>
using namespace std;
int main()
{
IntArray* a = IntArray::NewInstance(5);
cout << "a.length = " << a->length() << endl;
if( a != NULL)
{
IntArray& array = a->self();
array[0] = 1;
for(int i=0; i< array.length(); i++)
{
cout << array[i] << endl;
}
}
delete a;
return 0;
}
//IntArray.cpp
#include "IntArray.h"
#include<iostream>
#include<string>
using namespace std;
IntArray::IntArray(int len)
{
m_legth = len;
}
#if 0
//ʵ�����
IntArray::IntArray(const IntArray& obj)
{
m_legth = obj.m_legth;
m_pointer = new int[obj.m_legth];
for(int i = 0; i < obj.m_legth; i++)
{
m_pointer[i] = obj.m_pointer[i];
}
}
#endif
bool IntArray::construct()
{
bool ret = true;
m_pointer = new int[m_legth];
if(m_pointer)
{
for(int i = 0; i < m_legth; 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_legth;
}
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;
}
ret;
}
int& IntArray::operator [] (int index)
{
return m_pointer[index];
}
IntArray& IntArray::self()
{
return *this;
}
IntArray::~IntArray()
{
delete[] m_pointer;
}
//IntArray.h
#ifndef _INTARRAY_H_
#define _INTARRAY_H_
class IntArray
{
private:
int m_legth;
int* m_pointer;
IntArray(int len);
IntArray(const IntArray& obj);
bool construct();
public:
int length();
bool get(int index, int& value);
bool set(int index, int value);
static IntArray* NewInstance(int length);
//void free();
int& operator [] (int index);
IntArray& self();
~IntArray();
};
#endif// _INTARRAY_H_
9、小结
- string类最大程度的兼容了C字符串的用法
- 数组访问符的重载能够使得对象模拟数组的行为
- 只能通过类的成员函数重载数组访问符
- 重载函数能且仅能使用一个参数