c++ - C++ 中的字符矩阵?

标签 c++ matrix

我一直在努力尝试这个 C++ 练习,但没有成功。 如何创建一个包含 4 个单词的字符矩阵,每个单词的每个字母都位于一个字符空间上? 这是我的尝试。
我想使用 null 来知道字符串的结束位置。由于练习的说明,我必须使用字符矩阵。 正如你所看到的,我尝试了 vector [0]和其余的不同方法。它们都不起作用。

int ax = 3;
char ** vector = new char* [ax+1];
for (int i = 0; i < ax; i++){
    vector[i] = new char[10];
}
vector[0] = "F","u","t","b","o","l";
vector[1] = "AUTOmata";
vector[2] = "fUT";
vector[3] = "auto";
vector[ax+1] = NULL;

最佳答案

I have been trying hard with this C++ exercise to no avail.. How do i create a char matrix holding 4 words, with each letter of a word on a char space?

没问题。您可以使用std::vectorstd::string为了这个任务。事实上来自std::string你可以用 data() 得到一个 C 字符串(以 null 结尾,因为你喜欢它们) .

std::vector<std::string> vector {
    "Futbol",
    "AUTOmata",
    "fUT",
    "auto"
};

Live demo


另一方面,如果您想使用 C“功能”:

const int largo = 3;
char** vector = new char* [largo+1];
for (int i = 0; i < largo + 1; ++i)
    vector[i] = new char[10];
strcpy(vector[0], "Futbol");
strcpy(vector[0], "AUTOmata");
strcpy(vector[0], "fUT");
strcpy(vector[0], "auto");

Live demo

关于c++ - C++ 中的字符矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22897676/

相关文章:

c++ - 使用 Visual C++ 进行地址 sanitizer : ignore read buffer overflows while still catching write buffer overflows

python - 如何在numpy矩阵中找到最小值?

Python:水平切片矩阵

python - 列表理解选定的索引

r - R 中的 mat2listw 函数是否返回行标准化空间权重矩阵?

r - Error in `[<-` (`*tmp*` , , 下标越界 下标越界

c++ - 使用 C++ Boost 的格式对象作为异常成员

c++ - DirectX 9 引擎 Z 缓冲区不适用于 D3DImage

c++ - 使用 g++ 删除了 linux 中的 std basic_stringstream 函数

c++ - 为什么默认情况下数据实例不为 NULL?