1、问题
string 类对象还具备C方式字符串的灵活性吗?还能直接访问单个字符吗?
答:上一课刚写过类似的代码,肯定能访问。
2、字符串类的兼容性
- string 类最大限度的考虑了C字符串的兼容性
- 可以按照使用C字符串的方式使用string 对象
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s = "a1b2c3d4r";
int n = 0;
for (unsigned int i = 0; i < s.length(); i++)
{
if (isdigit(s[i])) //是不是数字
{
n++;
}
}
cout << n << endl;
return 0;
}
结论:在任意的C++编译器里面都是支持数组访问的方式来使用字符串对象。
出现这种情况的问题在于要把 for 语句里面的 i 改成 unsigned int 类型。
提出问题:类的对象怎么支持数组的下标访问?
如果我随机定义一个类的对象,那么这个对象可以支持下标访问吗?
#include <iostream>
#include <string>
using namespace std;
class Test
{
public:
};
int main()
{
Test t;
cout << t[0] << endl;
return 0;
}
编译出错,C++编译器不支持数组访问操作符和任意的类对象共同使用
3、重载数组访问操作符
- 被忽略的事实
— 数组访问符是C/C++ 中的内置操作符
— 数组访问符的原生意义是数组访问和指针运算
a[n] = *(a+n) = *(n+a) = n(a)
#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;
}
- 数组访问操作符([ ])
— 只能通过类的成员函数重载
— 重载函数能且只能使用一个参数
— 可以定义不同参数的多个重载函数
#include <iostream>
#include <string>
using namespace std;
class Test
{
public:
int operator[](int i)
{
return 0;
}
int operator[](const char* s)
{
return 0;
}
};
int main()
{
Test t;
cout << t[0] << endl;
cout << t["xiebs"] << endl;
return 0;
}
最终写法:
#include <iostream>
#include <string>
using namespace std;
class Test
{
private:
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 << t["1st"] << endl;
cout << t["2nd"] << endl;
cout << t["3rd"] << endl;
cout << t["4th"] << endl;
cout << t["5th"] << endl;
return 0;
}
字符串作为下标访问数组是合法的。
数组类的再优化:
IntArray.h
#ifndef _INTARRAY_H_
#define _INTARRAY_H_
#pragma once
class IntArray
{
private:
int* m_pointer;
int m_length;
IntArray(int len);
IntArray(const IntArray& obj);
bool construct();
public:
static IntArray* NewInstance(int length);
int length();
int& operator[](int i);
bool set(int index, int value);
bool get(int index, int& value);
IntArray& self();
~IntArray();
};
#endif
IntArray.cpp
#include "IntArray.h"
IntArray::IntArray(int len)
{
m_length = len;
}
IntArray::IntArray(const IntArray& obj)
{
m_length = obj.m_length;
m_pointer = new int[obj.m_length];
for (int i = 0; i < obj.m_length; i++)
{
m_pointer[i] = obj.m_pointer[i];
}
}
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;
}
int& IntArray::operator[](int i)
{
return m_pointer[i];
}
bool IntArray::set(int index, int value)
{
bool ret = (index >= 0) && (index < length());
if (ret)
{
m_pointer[index] = value;
}
return ret;
}
bool IntArray::get(int index,int& value)
{
bool ret = (index >= 0) && (index < length());
if (ret)
{
value = m_pointer[index];
}
return ret;
}
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();
for (int i = 0; i < array.length(); i++)
{
array[i] = i + 1;
}
for (int i = 0; i < array.length(); i++)
{
int value = 0;
cout << array[i] << endl;
}
}
delete a;
return 0;
}
小结:
- string 类最大程度的兼容了C字符串的用法
- 数组访问符的重载能够使得对象模拟数组的行为
- 只能通过类的成员函数重载数组访问符
- 重载函数能且仅能使用一个参数