问题:string类对象还具备C方式字符串的灵活性吗?还能直接访问单个字符吗?

1.字符串类的兼容性

—-string类最大限度的考虑与c字符串的兼容性
—-可以按照使用c字符串的方式使用string对象

以下代码可用?可以

  1. string s = "a1b2c3d4e"
  2. int n = 0;
  3. for(int i=0;i<s.length();i++)
  4. {
  5. if(isdigit(s[i]))
  6. {
  7. n++;
  8. }
  9. }

问题:类的对象怎么支持数组的下标访问操作符?
重载

2.重载数组访问操作符

被忽视的事实。。。
—-数组访问操作符是c/c++的内置操作符
—-数组访问操作符的原生意义是数组访问和指针运算

等价方式:
a[n] (a+n) (n+a) n[a]

数组访问操作符([ ])
—-只能通过类的成员函数重载
—-重载函数能且仅能使用一个参数
—-可以定义不同参数的多个重载函数

  1. #include<iostream>
  2. #include<string>
  3. #include<sstream>
  4. using namespace std;
  5. class Test
  6. {
  7. public:
  8. int operator [](int i)
  9. {
  10. return 0;
  11. }
  12. char operator [](const char* s)
  13. {
  14. return 0;
  15. }
  16. };
  17. int main()
  18. {
  19. Test t;
  20. cout << t[1] << endl;
  21. return 0;
  22. }

字符串类和数组访问操作符可以结合起来,通过重载技术实现

  1. #include<iostream>
  2. #include<string>
  3. #include<sstream>
  4. using namespace std;
  5. class Test
  6. {
  7. private:
  8. int a[5];
  9. public:
  10. int& operator [](int i)
  11. {
  12. return a[i]; //函数调用的返回值不能作为左值,所以返回引用
  13. }
  14. int& operator [](const string& s)
  15. {
  16. if(s == "1st")
  17. {
  18. return a[0];
  19. }
  20. else if(s == "2nd")
  21. {
  22. return a[1];
  23. }
  24. else if(s == "3rd")
  25. {
  26. return a[2];
  27. }
  28. else if(s == "4th")
  29. {
  30. return a[3];
  31. }
  32. else if(s == "5th")
  33. {
  34. return a[4];
  35. }
  36. return a[0];
  37. }
  38. int length()
  39. {
  40. return 5;
  41. }
  42. };
  43. int main()
  44. {
  45. Test t;
  46. for(int i=0;i<t.length();i++)
  47. {
  48. t[i] = i;
  49. }
  50. for(int i=0;i<t.length();i++)
  51. {
  52. cout << t[i] << endl;
  53. }
  54. cout << t["5th"] << endl;
  55. cout << t["4th"] << endl;
  56. cout << t["3rd"] << endl;
  57. cout << t["2nd"] << endl;
  58. cout << t["1st"] << endl;
  59. return 0;
  60. }

更完善的数组类

  1. //IntArray.cpp
  2. #include "IntArray.h"
  3. #include<iostream>
  4. #include<string>
  5. #include<sstream>
  6. IntArray::IntArray(int len)
  7. {
  8. m_length = len;
  9. }
  10. /*IntArray::IntArray(const IntArray& obj)
  11. {
  12. m_length = obj.m_length; //进行的是深拷贝
  13. m_pointer = new int[obj.m_length]; //重新申请一个对空间
  14. for(int i=0; i<obj.m_length; i++) //以对空间中的地址来赋值
  15. {
  16. m_pointer[i] = obj.m_pointer[i];
  17. }
  18. }*/
  19. bool IntArray::construct()
  20. {
  21. bool ret = true;
  22. m_pointer = new int[m_length]; //maybe fail
  23. if(m_pointer)
  24. {
  25. for(int i=0; i<m_length; i++)
  26. {
  27. m_pointer[i] = 0;
  28. }
  29. }
  30. else
  31. {
  32. ret = false;
  33. }
  34. return ret;
  35. }
  36. int IntArray::length()
  37. {
  38. return m_length;
  39. }
  40. bool IntArray::get(int index, int& value)
  41. {
  42. bool ret = (0 <= index) && (index < length());
  43. if( ret )
  44. {
  45. value = m_pointer[index];
  46. }
  47. return ret;
  48. }
  49. bool IntArray::set(int index, int value)
  50. {
  51. bool ret = (0 <= index) && (index < length());
  52. if( ret )
  53. {
  54. m_pointer[index] = value;
  55. }
  56. return ret;
  57. }
  58. IntArray* IntArray::NewInstance(int length)
  59. {
  60. IntArray* ret = new IntArray(length); //因为在静态成员函数的内部,访问私有成员是没有问题的
  61. //若第二阶段构造失败,返回Null
  62. if(!(ret&&ret->construct()))
  63. {
  64. delete ret;
  65. ret = NULL;
  66. }
  67. return ret;
  68. }
  69. int& IntArray::operator [](int index)
  70. {
  71. return m_pointer[index]; //函数调用的返回值不能作为左值,所以返回引用
  72. }
  73. IntArray& IntArray::self()
  74. {
  75. return *this;
  76. }
  77. IntArray::~IntArray()
  78. {
  79. delete[]m_pointer;
  80. }
  81. ////IntArray.h
  82. #ifndef _INTARRAY_H_
  83. #define _INTARRAY_H_
  84. class IntArray
  85. {
  86. private:
  87. int m_length;
  88. int* m_pointer;
  89. IntArray(int len);
  90. //IntArray(const IntArray& obj);
  91. bool construct(); //step 2 construct func
  92. public:
  93. int length();
  94. bool get(int index, int& value);
  95. bool set(int index ,int value);
  96. ~IntArray();
  97. static IntArray* NewInstance(int length);
  98. int& operator [](int index);
  99. IntArray& self(); //to void use of pointer
  100. };
  101. #endif
  102. //main.cpp
  103. #include<iostream>
  104. #include<string>
  105. #include<sstream>
  106. #include "IntArray.h"
  107. using namespace std;
  108. int main()
  109. {
  110. IntArray* a = IntArray::NewInstance(5);
  111. if(a != NULL)
  112. {
  113. IntArray& array = a->self();
  114. cout << "array.length() = " << array.length() << endl;
  115. array[0] = 1;
  116. for(int i=0; i<a->length(); i++)
  117. {
  118. cout << array[i] << endl;
  119. }
  120. }
  121. delete a;
  122. return 0;
  123. }

总结:

1.string类最大程度的兼容了c字符串的用法
2.数组访问符的重载能够使得对象模拟数组的行为
3.只能通过类的成员函数重载数组的访问符
4.重载函数能且仅能使用一个参数