user103052
user103052

Reputation: 51

c++ vector allocation question

I am trying to push a vector into a list. My question is how do you allocate a new vector after you push it into the list.

std::list<std::vector<int>> foolist;
std::vector<int> foo;

while(1)
{
  foolist.push_back(foo);
  foo = new std::vector<int>;
}

The above code doesn't work.
error: invalid conversion from ‘int’ to ‘int*’

This is for a crypto class. I need to generate a bunch of hashes and check for collisions. So yes, in some ways I will run out of memory if I can't find a collision. (Collision - finding a input that will generate the same hash).

Upvotes: 0

Views: 192

Answers (1)

Jerry Coffin
Jerry Coffin

Reputation: 490048

You don't need to allocate a new vector. std::list will actually hold a copy of what you pushed, so you can just do:

std::list<std::vector<int>> foolist;
std::vector<int> foo;

for (int i=0; i<100; i++)
  foolist.push_back(foo);

Chances are this whole thing is a mistake though -- I'd consider almost all use of std::list questionable, and this one even more so than most others.

Upvotes: 3

Related Questions