Questions tagged [vectors]

Vectors are sequence containers representing arrays that can change in size. For questions about geometric or algebraic vectors, use [coordinate-system] instead.

Filter by
Sorted by
Tagged with
28
votes
5answers
4k views

C++ Vector Clone

I'm learning C++, so I decided to make a simpler clone of std::vector. Concerns: I have seen people defining methods outside of the class, and only prototyping ...
19
votes
8answers
48k views

Java function to read a CSV file

I have a java function that reads a csv and returns its content as a Vector<Vector<String>>. The function seems to work as I need it, but my gut ...
18
votes
4answers
68k views

STL vector implementation

I've implemented a simple vector-like structure. I would appreciate all criticism relevant to code, style, flow, camelCase vs underscore, and so forth. ...
17
votes
2answers
545 views

How to speak Brainfuck: for beginners

The following code transforms multi-line input into the the Brainfuck equivalent. One line turns into one program. The resulting programs are split by newlines for readability. Brainf.cpp ...
15
votes
2answers
24k views

C++ Split string into a vector

While profiling parts of my code, I noticed that my Split( ... ) function is rather slow, (about 50% of the time). I was wondering if there was a more efficient way ...
15
votes
2answers
3k views

Template vector struct in C11

This one review will be a little bit tricky: I was trying to implement a template Vector struct in C11. Of course, templates don't really exist in C11, so I used a ...
13
votes
6answers
4k views

Modern Vector implementation

Please give me some feedback on my attempt at a vector. I wanted this to be the lowest level concept of a vector that I'm using, so I'm primarily concerned with speed, and I would build wrappers ...
13
votes
1answer
1k views

Comparing std::vector<bool> to std::vector<char>

A recent comment to an answer of mine here on Code Review brought up an interesting point. The comment was that one should use std::vector<char> over ...
11
votes
5answers
2k views

A simple program with trains and inheritance

Here it is my program. I'm especially worried about the main because I know that one must be careful while instancing vectors of "inherited" objects. The code I have works fine (as far as I can see) ...
11
votes
2answers
8k views

C++ vector implementation

I have tried to implement a basic vector type in C++, yet I am not sure if I can do anything any more efficiently. ...
11
votes
1answer
377 views

Vector implementation, which received unhelpful negative feedback from professor

I've just finished a homework assignment to implement a simple Vector class. -Kindly ignore the following two paragraphs if you don't want to read someone whining about their professor Yadda, yadda,...
11
votes
2answers
6k views

Vector Implementation C++

I have attempted to implement a similar version of the STL Vector; several functions are missing but I'd like a few words of advice on whether I am indeed on the right track or whether I should change ...
11
votes
2answers
101 views

Dynamic vector of void* in C

I have implemented a simple dynamic vector library in C. It is a header-only library. Full library listing - void_vector.h: ...
11
votes
3answers
788 views

C++ Vector The basics

Following on from my two previous posts. An alternative vector An Alternative Vector (Copy Assignment Operator) I have written a detailed blog about how to write a minimal vector like class. This ...
11
votes
1answer
121 views

IndexedVector - holding a vector of unique objects with faster finding elements

I am trying to write a class that will enable me to hold an std::vector of some unique objects, but faster finding elements already stored in a vector. I want the ...
10
votes
4answers
5k views

A vector implementation

I am an undergraduate currently learning data structures and C++. I am trying to implement some simplified STL containers. Here is my implementation of vector, which does not have the allocator. My ...
10
votes
4answers
3k views

Basic iterator supporting vector implementation

On the fly, I just implemented this iterator supporting vector class to understand how range-based for loop works. It's really basic but let me know what can be improved. Vector.h ...
10
votes
3answers
11k views

Storing words from an input stream into a vector

I'm extremely new to C++ and am doing the exercises on the book Accelerated C++. Here is one of the exercises: 4-5. Write a function that reads words from an input stream and stores them in a ...
10
votes
2answers
2k views

Implementation of a generic List

I have only been self teaching myself programming on and off for about 5 months. The purpose of me writing this class is to incorporate the knowledge that I have gathered since I started. ...
9
votes
2answers
1k views

Implement Own Vector Class in C++

I am preparing for an interview and came to know about this question: implement a vector class in C++. I thought of how I would write it an interview and included the code below. The things I know I ...
9
votes
3answers
928 views

Simple re-implementation of std::vector

Here is a simple reimplementation of most of std::vector. I want to check if I implement it correctly but the standard libraries source code is not very readable ...
9
votes
2answers
112 views

Generic data structures in C

I'm new to C, and I thought a great way to learn would be to implement some generic data structures and algorithms. So far I've implemented a linked list and a dynamic array. Both are generic, the ...
8
votes
4answers
2k views

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

I'm trying to construct an output table containing 80 rows of table status, that could be EMPTY or USED as below ...
8
votes
5answers
753 views

Counting pairs of integers whose sum is less than a given threshold

Recently I was going through this codechef problem while practicing for the upcoming zoc. The problem asks to calculate number of combinations of the given numbers where the sum is less than another ...
8
votes
4answers
1k views

std::vector composition with self populating super-powers

I have written a deliberately pretty simple class that wraps a std::vector to basically make it capable of self-populate itself with random generated numbers, ...
8
votes
6answers
365 views

Removing by indices several elements from a vector

From a sorted vector of indices (toRemove), I would like to remove from another vector (v) the elements at these indices. Note ...
8
votes
4answers
307 views

An alternative vector

Based on this question (as a starting point) and a couple of other questions about rewriting vector in C++, I am planning to write another blog article to show the best way (or more precisely, the ...
8
votes
2answers
951 views

C++11 Vector with move semantics

I am practicing move semantics and placement new by writing a custom Vector class but I am not confident that I use them right. I would really appreciate some pieces of advice regarding my code. Here ...
8
votes
2answers
5k views

Longest string in a vector — two implementations

I was given this piece of code, which calculates the length of the longest string in a vector of ...
8
votes
2answers
105 views

Read file into vector<byte>

I need to read a range of bytes from a file and return it as a std::vector<std::byte>. My current code looks like this: ...
8
votes
2answers
219 views

Graphical editor programming challenge

I have posted here my working and accepted solution to the graphical editor programming challenge (detailed here) for your review. The program recognizes the following image editing commands: ...
8
votes
1answer
175 views

An Alternative Vector (Copy Assignment Operator)

This is a continuation of An Alternative Vector, taking a closer look at the copy assignment operator. In the following code, I am only posting the new version of the copy assignment operator; all ...
8
votes
1answer
2k views

Vector of derived classes

Everyone knows you can't put a Derived in an std::vector<Base>. I decided to implement a collection which does allow you ...
7
votes
5answers
864 views

Vector-transposing function

I profiled a library I'm writing that uses vector transposes and found that I am spending a good bit of time doing the following transpose. I am using a std::vector ...
7
votes
4answers
6k views

C++ Template-Based ArrayList Implementation

As part of my learning C++, I have created an ArrayList class. It shares some of its interface with the Java ArrayList, but it by no means implements anywhere close to the full set of functions an ...
7
votes
4answers
2k views

Custom vector that uses less memory than std::vector

I'm using vectors to create matrix and the vector itself is consuming a large space. So I implemented a version that would use less memory and, if possible, be as fast or faster than ...
7
votes
5answers
2k views

An ordered set in C#

I need a generic data structure that ensures that its elements are unique. C# has HashSet<T>, great! As it turns out, the elements have to stay in the order ...
7
votes
4answers
10k views

C++ vectors sort ascending/descending

How can I eliminate repetition from this code? ...
7
votes
3answers
1k views

Sort Characters By Frequency

I solved this problem. Problem Statement Given a string, sort it in decreasing order based on the frequency of characters. Example 1 Input: "tree" Output: "eert" ...
7
votes
2answers
993 views

C - Generic ArrayList / Dynamic Array Implementations

As a small project to refresh my C and data-structure knowledge, I decided to write a generic ArrayList (as in Java). This is my most current version: ArrayList.h: ...
7
votes
3answers
1k views

Pascal's triangle using C++ vectors

This is my C++ program. I wanted to print out Pascal's triangle. I let the user decide how many rows to print out. The first row is considered as 0, and is just: 1 I'm using vectors for this task. ...
7
votes
2answers
5k views

Mean, Median and Mode of a QVector

I would like some help condensing this code down to size. I am trying to utilize Qt's container classes as efficiently as possible. I do not believe this code reflects my objective. ...
7
votes
2answers
2k views

Short and Messy Polybius Square

I attempted to recreate the Polybius square, also called the Polybius checkerboard, which was used in Ancient Greece for cryptography. Since it is an uncommon cipher, it is nowhere on Code Review. ...
7
votes
2answers
504 views

Fast variable-length stack allocator for vector<> in C++

I wrote a variable-length stack allocator for the vector<> class in C++ 11. In order to be able to allocate size dynamically at runtime I made use of the non-...
7
votes
2answers
1k views

Sorted vector (aka flat_set) for objects (pointers) with custom embedded key (functor used)

BEFORE YOU READ: this link should be included when you just don't like the format of the question and for those that read this question for the first time, the link may give you the idea what happened ...
7
votes
1answer
613 views

A pointer vector sorted by its member function

I'm asking for suggestions on a random accessed vector with allocated elements sorted by its key obtained from its member function. I use it with Qt tree view where the access, add and deletion of ...
7
votes
1answer
5k views

Writing a vector to a file

The following function (function template rather) is to take a vector of any type and write it a file using a filestream object. ...
7
votes
1answer
61 views

Managing initialization for a vector abstract data type in C

As an assignment, I was to create an \$n\$-dimensional vector type for \$n \le 20\$. This vector only has to support addition and scalar multiplication. I tried to write good code, with proper memory ...
6
votes
3answers
641 views

Determine whether one vector is a prefix of the other

Exercise 5.17: Given two vectors of ints, write a program to determine whether one vector is a prefix of the other. For vectors of unequal length, compare ...
6
votes
4answers
33k views

Creating a custom Vector class

I'm new to C++ and am doing the C++ 4th Edition Stroustrup book. I expanded on one of the examples, and have a few questions to ask (embedded within the code: ////QUESTION 1-9). Please provide any ...