c++ - vector 、字符串还是数组?

标签 c++ arrays

我的任务是编写一个 C++ 程序,在终端中创建一种文本编辑器。文本编辑器由单独的行组成。每个单独的行必须代表链表的一个成员。我正在遵循制作两个类(class)的格式。一个是线条的“列表”,另一个是线条的类。他们是 friend 类。对于每一个新行,都会创建一个行对象并将其添加到链表中。线的“列表”类通过执行插入线和从链表中删除线等操作来管理线对象和链表。

我的问题是,对于每一行,我应该将其存储为字符数组、字符串还是 vector ?

还应该注意的是,一旦将行添加到链表中,就不需要对其进行编辑。它们可以被删除并可以插入一个新行。 只有三个操作是可能的。将整行插入列表,将一行添加到列表末尾,并从列表中删除现有行。任何给定行的内容在创建并添加到列表后都不会更改。

我得到的一些示例输出:

1> The first line
2> The second line
3>
4> And another line
5> One more line
6>
7> what do you try to type here?
8> 
9>
10> 
11> This is the last line
> I 2 ↵
2> This line should be inserted into line number 2
> L ↵
1> The first line
2> This line should be inserted into line number 2
3> The second line
4>
5> And another line
6> One more line
7>
8> what do you try to type here?
9> 
10>
11> This line is boring......
12> This is the last line
> I 16 ↵
16> The line number is big BIG
> L ↵
1> The first line
2> This line should be inserted into line number 2
3> The second line
4>
5> And another line
6> One more line
7>
8> what do you try to type here?
9> 
10>
11> This line is boring......
12> This is the last line
13>
14>
15>
16> The line number is big BIG

最佳答案

什么最能代表您对实际数据的看法。

实际数据是字符串吗?使用 std::string。它是固定大小元素的固定大小数组吗?使用 std::array。它是固定大小元素的可变大小数组吗?使用 std::vector。

当您处理文本时,我会选择 std::string(或其支持 unicode 的变体),而 vector/数组操作通常采用统一元素。

关于c++ - vector 、字符串还是数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32901721/

相关文章:

c++ - vector 内存管理

c++ - lambda 始终返回 '1'

php - 获取先前的数组值

c - 结构中字节数组的对齐

java - 创建一个字符串数组,其长度由用户输入确定

c++ - 字符型变量的地址

c++ - OpenGL 的 GLM 库没有幅度函数吗?

c++ - FreeType 中的新线像素距离?

来自mysql结果的php多维数组

c++ - 在结构中的数组中初始化结构中的数组时出现问题