Solved

generating vectors on the fly

Posted on 2014-01-09
7
183 Views
Last Modified: 2014-01-31
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?
0
Comment
Question by:LuckyLucks
  • 3
  • 3
7 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 39769184
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?
0
 

Author Comment

by:LuckyLucks
ID: 39775597
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.
0
 
LVL 86

Expert Comment

by:jkr
ID: 39775601
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).
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 86

Expert Comment

by:jkr
ID: 39808196
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: http://www.experts-exchange.com/Programming/Languages/CPP/Q_28346593.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.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 39809457
>> 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

0
 
LVL 32

Expert Comment

by:phoffric
ID: 39809476
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.
0
 
LVL 32

Accepted Solution

by:
phoffric earned 500 total points
ID: 39809492
The parens are OK, but harder to type and not too stylish. You can change the () operator to, say, <<. I like this style better since it conveys the notion of pushing values to an object (like cout). Now you could write:
enclosing_vector.push_back( genVec() << 4 << 5 << 6 << 7 );

Open in new window

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question