1

Right now I'm making a class called Stopwatch for a Windows phone app. I need a dynamic data to add laps like a stopwatch does. That is, if a user presses the lap button while the stopwatch is running, it adds a data structure

struct lap
{
   unsigned double start;
   unsigned double stop;
}

(defined as a private variable in my Stopwatch class) to the end of some sort of dynamic data structure that is saving all the laps.

Now, Bjarne Stroustrup himself says that we should always use std::vector over linked lists: https://www.youtube.com/watch?v=YQs6IC-vgmo. So the Lord of C++ tells me that I should have

std::vector<Stopwatch::lap> Laps;

as a private variable in my class. However, at the same time, I don't need anywhere near all the functionalities of an std::vector, since the only things I'll be using it for is iterating through the elements and using push_back(). Should I create another generic linked list class that is limited to the functionalities I need?

  • FYI, Bjarne Stroustrup is the creator of C++ – Raptor Apr 02 '14 at 06:13
  • 3
    `vector` is not analogous to a linked list. – Ed S. Apr 02 '14 at 06:13
  • It is fun to create your first container structure... fun starts to wear out on the 2nd, the third... And BTW, a vector is not a linked list. – jsantander Apr 02 '14 at 06:13
  • 3
    This is completely opinion based because both approaches work. Personally, I think you should not create a new collection class. Chances are, you will either introduce bugs, or waste a ton of time testing it. std::vector works well, why not use it. I for one would not buy an additional new car, just because my current car can do more than required. – nvoigt Apr 02 '14 at 06:14
  • @user3474967 Offtopic: So, Bjarne recommends vectors over lists. Ok. But you still ask if you must reimplement list. Not vector. Where is the logic? Probably, you need to check [this](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Ivan Aksamentov - Drop Apr 02 '14 at 06:29
  • I don't think it's opinion-based; it's based on a mistaken assumption. "I don't need all those functions" implies that you'd get them. You don't; only the vector methods used are instantiated. – MSalters Apr 02 '14 at 10:53

3 Answers3

6

If you need a linked-list, you can use std::list. But linked lists are rarely the correct tool. Way overstressed in programming books and courses, in my opinion. The default choice for a dynamically resizable sequence container in C++ is std::vector. So use it. If you don't need the extra functionality, don't use it (the unneeded functionality). It costs you nothing. Don't waste your time implementing another redundant and buggy sequence container.

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
0

Not needing all the available functionality provided by a standard class is a poor reason to write your own. If everyone followed that logic, we'd have tonnes of non-standard classes which just provide subsets of the functionality of the standard classes.

Moreover, it is not trivial to write classes representing abstract data structures, and your own implementations are more likely to have bugs than the standard one, and will likely not be as optimised.

Finally, it is not a significant drain of resources to use classes which provide more functionality than you use. This functionality is primarily just method implementations, and they don't take up a lot of space in the grand scheme of things.

Anders Rabo Thorbeck
  • 1,126
  • 3
  • 18
  • 28
0

The std algorithms and containers use an efficient code base that is reusable and extensible. You should consider that other programmers should be able to understand your code simply and reuse or extend it. It is much more better to use std containers for regular usage. That's because most programmers are familiar with std and also most of the algorithms in std are very optimized and i doubt you can write something faster that std::sort.

Unless when performance demands are critical, or the container requirements are very specific, there may be no choice but to implement a container from scratch.

Nejat
  • 31,784
  • 12
  • 106
  • 138