Link to home
Start Free TrialLog in
Avatar of LuckyLucks
LuckyLucks

asked on

generating vectors on the fly

Hi

Is it possible to generate vectors on the fly? I just need generic vectors that I then add to another  vector. I find it quite useless to create a vector and push into it  like
 std::vector<UINT64> temp;
temp.push_back(1);
temp.push_back(2);
temp.push_back(3);

 just so that i can  then add it to the enclosing vector.
enclosing_vector.push_back(temp);
temp.empty();
//add to temp again and push into enclosing_vector

I want to do something like this:
enclosing_vector.push_back(std::vector<UINT64> {1,2,3});

Is something more elegant possible?
Avatar of jkr
jkr
Flag of Germany image

You'd usually use 'vector::insert()' and an array for that, e.g.

UINT64 buf [] = {1,2,3};
size_t sz = sizeof(buf) / sizeof(UINT64);

enclosing_vector.insert(enclosing_vector.end(), buf, buf + sz);

Open in new window


Is that what you had in mind?
Avatar of LuckyLucks
LuckyLucks

ASKER

no, actually that is not what i wanted. I wanted a more elegant approach to do on the fly insertions into vectors (inserting vectors into vectors), without creating vector definitions for such a temporary use.

what you offered was another way of what i was doing in the first place and both are many lines of codes for a very simple purpose. I need something cleaner and more elegant if possible.
Well, acting another vector to a vector still follows the same scheme, i.e.

std::vector<UINT64> temp;
temp.push_back(1);
temp.push_back(2);
temp.push_back(3);
enclosing_vector.(enclosing_vector.end(), temp.begin(), temp.end());

Open in new window


Sorry, but there does not seem to be a more elegant way (or I am completely missing the point).
Sorry, but I can only guess that with your responsiveness, this is going to en dup nowhere since you are already posing new Qs about this: https://www.experts-exchange.com/questions/28346593/C-vector-manipulation.html

Can we cool down (or in your case heat up) to close that step by step?

And, as a HINT: Iteraction solves problems, not getting back half a weeek later. That is just plain frustrating.
>> I want to do something like this: ... std::vector<UINT64> {1,2,3}
You can overload the () operator (or another operator of your choosing) to do the push backs. Here is one approach.
class genVec : public vector<uint64_t>
{
public:
   genVec & operator () (uint64_t const & val)
   {
      this->push_back(val);
      return *this;
   }
};

int main() {
   genVec gv;
   gv(1)(2)(3);

Open in new window

I just noticed that you did not want to define explicit temporary vectors. gv is a temporary, but you don't have to use it:
   vector<genVec> enclosing_vector;
   genVec gv;
   gv(1)(2)(3);
   enclosing_vector.push_back(gv);
   enclosing_vector.push_back( genVec() );
   enclosing_vector.push_back( genVec() (4)(5)(6)(7) );

Open in new window

enclosing_vector now has a size of 3, and it's first element is a vector of size 3; 2nd element is a vector of size 0; 3rd vector is a vector of size 4.
ASKER CERTIFIED SOLUTION
Avatar of phoffric
phoffric

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial