4

I need to push 66,000 vectors (the number of vectors is not fixed, it can be 90,000 vectors also. For the sake of brevity I am showing the below code with the example of 66,000 vectors) of type vector into the following vector:

vector<int> vec;

The size of each of the 66,000 vectors is 9,000 elements. I am using the following for doing the same:

vec.reserve(66000*9000);
for(int j=0;j<66000;j++)
    for(int i=0;i<9000;i++) //9000 elements in vec1[i] per vector is not fixed
        vec.push_back(vec1[i]); //i am pushing i as an example

Is there someway by which I may increase the efficiency of this code?

I need to concatenate too many vectors, so a solution for the same is potentially different from concatenating two vectors. Also I can not use multi-threading as mentioned in the previous question

Steg Verner
  • 893
  • 2
  • 12
  • 28

3 Answers3

6

Try the following

std::vector<int> vec;
vec.reserve( 66000*other_vec.size() );

for ( size_t i = 0; i < 66000; i++ ) 
{
    vec.insert( vec.end(), other_vec.begin(), other_vec.end() );
}
Galimov Albert
  • 7,269
  • 1
  • 24
  • 50
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

You may use resize() instead of reserve(). Remove push_back when resize() is used. Memory is allocated with resize() and initializes. reserve just allocates but doesn't initialize.

0

You can go to the underlying data directly via data():

http://en.cppreference.com/w/cpp/container/vector/data

Then you can copy the data directly (e.g. just memcpy).

std::vector<int> foo;
for (int i=0; i<10;++i){
    foo.push_back(i);
}

std::vector<int> bar;

bar.resize(10*10);

int pos = 0;
int size = foo.size()*sizeof(int);
for (int i=0; i<10; ++i){
    memcpy(bar.data()+pos/sizeof(int),foo.data(),size);
    pos += size;
}


for (size_t i=0; i<bar.size(); ++i){
    cout << i << " " << bar[i] << endl;
}
FooTheBar
  • 818
  • 1
  • 9
  • 20
  • The STL uses memcpy internally anyway when it is safe to do so. See: http://maintainablecode.logdown.com/posts/159916-memcpy-memmove-and-memset-are-deprecated. This is way too complicated/error prone and doesn't actually buy you anything. – Mark Jul 13 '15 at 20:17