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
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) ...
5
votes
1answer
108 views

C++ container — variable byte coded numbers list

I'm writing a container for unsigned integers, which uses variable byte coding. It works but some decisions I took look ugly so I would be glad if you gave me some hints for improving the design. I ...
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 ...
6
votes
1answer
278 views

Given a vector and a target sum, returns zero-based indices of any two distinct elements whose sum is equal to the target sum

I am trying to solve 4. Two Sum from this set of C++ interview questions. As far as I can think this solution has a time complexity of \$O(n \log n)\$ can it be further optimized? Write a function ...
1
vote
2answers
448 views

Text-based game involving Objects placed inside Rooms [closed]

I'm a coding newbie (and despite what my user name may imply I am far from a pro), and I'm trying to write my own text-based adventure game. I have two questions. First, I want to implement an ...
3
votes
2answers
606 views

Pairwise Hamming Distance [closed]

This is a program to return the sum of pairwise Hamming Distance of the binary string representation of a vector of integers. Please let me know how I can make this better? ...
6
votes
1answer
122 views

Move-aware noexcept stack in C++

I'm learning C++, and move semantics and exception safety seem to be a top priority when using the language. I implemented the simplest data structure I could think of. ...
6
votes
0answers
139 views

OCaml safe uninitialized array

One of the annoying things about OCaml arrays is that every cell has to be initialized when the array is first created. I'm trying to implement an array in OCaml that distinguishes ...
3
votes
1answer
261 views

Efficiently Generate Subset of all Permutations or Combinations with and without Repetition (C++)

Suppose I want to write some functions which return the vector containing a vector representing all permutations or combinations with or without repetition (dependent on the function) of a subset of a ...
1
vote
2answers
79 views

Making Math vector implementation using java [closed]

Below is the Code. ...
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 ...
2
votes
1answer
2k views

Vector-like custom container with improved insert performance

I rolled a little container class (just for couriosity) which was meant to be able as a vector replacement in some cases. (Whoever wonders about the lack of move semantic etc, I just left it out due ...
1
vote
2answers
820 views

Finding subsequences of vector from a vector

I'm trying to find subsequences of vector from a vector. For example, if I want to find {2, 3} from {1, 2, 3, 1, 2, 3, 4}, the result should be {1, 4}. Here's what I wrote so far. ...
4
votes
2answers
106 views

Adding input strings and finding occurrences of certain prefixes

A user inputs data using a keyword add word and when he wants to find words with particular prefix then he enters find keyword. ...
1
vote
1answer
206 views

Map square coordinates in matrix to flat array indices without hurting performance

I'm more like playing and the problem I'm going to demonstrate is a learning task, so to speak, not something I really need to solve. Imagine having a matrix of data. You chose square in it and ...
5
votes
2answers
277 views

Simple expandable vector in C

I'm getting tired of reading templated c++ code so I am dipping my feet in c. Here is a small, not full, implementation of an expandable vector. Right now I need only ...
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 ...
4
votes
2answers
654 views

Dynamic array C implementation

I'm a C beginner. For a tiny personal project, I was in need of an "auto-growing" array and here it is the implementation. I'd like to get some feedback. At the end there is a test function called in ...
3
votes
1answer
89 views

Sorted vector/set class that works with simple types (char, int, double, etc…)

I'm new to C++ (approx. 3 months) and have just read Stroustrup's Programming Principles and Practice. Previously used Python a lot. As an exercise I tried to implement a custom container, with the ...
2
votes
1answer
372 views

Google CodeJam Qualification Round: Bathroom stall

Problem A certain bathroom has N + 2 stalls in a single row; the stalls on the left and right ends are permanently occupied by the bathroom guards. The other N stalls are for users. ...
1
vote
1answer
1k views

Resizing an `IList<T>`

I need to resize an IList<T>. I have written two versions and would like to have a review of those. I am particularly interested on which of the two is to be ...
5
votes
2answers
87 views

Checking whether a number starts with any of a list of string prefixes

This code will return false if the long double rand begins with any string in the vector ...
3
votes
1answer
938 views

Weak pointers to vector elements idea

I want to store some objects in linear memory, but sometimes I want to use these objects in some place for some time, but in the meantime object can be deleted, so I want to use weak_ptr's. This is my ...
3
votes
1answer
63 views

Loose string matching for words in postal addresses

I'm trying to compare every element in a vector with every other. From a dataframe of addresses, I extract streets for a zip code. And a vector of these street strings (like ...
1
vote
0answers
2k views

Reading and storing nested data

I'm reading data from a json-structure that describe aquariums and how many fish they contain (among other things). Overall, the structure of the data is like this: "house" "info" "animals" "fish" ...
1
vote
0answers
49 views

Sending info (state, college, teacher) to client based on the request received by the server

I wrote this code to send info as (state, college, teacher) from the server to the client. I am running a script which will dump all the info about (state, college and teacher) into a file with a ...
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 ...
5
votes
3answers
582 views

Finding consecutive numbers in a vector

I need to write a program to print the line numbers, in ascending order, of a particular word in a file. Example output of the program would be: ...
1
vote
1answer
188 views

A wrapper on top of boost vector

Please review the following piece of code. The class implements a light weight wrapper on top of boost::container::vector. I am not getting the expected performance — nowhere comparable to std::vector....
6
votes
1answer
737 views

Find only unique number in vector

Find the number occuring only once in a vector. Given a vector A, containing N pairs (1, 1, 2, 3, 3 ... etc.) and one unique number, find and return the one unique number. I wrote the following code ...
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: ...
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,...
-2
votes
1answer
2k views

C++ Erasing an object from vector of pointers

I am making this simple game in SFML and I have this vector of pointers std::vector<Brick*> bricks; which stores objects of class ...
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 ...
2
votes
1answer
5k views

Vector with move constructor and move assignment operator [closed]

Please verify whether the code for a move constructor and a move assignment operator is correct. ...
1
vote
6answers
2k views

Reading strings into a vector, without using namespace std

I have been told that using namespace std is a bad practice (especially in header files). But wouldn't this make the program less readable? ...
6
votes
4answers
7k views

Removing Values From a Vector

I am currently writing some generic functions to make working with vectors a little "cleaner". Working with the "erase-remove idiom" can be a little confusing sometimes, and make the code look a ...
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
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. ...
2
votes
0answers
65 views

Genetic operators in Haskell

I have implemented two genetic operators, which is really just a fancy way of mangling lists. In particular OX-1 (illustration) and displacement mutation. I found it necessary to extract out the ...
4
votes
1answer
935 views

Sliding window over a 2D vector, implementation of Marching Squares algorithm

I have not used C++ in quite a while and looking for advice/feedback, the code below implements the Marching Squares Algorithm which is detailed here. While the implementation appears correct and ...
7
votes
2answers
509 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-...
2
votes
2answers
113 views

a way to rewrite compress and ensure capacity methods in an array

I've been working on this array, and I was wondering if there was any other way to rewrite the compress and ensure capacity methods since I've been running into problems with a different application ...
5
votes
1answer
3k views

Implementing views on a std::vector

Goal: Wrap a vector. The wrapper can create view on part of the vector. Support const_view and view. view allows modification of elements while const_view does not. The views are ranges (has begin() ...
1
vote
2answers
2k views

Custom STL Vector in C++

This is my first time I tried to implement a custom STL vector with iterator. I have some questions: What are better ways to write an iterator? I know that there is a ...
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. ...
-1
votes
1answer
596 views

Finding the lowest missing integer in a vector containing negative and positive values [closed]

I'm writing an operation to find the lowest missing element of a vector, V = 1..N + 1. This has to be performed in \$O(N)\$ time complexity. Solution One: ...
0
votes
2answers
194 views

LSH hashcode generator function

For building an LSH based system in Python, I need to have a very fast calculation of the hashcode. I won't need to explain here the LSH algorithm itself, but I need help in improving performance of ...
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" ...