1

I'm tasked with writing a c++ program that creates a sort of text editor in the terminal. The text editor consists of individual lines. Each individual line must represent a member of a linked list. I'm following a format of making two classes. One being a "list" of lines and the other being a class for the lines. They are friend classes. For every new line a line object is created and added to the linked list. The "list" of lines class manages the line objects and the linked list by performing operations such as inserting lines and deleting them from the linked list.

My question is, for each individual line should I store it as a character array, a string, or a vector?

It should also be noted that the lines need not be editable once they are added to the linked list. They can just be deleted and a new line can be inserted. Only three operations will be possible. Inserting an entire line into the list, adding a line to the end of the list, and deleting an existing line from the list. The content of any given line will not be altered after it is created and added to the list.

Some sample output that I was given:

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
4
  • "My question is, for each individual line should I store it as a character array, a string, or a vector? " This really depends from what the usage is. What is the list of all the possible operations you want to perform in lines? For example: do you want to be able to insert if the middle of a line? Do you want to delete a portion of a line? Also, do you have limits? A maximum number of lines or a maximum line length? Please start telling exactly which operations you want to perform on lines. Commented Oct 2, 2015 at 6:40
  • 1
    It's a text editor so string seems appropriate (for simple implementation). Commented Oct 2, 2015 at 6:43
  • Tip: consider getting your program working using std::list<std::string>, then if your assignment requires it write your own alternative list class that has a similar interface (exactly the same may be a bit hard, as it may require implementing iterators which isn't a beginner-friendly ask). Commented Oct 2, 2015 at 7:00
  • At this level of exercise I would just use std::string. Don't bother with any unicode at all, just pretend it's all ASCII. As long as you only do line editing you don't need to bother at all. Commented Oct 2, 2015 at 10:52

2 Answers 2

5

What represents your view on the actual data the best.

Is the actual data a string? Use a std::string. Is it a fixed-size array of fixed-size elements? Use std::array. Is it a variable-size array of fixed-size elements? Use std::vector.

I would choose std::string (or a unicode-supporting variant of it) as you're dealing with text, and vector/array operations typically assume uniform elements.

Sign up to request clarification or add additional context in comments.

Comments

1

In that case, my first choice would be const std::string. Do you need Unicode characters? In that case either resort to UTF8, or consider UTF16 with const std::wstring.

BTW.: Just because I've seen it too often. Don't implement your own linked list. Prefer std::list. It is usually better tested than YetAnotherLinkedListImplementationIDidMyselfAndAmProudOf. Also consider to use std::vector instead of std:list, because in modern computers memory is so much slower than the CPU, that a more predictable memory access pattern can overrule O()-complexity classes. See Bjarne Stroustrups talk at GoingNative 2012.

2 Comments

As much as I'd like to not implement my own linked list, I'm forced to do so as this is an assignment for a course that I'm taking.
In that case, as an exercise, of course, implement your own linked list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.