Skip to main content
29 votes

C++ Vector Clone

Issues Your vector assumes that the type being stored has a default constructor. arr = new element[size]; Also this is really inefficient for expensive to ...
Loki Astari's user avatar
  • 95.7k
23 votes
Accepted

A vector implementation

Design The main thing about a vector is not constructing its members until they are put into the container. The vector you have constructs all the members immediately which can be very expensive if ...
Loki Astari's user avatar
  • 95.7k
22 votes

Modern Vector implementation

Memory management new T[m_capacity] default-constructs m_capacity objects of type T in ...
hoffmale's user avatar
  • 6,508
20 votes
Accepted

Most efficient way to find an entry in a C++ vector

Header files It's strange that this code uses the C header <string.h> but the C++ versions of <cmath>, ...
Toby Speight's user avatar
  • 78.9k
17 votes
Accepted

C++ Split string into a vector

I haven't attempted to measure the timing of this, in part because I don't know how you're planning to use it, but here are some ideas for improving your code. Consider returning something useful ...
Edward's user avatar
  • 66.5k
17 votes
Accepted

Basic iterator supporting vector implementation

The range-based for loop This works via the begin() and end() functions. So for any class <...
Loki Astari's user avatar
  • 95.7k
17 votes
Accepted

Vector-transposing function

Change your column loop to use a reference. for (const auto &c : column_vec) Without the reference, a copy will be made of each vector. This will involve a ...
1201ProgramAlarm's user avatar
15 votes
Accepted

Dynamic array of int in C

Thank you for the very nice Doxygen comment. Consider renaming the struct to DynamicIntArray, and then I won't even have to read the comment. Or perhaps in the use ...
J_H's user avatar
  • 32.4k
14 votes
Accepted

Vector implementation, which received unhelpful negative feedback from professor

I understand that some of this class is predicated by your professor; However I will review the code as a whole and you can then choose what can be changed and what cannot be changed. Interface/High-...
Emily L.'s user avatar
  • 16.6k
14 votes
Accepted

C++ Vector Clone

First of all, lets answer the questions you have: Yes, no, maybe. Some people do it one way, some the other. Personally, I prefer splitting declarations and definitions up so that I have an overview ...
Ben Steffan's user avatar
  • 5,228
14 votes

Dynamic array of int in C

Use realloc - it can be much more efficient than malloc+copy+free Unlike C++, C's allocator API doesn't totally suck: it has ...
Peter Cordes's user avatar
  • 3,346
13 votes
Accepted

Implement Own Vector Class in C++

You tagged c++11, but your code does not look like C++11. You do not implement move construction and move assignment. You should implement them. Here's a sample implementation using the copy-and-swap ...
L. F.'s user avatar
  • 9,570
13 votes

Read file into vector<byte>

Nice: using bytes = std::vector<std::byte>; I would call it Bytes to make it clear it is a type rather than an object. ...
Loki Astari's user avatar
  • 95.7k
12 votes

Modern Vector implementation

A few things you can improve upon: Use more of <algorithm> and <memory> Raw loops are verbose and unreadable. Use ...
papagaga's user avatar
  • 5,757
12 votes

Basic iterator supporting vector implementation

Use delete[] You are allocating an array of objects with new[], but attempting to delete them with ...
cariehl's user avatar
  • 957
12 votes
Accepted

Simple re-implementation of std::vector

Code: I realise you're not implementing the complete functionality, but you probably want add and use at least the value_type, ...
user673679's user avatar
  • 11.8k
12 votes

Vector-transposing function

Is the vector-of-vectors representation imposed on you, or can you change it? It's going to be more efficient to allocate a single vector, and use simple arithmetic to convert row/column ...
Toby Speight's user avatar
  • 78.9k
12 votes

Implement Own Vector Class in C++

It's great that you provide a test program. Although it doesn't yet test very much, running it under Valgrind uncovers a few wild accesses: ...
Toby Speight's user avatar
  • 78.9k
12 votes

C++ Creating custom vector

Correctness and testing: First off... note that compilers are not required to do anything with template code unless it's actually used ("instantiation"). Some compilers (or versions of a ...
user673679's user avatar
  • 11.8k
12 votes

C++11 Custom Vector Implementation

This is not a bad attempt at making a vector-like container, except it is lacking support for const and makes unnecessary copies (see below for a more detailed ...
G. Sliepen's user avatar
12 votes

Vector Implementation in C

#define growCapacity(capacity) \ (capacity < 8) ? 8 : capacity*2 I don't think this should be in the header, as it's not useful to the user of our vector. ...
Toby Speight's user avatar
  • 78.9k
11 votes

Modern Vector implementation

There is a bug in pushBack, you need to take into account the case where you call it on an existing member of the array. For example in the code ...
David Brown's user avatar
11 votes
Accepted

A STL-like vector implementation in C++

Your two constructors don't store a value into m_capacity, so if the initial capacity requested (passed in as a parameter) is larger than the default capacity you'...
1201ProgramAlarm's user avatar
11 votes
Accepted

Proper Case Conversion (Performance)

std::vector<std::string> ToProperCase(std::vector<std::string>& array) { // ... return array; } There are many styles when it comes to ...
Snowhawk's user avatar
  • 6,148
10 votes
Accepted

A simple program with trains and inheritance

Coal_ already made some good points. Let's just elaborate on the one about private. A class makes everything in it ...
yuri's user avatar
  • 4,458
10 votes

A simple program with trains and inheritance

First, show your teacher the C++ Core Guidelines and various videos from conferences saying that you should not use naked new or ...
JDługosz's user avatar
  • 11.4k
10 votes

Modern Vector implementation

Use the well-known names for methods c_begin and c_end are so close to the standard-container ...
Toby Speight's user avatar
  • 78.9k
10 votes

Simple re-implementation of std::vector

Undefined type We use size_t with no definition. It seems like we want to use std::size_t, in which case, we need a suitable ...
Toby Speight's user avatar
  • 78.9k
10 votes

YAVI (Yet another vector implementation)

As a learner, I think you have done a great job. Here's some suggestions: General Don't use multiple public: labels. It seems your intent is to split the ...
L. F.'s user avatar
  • 9,570
10 votes
Accepted

Reverse each word in the string. All spaces in the string should be retained

The split function ...
L. F.'s user avatar
  • 9,570

Only top scored, non community-wiki answers of a minimum length are eligible