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 ...
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 ...
22
votes
Modern Vector implementation
Memory management
new T[m_capacity] default-constructs m_capacity objects of type T in ...
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>, ...
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 ...
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 <...
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 ...
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 ...
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-...
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 ...
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 ...
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 ...
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.
...
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 ...
12
votes
Basic iterator supporting vector implementation
Use delete[]
You are allocating an array of objects with new[], but attempting to delete them with ...
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, ...
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 ...
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:
...
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 ...
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 ...
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. ...
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
...
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'...
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 ...
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 ...
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 ...
10
votes
Modern Vector implementation
Use the well-known names for methods
c_begin and c_end are so close to the standard-container ...
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 ...
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 ...
10
votes
Accepted
Reverse each word in the string. All spaces in the string should be retained
The split function
...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
vectors × 315c++ × 248
c++11 × 50
performance × 47
beginner × 29
algorithm × 28
reinventing-the-wheel × 25
c × 23
array × 18
strings × 17
memory-management × 17
programming-challenge × 16
template × 15
collections × 13
matrix × 12
rust × 12
java × 9
object-oriented × 9
time-limit-exceeded × 9
c# × 8
sorting × 7
iterator × 7
comparative-review × 6
pointers × 6
c++14 × 5