Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

another vector vs dynamically allocated array

There shouldn't be a performance difference between vector and a dynamic array, since a vector is a dynamic array.

The performance difference in your code comes from the fact that you are actually doing different things between the vector and array version. For instance:

primes(primes_count, 0);
    for (auto i = 0; i < max_prime; i++) {
            if (is_prime[i]) {
    return primes;

This creates a vector of size primes_count, all initialized to 0, and then pushes back a bunch of primes onto it. But it still starts with primes_count 0s! So that's wasted memory from both an initialization perspective and an iteration perspective. What you want to do is:

std::vector<int> primes;
// same push_back loop
return primes;

Along the same lines, this block;

is_prime(max_prime, true);
    is_prime[0] = is_prime[1] = false;
    for (auto i = 2; i < max_prime; i++) {
            is_prime[i] = true;

You construct a vector of max_prime ints initialized to true... and then assign most of them to true again. You're doing the initialization twice here, whereas in the array implementation you only do it once. You should just remove this for loop.

I bet if you fix these two issues - which would make the two algorithms comparable - you'd get the same performance.

Categories : C++

Related to : another vector vs dynamically allocated array
c++ vector of objects, containing dynamically allocated memory - 'erase' does not work
What you're doing in operator= makes no sense whatsoever. You should assign the state of the parameter to the current object. Instead, you create a new object and return a dangling reference to it. Try to google for "assignment operator" or "special member functions" or "rule of three" for details.

Categories : C++
Getting out of scope error with dynamically allocated array
If you declare the variable inside the try block then it's only accessible there. You can work around this by moving the declaration outside of the block. int *newArr; try { newArr = new int[els]; } catch(const bad_alloc &) { die("Alloc Failure"); } for (int i = 0; i < els; i++) newArr[i] = a[i]; Or by moving the rest of the code inside the try. try { int *newArr = new

Categories : C++
Create hash table with dynamically allocated array?
I can't completely understand your question, but if your problem is a "scope" problem, you can solve it by declaring your array as a member of your HashTable class: HashTable.cpp class HashTable { private: Item* items; public: HashTable() { items = new Item[size]; } ~HashTable() { delete[] items; } }; As a member it will be visible from every metho

Categories : C++
How can I plot a surface using a function with a single vector or array input using matplotlib?
If the input x is a 3-D array representing a regular mesh, you can do, assuming a shape (2, m, n): def f(x, sign=1.0): x1 = x[0, :] x2 = x[1, :] # Objective function out = sign*(x1**3 + 3*x1**2 - 2*x1*x2 + 3*x1 + x2**3 + 3*x2**2 + 3*x2) return out such that out will be a 2-D array with shape (m, n), ready to be plot with matplotlib: ax.plot_surface(x[0, :], x[1, :], f(x), c

Categories : Python
php multi array create dynamically
This should work if $tab always has 3 indices: function func(&$name, $indices, $value) { $name[$indices[0]][$indices[1]][$indices[2]] = $value; }; func($tab, array( 'k1', 'k2', 'k3' ), 'value' );

Categories : PHP
Recently Add
VS2013 - Error with multiple includes of same header
Multiplying two large integers stored as integer vectors
How Do I Create a max Functor?
Eigen sparse matrix multiplications seem to compute full matrix
zlib compress() produces awful compression rate
How to delete a window?
passing variable without a name to function
OMNeT++ disassembling received messages
How do I ignore certain file types with cppcheck?
Posting a message to UI from business logic
STL algorithm/functional
su -c replacement using c++
How to use a sdk (non-ros header/lib/binaries) in ros package?
using std::chrono::high_resolution_clock to write a frame 30 times per second
Extracting ints and shorts from a struct using AVX?
Finding Bridges in a graph C++ (BOOST)?
Qt Creator cannot find library with custom cmake step
running visual studio release build .exe file in different machines
class oop realions cpp
Declare interface and Implement the interface using multi-inheritance in C++
How to access all elements of boost::multi_index::multi_index_container<> by their indices?
Understanding what a double pointer to a reference does C++
ld: symbol(s) not found for architecture x86_64 when compiling c++
Disconnected node during Graph traversal
Const behavior in operator >>
Structuring C++ Application (directory and folders)
Sendkey Function Enter Key Issue
Why the memory do not release after funciton call?
Passing C++ functions as arguments without function pointer/inlined?
How to create multiple objects inside of for loop with different names?
© Copyright 2017 Publishing Limited. All rights reserved.