我正在编写一些代码,只是想完善一些编程抽象的想法。这是我所在的地方,我要问的是:
在 c++ 中,我们有数据类型std::string
.此数据类型基本上是具有成员函数和变量的高级指针。
当你使用这样的字符串数组时:std::string test[5];
技术上,字符串会被标记为矩阵?我想知道的原因是,使用std::string
您可以使用以下命令指定常规字符串的各个字符:
std::string hello = "Hello"; std::cout<<hello[3];
输出将是:
l
当您声明正常std::string
时,此输出有效。有了这个;当您有:
std::string hello2[2] = {"hello", "world"}; std::cout<<hello2[0][2]<<hello2[1][3];
其输出应为:ll
有了所有这些信息,一维 std::string 数组会被视为矩阵吗?
矩阵是数字或其他数学对象的矩形数组,定义了加法和乘法等运算([https://en.wikipedia.org/wiki/Matrix_(数学]((。当涉及到一维 std::string 数组时,它们始终是矩形的属性不成立。例如,您可以拥有以下内容:
std::string strNames[] = { "David", "Bob", "Yana" };
对于上述数组,值strNames[1][3]
没有很好地定义。在矩阵的情况下,如果第一列有 5 个值,那么所有其他行也应该有 5 列,一维 std::string 数组可能是也可能不是这种情况。因此,你不能说一维 std::string 数组也是矩阵。