• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2270
  • Last Modified:

How do I push an object to the front fo an C++ STL vector?

I've been using the C++ STL vector class for years but I still need to get more familiar with it.  I always use the push_back() function, but can't seem to find an equivalent push_front() function (at least push_front is not in my Borland C++ documentation).  If someone can show me how to push to the front of a vector (both with and without using iterators), you get 500 points.  For the iterator solution, you will probably need to use the insert() function.  

NOTE: The solution must push an object--not a scalar type.  Just make a simple class with, say, two attributes.

Thanks,
Kleencoder.
0
kleencoder
Asked:
kleencoder
  • 4
  • 2
  • 2
  • +1
2 Solutions
 
ZoppoCommented:
Hi kleencoder,

std::vector < TheClass > v;

// fill vector here ...

// then push one at front:

TheClass x;

v.insert( v.begin(), x );

Hope that helps,

ZOPPO
0
 
jkrCommented:
You could use 'insert()', i.e.

vector<myobject> v;

myobject mo;

v.insert(v.begin(),mo);

which will push 'mo' to the beginning (front) of the vector.
0
 
kleencoderAuthor Commented:
Oh yeah... the begin() function.  I forgot it returned an iterator!

Man, you guys are quick.  But jkr, you were beaten to the punch by a measly one minute!!!!!   Shall I split points Zoppo?  I leave it up to you since you were technically first.

Thanks!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
ZoppoCommented:
Hi,

doesn't matter, you can split if you want - for me it's quite a satisfaction to be faster than jkr (just kidding ;o)

Havea nice day,

best regards,

ZOPPO
0
 
Infinity08Commented:
Maybe it's better to use a
0
 
Infinity08Commented:
continued :

Maybe it's better to use a deque instead of a vector. Inserting at the front of a vector is not very efficient.

        http://www.cplusplus.com/reference/stl/deque/
0
 
Infinity08Commented:
Oh, and in case you didn't notice, a deque has a push_front() and a push_back() ;)
0
 
kleencoderAuthor Commented:
Infinity08,

This is some very good and useful advice.  I wasn't thinking of a queue for my particular application, but it certainly would work.  Although it did not answer my original question, the advice is much appreciated.  I just wish I could give everyone 500 points!
0
 
Infinity08Commented:
>>  I wasn't thinking of a queue

Note that I was talking about a deque, not a queue - they're pretty different.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now